0% found this document useful (0 votes)
178 views

Class 9 Computer Chapter - 6 & 3 (Functions)

The document discusses user-defined functions or methods in Java. It defines what a method is, provides examples of method definitions and calls, and discusses key concepts like parameters, return types, access modifiers, method signatures, and the differences between pure and impure methods. Several questions are asked and answered about methods, covering topics like advantages of methods, syntax, parameters, return statements, function prototypes, static vs non-static methods, and more.

Uploaded by

Mamata Biswas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
178 views

Class 9 Computer Chapter - 6 & 3 (Functions)

The document discusses user-defined functions or methods in Java. It defines what a method is, provides examples of method definitions and calls, and discusses key concepts like parameters, return types, access modifiers, method signatures, and the differences between pure and impure methods. Several questions are asked and answered about methods, covering topics like advantages of methods, syntax, parameters, return statements, function prototypes, static vs non-static methods, and more.

Uploaded by

Mamata Biswas
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CLASS : IX

CHAPTER: 6 ( USER-DEFINED FUNCTIONS / METHODS )


CHAPTER: 3 ( CLASS :X BOOK)
Q1 What is Function?
Answer : A method is named blocked of code within class. It executes a defined set of
instructions when called from another part of the program. Function is also known as
method.
Q2. Give the advantages of methods
Answer : Advantages of methods :
i) Code reusability
ii) To allow us to cope with complex problem.
iii) To hide low level details
iv)To create conceptual unit

Q3. Give the syntax of method Definition. Give example also


Answer : The syntax of method definition is :
< Access-Specifier>< Modifier> < Data Type > method /function name(Parameter list)
{
Block of statements/ Method Body;
}

Example of method definition:

public void sum( int a , int b) //Method prototype


{//Defining impure function
System.out.println("Sum Value="+(a+b));
}//End of function

public int sum(int x, int y)


{//Defining pure function
return (x+y);
}
Q4. What is parameter list ?
Answer: The parameter list is a comma-separated list of variables of a
method referred to as its arguments or functions. A method /function may
be without any parameters. This is called empty parameter list.
Example : public static in maximum( int a, int b)
{
int ans;
ans =(a>b) ? a : b ;
return ans;
}
Q5. What are access modifiers or access specifiers?
Answer: Access specifiers are used to determine the type of access to the
Function. Access specifier can be either public or protected or private.

Q6. What is method signature?


Answer : The method name along with the list of parameters used
in the method prototype is known as method signature .
It is a part of method prototype.
Example
public int product(int x, int y)
{//Defining pure function
return (x*y);
}
Here , int x , int y are the signature of method product

Q7. What is the difference between formal and actual parameter? Give
example.
Answer: Formal Parameter : Formal Parameters are the parameters that
appear in function definition.
Actual Parameter: Actual Parameter are the parameters that appear at
function call statement.
Example :
public int product(int x, int y)
{//Defining pure function
return (x*y);
}
Here , int x , int y are the formal parameter of the function.
int p=product(12,2);
Here 12, 2 are the actual parameter of the function

Q8. What are the uses of return statement.


Answer : The return statement is used to return the value from the function.
Example : public boolean positive( int x)
{
if(x>0)
return true;
else
return false;
}
Q9. Differentiate between pure and impure method .
Answer:
Pure function/ Method Impure function/ method
1. It returns the value. 1. It doesn't return any value.
2. It can't change state of 2. It can change the state of an
an object. object.

Example : Example
public int product(intx, inty) public void display(int x, int y)
{ {
return (x*y); System.out.println("Product:"+(x*y));
} }

Q.10. Write two uses of return statement.


Answer : a) An immediate exit from the function is causes as soon as
return statement is encountered and program control passes back to
the operating system.
b) The use of return statement is that it is used to return a value to the
calling code.

Q11. Define function prototype with example .


Answer : Method/ Function Prototype: Method/Function Prototype is the first line
definition that tells the program about the type of the value required by the method
and the number and type of arguments .
Example : int absval( int num)
public static int maximum(int a, int b)

Q12.What is the role of keyword void declaring functions ?


Answer : The void means that the method does not return a value. This ensures that
method cannot be used in an assignment statement. Only the methods that return a
value can be used in expressions and assignment statements.

Q13. Differentiate between non- static and static methods .


Answer : The differences between non- static and static methods are as follows :
Static method : The static method does not need an instance to be called. It can be
called directly using the class name.
Non-static method :Non static method is also known as an instance method . An
instance is required to call the non-static method . It cannot be called directly.
Q14. Name the following:
a) The parameters that appear in function definition is known as _________.
Answer : Formal Parameter
b) Method not returning any value has return types as_________.
Answer: void type
c) The first line of method definition that tells about the type of return value along
with number and type of arguments is called ___________.
Answer : Method prototypes
d) The parameters that appear in function call is known as _________.
Answer : Actual Parameter

e) In Java method reside in _______________.


Answer : Class

f) The number and type of arguments of a method are known as____________.


Answer : Method signature

g) A method can return ________ value.


Answer : One (1)

i) The method that changes the state of an object is known as _______________.


Answer : Impure method

j) The scope of a local variable is limited to the ______________.


Answer : Method or block it is declared

You might also like