Parameter Passing - : Call by Value, Call by Reference, Recursion
Parameter Passing - : Call by Value, Call by Reference, Recursion
call by value,
Call by reference,
Recursion
Call by value
• In general, there are two ways that a computer
language can pass an argument to a subroutine
(method).
• The first way is call-by-value.
• This approach copies the value of an argument
into the formal parameter of the subroutine.
• Therefore, changes made to the parameter of
the subroutine have no effect on the argument.
Call by value
• The second way an argument can be passed is
call-by-reference.
• In this approach, a reference to an argument
(not the value of the argument) is passed to
the parameter.
• Inside the subroutine, this reference is used to
access the actual argument specified in the
call.
Call by value
• This means that changes made to the
parameter will affect the argument used to
call the subroutine.
• As you will see, Java uses both approaches,
depending upon what is passed.
Call by value
• In Java, when you pass a primitive type to a
method, it is passed by value.
• Thus, what occurs to the parameter that
receives the argument has no effect outside
the method.
LTC: Call by value
• For example, consider the following program:
// Primitive types are passed by value.
class Test {
void meth(int i, int j) {
i *= 2;
j /= 2;
}
}
class CallByValue {
public static void main(String args[]) {
Test ob = new Test();
int a = 15, b = 20;
System.out.println("a and b before call: " +
a + " " + b);
ob.meth(a, b);
System.out.println("a and b after call: " +
a + " " + b);
}
}
• The output from this program is shown here:
a and b before call: 15 20
a and b after call: 15 20
Call by reference
• When you pass an object to a method, the
situation changes dramatically, because
objects are passed by what is effectively call-
by-reference.
• Keep in mind that when you create a variable
of a class type, you are only creating a
reference to an object.
• Thus, when you pass this reference to a
method, the parameter that receives it will
refer to the same object as that referred to by
the argument.
• This effectively means that objects are passed
to methods by use of call-by-reference.
• Changes to the object inside the method do
affect the object used as an argument.
LTC:Call by reference
• For example, consider the following program:
// Objects are passed by reference.
class Test {
int a, b;
Test(int i, int j) {
a = i;
b = j;
}
// pass an object
void meth(Test o) {
o.a *= 2;
o.b /= 2;
}
}
class CallByRef {
public static void main(String args[]) {
Test ob = new Test(15, 20);
System.out.println("ob.a and ob.b before call: " + ob.a + " " +
ob.b);
ob.meth(ob);
System.out.println("ob.a and ob.b after call: " +
ob.a + " " + ob.b);
}
}
• This program generates the following output:
ob.a and ob.b before call: 15 20
ob.a and ob.b after call: 30 10
• As you can see, in this case, the actions inside
meth( ) have affected the object used as an
argument.
REMEMBER
1. When a primitive type is passed to a method,
it is done by use of call-by-value.
2. Objects are implicitly passed by use of call-by-
reference.
Recursion
• Java supports recursion.
• Recursion is the process of defining something
in terms of itself.
• As it relates to Java programming, recursion is
the process that allows a method to call itself.
• A method that calls itself is said to be
recursive.
• The classic example of recursion is the
computation of the factorial of a number.
• The factorial of a number N is the product of
all the whole numbers between 1 and N.
• For example, 3 factorial is 1 × 2 × 3, i.e., 6.
• Here is how a factorial can be computed by
use of a recursive method:
LTC: Recursion
// A simple example of recursion.
class Factorial {
// this is a recursive method
int fact(int n) {
int result;
if(n==1) return 1;
result = fact(n-1) * n;
return result;
class Recursion {
public static void main(String args[]) {
Factorial f = new Factorial();
System.out.println("Factorial of 3 is " + f.fact(3));
System.out.println("Factorial of 4 is " + f.fact(4));
System.out.println("Factorial of 5 is " + f.fact(5));
}
}
• The output from this program is shown here:
Factorial of 3 is 6
Factorial of 4 is 24
Factorial of 5 is 120
• fact( ) is called with an argument of 1, the
function returns 1; otherwise, it returns the
product of fact(n–1)*n.
• To evaluate this expression, fact( ) is called
with n–1.
• This process repeats until n equals 1 and the
calls to the method begin returning.
• To better understand how the fact( ) method
works, let’s go through a short example.
• When you compute the factorial of 3, the first
call to fact( ) will cause a second call to be
made with an argument of 2.
• This invocation will cause fact( ) to be called a
third time with an argument of 1.
• This call will return 1, which is then multiplied
by 2 (the value of n in the second invocation).
• This result (which is 2) is then returned to the
original invocation of fact( ) and multiplied by
3 (the original value of n).
• This yields the answer, 6.
• You might find it interesting to insert println( )
statements into fact( ), which will show at what
level each call is and what the intermediate
answers are.
• The main advantage to recursive methods is
that they can be used to create clearer and
simpler versions of several algorithms than can
their iterative relatives.
End of session