Call by reference vs Call by value

Call by Value vs Call by Reference :

Call by value in function is passing argument method in function, in which copies of actual arguments are passed to the function.Call by Reference  in function is passing argument method in function, in which actual arguments are passed to the function.

Call by reference vs Call by value

Call by value:

The call by value method inserts the value of an argument into the function’s formal parameter.As a result, adjustments made to the main function’s parameter have no impact on the argument.

This method of transferring parameters copies the values of the actual parameters to the formal parameters of the function, which are kept in several memory locations.
Therefore, modifications done inside such functions do not affect the caller’s actual parameters.

Example :

Run
#include<stdio.h> 
void change(int a, int b){     // function to chane the value
  
    a = 100, b=200;
}
int main() {

    int a = 10, b =20;
    int sum =0;
    sum = a+b;
    printf("the sum of values before function calling : %d \n", sum); 
    change(a,b);        // caliing the function
    sum = a+b;          // add again the value

    printf("the sum of values after function calling : %d ", sum); 
   return 0;
}

Output:

the sum of values before function calling : 30

the sum of values after function calling : 30

Call by Reference :

The address of an argument is copied into the formal parameter using the call by reference mechanism.In this manner, the actual parameter used in the function call is accessed using the address.In other words, changing the parameter will modify the passing argument.

The memory allocation in this method corresponds to the actual parameters.
The value stored at the address of the actual parameter is the starting point for all operations in the function, and the updated value will be placed there as well.

Example:

Run
#include<stdio.h>

void addition(int *val){ // function to add

    (*val) += 100;
}

int main(){

    int val = 10;
    printf("The value of integer before function calling : %d\n", val);

    addition(&val); // function calling

    printf("The value of integer after function calling : %d", val);

    return 0;
}

Output :

The value of integer before function calling : 10
The value of integer after function calling : 110
Call by value Call by reference
In calling function, we pass value of variable In calling function, we pass address of variable
A copy of variable is created A real argument is passed to the function
Changes made in the function does not effect the real variable Changes made in the function effect the real variable

Prime Course Trailer

Related Banners

Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription

Get over 200+ course One Subscription

Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others

Checkout list of all the video courses in PrepInsta Prime Subscription

Checkout list of all the video courses in PrepInsta Prime Subscription