Call by value and Call by reference

The arguments that appear in function call statement are called actual arguments or parameters.

The arguments that appear in function definition are called formal arguments.

Call by value:

The call by value method copies the value of actual parameters into formal parameters, i.e. the function creates its own copy of parameter values and then uses them.

            Thus, in call by value method, any change made in the formal parameters does not reflected on the actual parameters. Generally all primitive data type such as int, double, char etc. are by default passed by value.           

Example:

import java.util.*;

class CallbyVal

    {

    int reverse(int n) //function definition, n is formal argument.

    {

    int d,r=0;

    while(n!=0)

    {

        d=n%10;

        r=r*10+d;

        n=n/10;

    }

    return r;

    }

    void main()

    {

    int n,a;

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter a Number ");

    n=sc.nextInt();

    a=reverse(n); //function call, n is an actual argument.

    if(a==n)

    System.out.println("Number is Palindrome");

    else

    System.out.println("Number is not Palindrome");

   }

  }

In above example the actual and formal both arguments are represented by n. After function execution the formal n becomes 0 but actual n will remain unchanged.

Call by reference:

In call by reference method, instead of passing a value to the function, a reference to the original variable is passed. When a function is called by reference, the called function does not create its own copy of original values, it refers to the original values only by different names.

            Thus, in call by reference method any change made in the formal parameter will be reflected on the actual parameters also. Generally all reference types such as array, objects of class type are passed by reference.  

Example:

import java.util.*;

class CallbyRef

    {

    void sort(int a[])

    {

    int i,j,l,t;

    l=a.length;

    for(i=0;i<l-1;i++)

    {

      for(j=i+1;j<l;j++)

      {

          if(a[i]>a[j])

          {

              t=a[i];

              a[i]=a[j];

              a[j]=t;

          }

         }

        }

    }

    void main()

    {

    int i,n;

    Scanner sc=new Scanner(System.in);

    System.out.println("Enter size of array");

    n=sc.nextInt();

    int x[]=new int[n];

    System.out.println("Enter elements of Array");

    for(i=0;i<n;i++)

    x[i]=sc.nextInt();

    System.out.println("Original Array");

    for(i=0;i<n;i++)

    System.out.print(x[i]+" ");

    sort(x);

    System.out.println("Sorted Array");

    for(i=0;i<n;i++)

    System.out.print(x[i]+" ");

    }

  }

In above example the actual argument is x and formal argument is a. After function execution the formal a becomes sorted but it is reference of actual x therefore change in formal will also reflect actual and sorted elements can be printed in main () function.

 

   

   

 

Make a free website with Yola