You must Sign In to post a response.
  • Category: Programming

    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

    1 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);
    }


  • Sign In to post your comments