what is Call by value and call by reference?

There are two ways to pass value or data to function in C language: call by value and call by reference. Original value is not modified in call by value but it is modified in call by reference.


Call by value in C :-

Function call by value is the default way of calling a function in C programming. By default, C programming uses call by value to pass arguments.

In call by value, original value is not modified.


Calling a function by value means, we pass the values of the arguments which are stored or copied into the formal parameters of the function. Hence, the original values are unchanged only the parameters inside the function changes.


In call by value, value being passed to the function is locally stored by the function parameter in stack memory location. If you change the value of function parameter, it is changed for the current function only. It will not change the value of variable inside the caller method such as main().


#include<stdio.h> void calc(int x);
int main()
{
int x = 10;
calc(x);
// this will print the value of 'x'
printf("\nvalue of x in main is %d", x);
return 0;
}
void calc(int x)
{
// changing the value of 'x'
x = x + 10 ;
printf("value of x in calc function is %d ", x);
}

Output

value of x in calc function is 20
value of x in main is 10

Example

#include<stdio.h> void change(int num) {
printf("Before adding value inside function num=%d \n",num);
num=num+100;
printf("After adding value inside function num=%d \n", num);
}
int main() {
int x=100;
printf("Before function call x=%d \n", x);
change(x);//passing value in function
printf("After function call x=%d \n", x);
return 0;
}

Output :-

Before function call x=100

Before adding value inside function num=100
After adding value inside function num=
After function call x=100

Note :-

  • Actual parameters: The parameters that appear in function calls.
  • Formal parameters: The parameters that appear in function declarations.


  • Call by reference in C :-

    In call by reference, original value is modified because we pass reference (address). Here, address of the value is passed in the function, so actual and formal arguments shares the same address space. Hence, value changed inside the function, is reflected inside as well as outside the function.

    In call by reference we pass the address(reference) of a variable as argument to any function. When we pass the address of any variable as argument, then the function will have access to our variable, as it now knows where it is stored and hence can easily update its value.
    In this case the formal parameter can be taken as a reference or a pointer in both the cases they will change the values of the original variable.


    Example

    #include<stdio.h> void calc(int *p); // functin taking pointer as argument
    int main()
    {
    int x = 10;
    calc(&x); // passing address of 'x' as argument
    printf("value of x is %d", x);
    return(0);
    }
    void calc(int *p) //receiving the address in a reference pointer variable
    {
    /* changing the value directly that is stored at the address passed */
    *p = *p + 10;
    }

    output

    value of x is 20

    Example

    #include<stdio.h> void change(int *num) {
    printf("Before adding value inside function num=%d \n",*num);
    (*num) += 100;
    printf("After adding value inside function num=%d \n", *num);
    }
    int main() {
    int x=100;
    printf("Before function call x=%d \n", x);
    change(&x);//passing reference in function
    printf("After function call x=%d \n", x);
    return 0;
    }

    output

    Before function call x=100
    Before adding value inside function num=100
    After adding value inside function num=200
    After function call x=200

    Difference between call by value and call by reference in c






    Previous Next

    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here


    Ittutorial