0% found this document useful (0 votes)
30 views6 pages

Understanding Java Functions and Methods

jj,jj

Uploaded by

viratsagarlp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views6 pages

Understanding Java Functions and Methods

jj,jj

Uploaded by

viratsagarlp
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

C ITY MONTESSORI SCHOOL

SECTOR-D, LDA COLONY, KANPUR ROAD, LUCKNOW (INDIA)

FUNCTIONS
Q. What is a function?
Ans. A function or method is a self-contained block of executable statements, that perform a specific task.

Q. State the advantages of using a function.


Ans.
i) It reduces complexity by breaking the program into smaller units.
ii) It enables reusability of code.
iii) It checks code redundancy i.e. repetition of code.

Q. Where do functions reside?


Ans. Functions form an integral part of a class as they define the behavior of an object. Hence, functions may either
belong to:
i) User – defined classes.
ii) Pre – defined classes of Java API. For e.g. [Link]( )

Q. State the types of functions in a class.


Ans. Static and Non – static functions.

Parts of a Function
// Skeletal structure of a function-oriented program
class abc {
static void sqr(int x)  Function declaration
{
[Link](x  x) ;  Function definition
}
public static void main( )
{
……………
sqr(5) ;  Function calling
……………
}
}
The parts of a function are:
i) Function definition
ii) Function declaration
iii) Function calling

Function Definition (Syntax):


[modifier] type function-name ([parameter–list])
{ //…..function body
[ variable definitions ]
[ Java statements ]
[ return expression ; ]
}
where,
i) modifier may be static, final etc.
ii) type specifies the type of value returned by the method.
(25)
iii) parameter–list specifies a comma separated list of declared variables which receive input values sent to the
method.
iv) If the method has no or zero parameters, then the parameter list will be empty.

Some examples of method definitions are:


static int add(int a, int b) // 2-parameter function
{
int c = a + b ;
return c ;
}
public static void main( ) { // 0-parameter function
int a = 2, b = 3 ;
[Link](a + b) ;
}

Function Declaration
Q. Explain function prototype with an example.
Ans. A function prototype or declaration is the first line of the function definition which describes the function interface
to the compiler. It specifies the function’s return type along with the number and type of parameters passed to it. For
e.g.
static int max(int a, int b) {…} // static method
void abs(int a) {…} // non-static method

Q. Write the function prototype for the function “sum” that takes an integer variable (x) as its argument and returns a
value of float data type.
Ans. float sum(int x)

Q. Write the function prototype for a function “check” which receives a character ch and an integer n and returns true
or false.
Ans. boolean check(char ch, int n)

Q. What is function signature? Give an example


Ans. It refers to the function’s name along with the number and type of arguments passed to it. For e.g.
add(int a, int b) {…}

Function Calling: A function is called or accessed by using its name and passing parameters, if any. For e.g.
// Function calling and parameter passing
class fun {
static void add(int a, int b)
{
[Link](a + b) ;  Called function
}
public static void main( )
{
int a = 2, b = 3 ;
add(a , b) ; // function call  Calling function
}
}
Q. Write a Java statement to compare 50 and 500 by calling the function void compare(int m1, int m2).
Ans. compare (50, 100) ;
Parameter: A parameter is variable that contains a value to be used in a function. It may be of 2 types:
1. Formal parameter
2. Actual parameter
(26)
F ormal parameter Actual parameter

P arameter that ap p ears in a P arameter that ap p ears in a


function definition. function call.
Holds a value received by a H o l d s a v a l u e s e n d t o a
function. function.

Arguments: An argument is an input value that is referenced by a parameter name. It may be of 2 types:
1. Formal argument
2. Actual argument

F ormal argument Actual argument

It is a va lue r e c e ive d b y a
It is a value send to a functio n.
functio n.
It is used when a function is It is used when a function is
d efined. called.
An argument may belong to:
i) a simple type like int, char, float, double etc.
ii) a composite type like object, array, string.

Argument Passing
An argument may be passed to a method
i) By value.
ii) By reference

Q. How are the following passed in Java ? i) primitive types ii) reference types
Ans.
i) Simple or primitive types are passed by value.
ii) Composite or reference types are passed by reference.

Pass or Call by Value:


Q. What is call by value? Give an example.
Ans. In call by value, the value of an actual parameter is copied into the formal parameter of the function. Hence,
changes made to the formal parameter are not reflected on the argument used to call it.

// Passing primitive type by value


class test {
static void change(int a) {
a=a3;
[Link](“change: a = “+a) ;
}
public static void main( ) {
int x = 2 ; // x is of simple type
[Link](“main: x = “+x) ;
change(x) ; // function call by value
[Link](“main: x = “+x) ;
}
}
(27)
Output:
main: x = 3
change: a = 6
main: x = 3

Pass or Call by Reference:


Q. Explain call by reference with an example.
Ans. In call by reference , the reference of an actual parameter is passed to the formal parameter of the function.
Hence, changes made to the formal parameter are reflected on the argument used to call it.
// Passing composite type by reference
class test {
static void change(int b[ ]) {
b[0] = b[0]  b[0] ;
b[1] = b[1]  b[1] ;
}
public static void main( ) {
int a[ ] = {2, 3} ; // a[ ] is of composite type
[Link](“Before call: “+a[0]+ “,”+a[1]) ;
change(a) ; // function call by reference
[Link](“After call: “+a[0]+ “,”+a[1]) ;
}
}
Output:
Before call: 2,3
After call: 4,9

Call by value Call by reference

Uses simple type of data as Uses composite type of data


argument. as argument.
Formal parameter is copy of Formal parameter is reference
actual parameter. of actual parameter.
Da ta in a c tua l p a ra me te r Data in actual parameter gets
remains intact. changed.
Returning from a Function:
Q. Explain the function of a return statement with an example.
Ans. The return statement has two uses:
i) It is used to return a value to the calling function.
ii) It terminates the function immediately and the control passes back to the calling function.
For e.g. static int add(int a, int b)
{
int c = a + b ;
return c ;
}

Q. What is the role of ‘void’ keyword in declaring functions?


Ans. A function that does not return any value is declared as void.

Q. How many values can a function return at the most?


Ans. A function can return only a single value which may be either of primitive or reference types.

Q. Where does the return value from a function go? Give an example.
(28)
Ans. The return value of a function goes back to the location from where the function was called. For e.g.
class abc {
static double area(int r) { 
double a ;
a = 3.14  r  r ;
return a ;
}
public static void main( ) {
int rad = 2 ;
double ar ;
ar = area(rad) ; // function call
[Link](“Area= “+ar) ;
}
}
Q. Can a return statement be given without a value or an expression? Explain.
Ans. In a void type function, a return statement can be given without a value or an expression. In such a function, the
return statement is used to terminate the function. For e.g.
static void area(int r) {

double a ;
a = 3.14  r ? r ;
[Link](“Area= “+a) ;
return ;
}
Pure and Impure Functions
Q. Define an impure function with an example.
Ans. An impure or modifier function is one that changes the state of its parameters. For e.g. procedural functions.
static void swap(int a, int b) { // an impure procedural function
int c ;
c=a;
a=b;
b=c;
}
Q. What do you mean by side-effect of an impure function?
Ans. The side-effect of an impure function is the change in the value of its received parameters.

Q. What is a pure function? Give an example.


Ans. A pure function is one that returns a value but does not change the state of its parameters. For e.g. boolean
functions, computational functions.
static int diff(int a, int b) { // a pure computational function
int c = a – b ;
return c ;
}

P ure func tio ns Imp ure functio ns

The y d o no t change the sta te


The y mo d ify the state o f the
o f th e r e c e iv e d p a r a m e t e r,
r e c e iv e d p a r a m e t e r, h e n c e
h e n c e t h e y h a v e n o s id e -
the y ca use sid e- effe cts.
effe c ts.

The y ma y o r ma y no t return a
The y a lw a ys re turn a value .
va lue.

(29)
Method Overloading
Q. Explain function overloading with an example.
Ans. Function overloading is a concept in which a function name has multiple definitions that differ in the number
and/or types of parameters. For e.g.
int area(int a, int b) {…}
float area(float a, float b) {…}

// Polymorphism using function overloading


class abc {
static void add(int a, int b) {
[Link](a + b) ;
}
static void add(String a, String b) {
[Link](a + b) ;
}
public static void main( ) throws Exception {
add(2,5) ;
add(“New “, “Delhi”) ;
}
}
Output:
7
New Delhi

Q. When multiple definitions share the same function name, what makes them significantly different?
Ans. In such a situation, the several definitions are distinguished by their signatures i.e. they are differentiable by the
number and/or types of their parameters.

Q. What is the significance of function overloading?


Ans.
i) It implements polymorphism.
ii) It reduces the number of comparisons as there are no if-else statements in the program and thereby the program
runs faster.

Q. How does function overloading implement polymorphism?


Ans. By means of function overloading the same function name is associated with different definitions and each
definition can be used for a different purpose. Thus, a function behaves differently in different circumstances thereby
implementing polymorphism.

(30)

Common questions

Powered by AI

In "call by value," the value of an actual parameter is copied into the formal parameter, meaning changes to the formal parameter do not affect the actual parameter. Conversely, "call by reference" passes the actual parameter's reference, so changes to the formal parameter are reflected on the actual parameter .

A function signature, consisting of the function's name combined with its parameters' number and types, distinguishes functions in overloading scenarios and ensures the correct function is called during execution .

Functions reduce complexity by breaking the program into smaller units, enable code reusability, and prevent code redundancy by minimizing repetition .

The "void" keyword indicates that a function does not return a value. In such functions, a return statement can be used solely to terminate the function without returning a specific value .

Function overloading allows multiple definitions to share the same function name, differentiated by their signatures, thus supporting polymorphism by enabling the same function name to operate in various ways depending on its definitions. This reduces the need for conditional statements and accelerates the program .

A function prototype assists in the compilation process by describing the function interface to the compiler, specifying the function’s return type and the number and types of parameters .

In Java, primitive types are passed by value, meaning the original variable’s value is not altered by the function. Reference types are passed by reference, allowing the function to modify the original variable .

A pure function returns a value without altering the state of its parameters, avoiding side effects, while an impure function modifies parameter states and may cause side effects .

Method overloading is preferred when a single function name is needed for operations with different parameter types or counts, enhancing readability and flexibility. Overloaded methods must differ in their signatures, specifically in parameter number and/or types .

A return statement is used to return a value to the calling function and terminates the function immediately, with control passing back to the caller. This can influence program flow by determining the point at which a function stops executing and what value is given back to the caller .

You might also like