Function calling in Java can be done using two techniques, call be reference and call by value. We know that whenever we define any function in our program, to execute the codes inside the function body the function must be called. This is also known as invoking the function. In many functions we define argument. The argument may be single or muttiple. We can even define functions without any argument. Such functions are known as zero argument function or function without argument. Call by value and call by reference has nothing to do with such functions.
The values which are passed to a function are known as actual argument and the variables or objects which are declared inside the parenthesis of the defined function are known as formal argument.
For example, suppose we have two variables ‘int a’ and ‘int b’ with 3 and 5 as values. We are calling a function using a statement like ‘object_name.function_name(a,b);’. Here ‘a’ and ‘b’ are called actual argument. Again suppose the function ‘function_name()’ is defined like:
void function_name(int x, int y)
{
Statements
}
Here’int x’ and ‘int y’ are called formal arguments.
Calling a function using call by value technique
Whenever we use primitive variables as actual argument, the function calling technique is known as call by value. As the actual argument or arguments are primitive variable, the formal arguments should also be primitives. In such calling technique of function, the values of the actual arguments or parameters are not affected in the function body as function creates new set of variables which are declared as formal arguments to copy the values of actual arguments and any modification done within the function body will affect formal argument variables only, not the actual arguments.
import java.io.*;
class CallByValue
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a,b;
public void take() throws Exception
{
System.out.println("Enter 1st Value:");
a=Integer.parseInt(br.readLine());
System.out.println("Enter 2nd Value:");
b=Integer.parseInt(br.readLine());
System.out.println("Values before calling function:"+ a + ","+b);
change(a,b);
System.out.println("Values after calling function:"+ a + ","+b);
}
void change(int x, int y)
{
int t;
t=x;
x=y;
y=t;
System.out.println("Values within function body:"+ x + ","+y);
}
public static void main(String args[])throws Exception
{
CallByValue ob=new CallByValue ();
ob.take();
}
}
The above program is an example of call by value. The actual arguments in this program are ‘int a’ and ‘int b’. The values are displayed within the function ‘public void take()’ twice, one before calling the ‘void change()’ function and another display is after calling the function. Both the display will show same values as the exchange of values are done on the variables of the function ‘void change()’. The name of the formal arguments of the function can be same as that of actual arguments.
Whenever we use objects of any class or array as actual argument, the function calling technique is known as call by reference. We know that simple declaration of object does not create object and in java, declared objects can refer created objects of same class or sub class. A reference object deals with the actual members of the passed argument and in such case any modification done inside the function body will have an impact on the actual object.
import java.io.*;
class CallByRef
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a,b;
public void take() throws Exception
{
System.out.println("Enter 1st Value:");
a=Integer.parseInt(br.readLine());
System.out.println("Enter 2nd Value:");
b=Integer.parseInt(br.readLine());
}
void change(CallByRef ob)
{
ob.a=100;
ob.b=200;
}
public void display()
{
System.out.println("Values :"+ a + ","+b);
}
public static void main(String args[])throws Exception
{
CallByRef ob1=new CallByRef();
CallByRef ob2=new CallByRef();
ob1.take();
ob2.take();
System.out.println("Values of the second object before calling 'change()' function :");
ob2.display();
ob1.change(ob2);
System.out.println("Values of the second object after calling 'change()' function :");
ob2.display();
}
}
Here in this program ‘CallByRef ob’ declared as formal argument in ‘void change()’ function refers the object ‘ob2’ of CallByRef class. Using the reference object ‘ob’ we have changed the data member values which affects the values of the acual argument ‘ob2’.
The values which are passed to a function are known as actual argument and the variables or objects which are declared inside the parenthesis of the defined function are known as formal argument.
For example, suppose we have two variables ‘int a’ and ‘int b’ with 3 and 5 as values. We are calling a function using a statement like ‘object_name.function_name(a,b);’. Here ‘a’ and ‘b’ are called actual argument. Again suppose the function ‘function_name()’ is defined like:
void function_name(int x, int y)
{
Statements
}
Here’int x’ and ‘int y’ are called formal arguments.
Calling a function using call by value technique
Whenever we use primitive variables as actual argument, the function calling technique is known as call by value. As the actual argument or arguments are primitive variable, the formal arguments should also be primitives. In such calling technique of function, the values of the actual arguments or parameters are not affected in the function body as function creates new set of variables which are declared as formal arguments to copy the values of actual arguments and any modification done within the function body will affect formal argument variables only, not the actual arguments.
Program to demonstrate call by value
import java.io.*;
class CallByValue
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a,b;
public void take() throws Exception
{
System.out.println("Enter 1st Value:");
a=Integer.parseInt(br.readLine());
System.out.println("Enter 2nd Value:");
b=Integer.parseInt(br.readLine());
System.out.println("Values before calling function:"+ a + ","+b);
change(a,b);
System.out.println("Values after calling function:"+ a + ","+b);
}
void change(int x, int y)
{
int t;
t=x;
x=y;
y=t;
System.out.println("Values within function body:"+ x + ","+y);
}
public static void main(String args[])throws Exception
{
CallByValue ob=new CallByValue ();
ob.take();
}
}
The above program is an example of call by value. The actual arguments in this program are ‘int a’ and ‘int b’. The values are displayed within the function ‘public void take()’ twice, one before calling the ‘void change()’ function and another display is after calling the function. Both the display will show same values as the exchange of values are done on the variables of the function ‘void change()’. The name of the formal arguments of the function can be same as that of actual arguments.
Calling a function using call by refence technique
Whenever we use objects of any class or array as actual argument, the function calling technique is known as call by reference. We know that simple declaration of object does not create object and in java, declared objects can refer created objects of same class or sub class. A reference object deals with the actual members of the passed argument and in such case any modification done inside the function body will have an impact on the actual object.
Program to demonstrate call by value
import java.io.*;
class CallByRef
{
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
int a,b;
public void take() throws Exception
{
System.out.println("Enter 1st Value:");
a=Integer.parseInt(br.readLine());
System.out.println("Enter 2nd Value:");
b=Integer.parseInt(br.readLine());
}
void change(CallByRef ob)
{
ob.a=100;
ob.b=200;
}
public void display()
{
System.out.println("Values :"+ a + ","+b);
}
public static void main(String args[])throws Exception
{
CallByRef ob1=new CallByRef();
CallByRef ob2=new CallByRef();
ob1.take();
ob2.take();
System.out.println("Values of the second object before calling 'change()' function :");
ob2.display();
ob1.change(ob2);
System.out.println("Values of the second object after calling 'change()' function :");
ob2.display();
}
}
Here in this program ‘CallByRef ob’ declared as formal argument in ‘void change()’ function refers the object ‘ob2’ of CallByRef class. Using the reference object ‘ob’ we have changed the data member values which affects the values of the acual argument ‘ob2’.
No comments:
Post a Comment