Page 234 - Computer science 868 Class 12
P. 234
For example,
class calculate
{
void product(int a, int b) Parameters
{
System.out.println("Result : "+(a*b));
}
void calculate()
{
int m=5,n=3;
product(m,n); Arguments
}
}
In the above example, m and n are the arguments which are passed during the method call; and a and b are the
parameters which are defined in the function definition.
The arguments are also known as the Actual Parameters where as parameters are also known as Formal Parameters.
8.5 CALLING FUNCTION
After declaring and defining a method, we should call it or invoke it so that the job assigned to the method can be
executed. To do so, there are two different ways:
1. Pass by Value: Here, the values of actual parameters are copied to the formal parameters.
class pass_by_value
{
public void factorial(int a)
{
int i,f=1;
for(i=1;i<=a;i++)
f=f*i;
System.out.println(f);
}
public void main()
{
factorial(5);
}
}
So, in pass by value, any change in the formal parameter will not reflect on the Actual parameter. Also, any type of
Primitive data can be used in Pass by value.
2. Pass by Reference: Reference here means address. In this type of programming, the actual parameter and the
formal parameter share the same location in the memory. This means any change in the formal parameter while
the method is executing, the actual parameter also changes.
Program 1 Write a program to calculate the sum of all the numbers of array ar[5] in the method add()
and keep it in the last position of the array.
1 class pass_by_reference
2 {
3 void add(int arr[])
4 {
232232 Touchpad Computer Science-XII

