You must Sign In to post a response.
  • Difference between call by value and call by reference


    Are you preparing for Diploma In Computer engineering Exam? Searching for answer to understand the difference between call by value and call by reference? Here, on this Ask Expert page find responses from experts.

    Following is a question that was asked in the question paper of Diploma in Computer Engineering (Summer 2019)conducted by MSBTE Mumbai, for subject Programming in C(22226).

    This question is asked for 4 marks and hence requires some explanation. Please let me know the solution.

    Q) Distinguish between call by value and call by reference. [4 Marks]
  • Answers

    2 Answers found.
  • In procedural oriented programming(C Language), call by value and call by reference are two types of functions.
    Function:- Function is a self contained block of program which performs the predefined task.
    Syntax:- Return Data Type Name of Function(Argument list);

    A) Call by value type function includes

    1) In call by value type function actual values of variables are passed as argument to function and that is the reason this type of function is called as call by value.
    2) In function definition block these actual values are received in appropriate data type variables.
    3) If any change is made in received values inside the function definition block then it does not reflect any effect on actual values in calling function.
    4) Example of call by value function
    int add(int, int); // declaration of function
    int r,a,b;
    r=add(a,b); // function call
    5) Sample program for demonstration of call by value
    #include
    #include
    void main(void)
    {
    int add(int, int); // declaration of function
    int r,a,b;
    clrscr();
    printf("Enter any two numbers\n");
    scanf("%d%d",&a,&b);
    r=add(a,b); //call to function
    printf("\nvalue of a is %d\nvalue of b is %d\naddition is %d",a,b,r);
    getch();
    }
    int add(int x, int y) //definition of function
    {
    int f;
    f=x+y;
    return(f);
    }

    B) Call by reference function includes
    1) In call by reference type function instead of actual values of variables their addresses are passed as argument to function and that is the reason this type of function is called as call by reference.
    2) In function definition block these addresses of variables are received in appropriate data type pointer variables.
    3) If any change is made in received values inside the function definition block then it directly reflect on actual values in calling function.
    4) Example of call by reference function
    int add(int *, int *); // declaration of function
    int r,a,b;
    r=add(&a,&b); // function call
    5) Sample program for demonstration of call by reference
    #include
    #include
    void main(void)
    {
    int add(int *, int *); // declaration of function
    int r,a,b;
    clrscr();
    printf("Enter any two numbers\n");
    scanf("%d%d",&a,&b);
    r=add(&a,&b); //call to function
    printf("\nvalue of a is %d\nvalue of b is %d\naddition is %d",a,b,r);
    getch();
    }
    int add(int * x, int * y) //definition of function
    {
    int f;
    f=*x + *y;
    return(f);
    }

  • Call by value and Call by reference both are the way to invoke the functions. A function is basically a block of code that is used for the perform the specific task.

    Call by value:- In call by variable, a copy of the variable is passed. if you want to modify the copy of the variable it doesn't affect the actual variable values. Below is the example of the call by value.

    #include
    void swap(int a, int b);
    int main()
    {
    int x = 10, y = 20;
    swap(x, y);
    printf("\n%d\t%d", x, y);
    return 0;
    }
    void swap(int a, int b)
    {
    int tmp;
    tmp = a;
    a = b;
    b = tmp;
    printf("%d\t%d", a, b);
    }

    Call by reference:-In call by reference, instead of passing the variable, we pass the address of that variable. If you want to modify the copy of the variable it will affect the actual variable values. Below is the example of the call by reference.

    #include
    void swap(int *, int *);
    int main()
    {
    int x = 10, y = 20;
    swap(&x, &y);
    printf("\n%d\t%d", x, y);
    return 0;
    }
    void swap(int *a, int *b)
    {
    int tmp;
    tmp = *a;
    *a = *b;
    *b = tmp;
    printf("%d\t%d", *a, *b);
    }


  • Sign In to post your comments