Here you will learn about difference between call by value and call by reference in C.
There are two different ways of passing values to functions: call by value and call by reference. They are also called as pass by value and pass by reference.
Call by Value in C
In call by value method a copy of actual parameters is passed which is stored in formal parameters.
Actual Parameters: The parameters that are passed while calling the function are called actual parameters.
Formal Parameters: The parameters that hold the values of actual parameters are called formal parameters.
In call by value method, if any changes are done in formal parameters then actual parameters are not changed.
Let’s take an example of call by value in C language.
Example:
#include<stdio.h> void fun(int x) { x=x+5; } int main() { int a=10; printf("Before calling\na=%d",a); fun(a); printf("\n\nAfter calling\na=%d",a); return 0; }
Output:
As you can clearly see in output that after calling the function and doing changes in formal parameter x, the actual parameter a is not changed.
Call by Reference in C
In call by reference method, reference or address of actual parameters is passed which is stored in formal parameters.
If any changes are done in formal parameters then actual parameters are also changed.
Let’s take an example of call by reference in C language.
Example:
#include<stdio.h> void fun(int *x) { *x=(*x)+5; } int main() { int a=10; printf("Before calling\na=%d",a); fun(&a); printf("\n\nAfter calling\na=%d",a); return 0; }
Output:
You can clearly see in output that after calling the function and doing changes in formal parameter x, the actual parameter a is also changed.
Difference between Call by Value and Call by Reference in C
S. No. | Call by Value | Call by Reference |
1. | A copy of actual parameters is passed into formal parameters. | Reference of actual parameters is passed into formal parameters. |
2. | Changes in formal parameters will not result in changes in actual parameters. | Changes in formal parameters will result in changes in actual parameters. |
3. | Separate memory location is allocated for actual and formal parameters. | Same memory location is allocated for actual and formal parameters. |
If you have any doubts related to above tutorial then you can ask it by commenting below.
best site love u all members
WHICH COUNTRY U BELONG ?????? .. DO U PLAY COC… OR U PLAY TANKI ONLINE…PLZ REPLY
ME TOO .. LOVE THIS WEBSITE
Hello! I just wish to provide a big thumbs up for the good info you’ve here with this post.
I’ll be coming back toyour website for more soon.
Its amazing
really a nice site i loved it
Thank u sir..you cleared my all concept but plz give the more basic example.
gud one bro bt how u make it ??????????
Tq so much but for more clarity u can give some more differences 😊
You Are Awesome .
You clears all my doubts 😊✌️
my dout is clear but not a perfect I just try again &again to understood all things………
thank you sir clear my dout
Want more difference for better understanding…please
Thank you I think required more basic examples
Tq u soo much