Search Your Topic

config
Monday, 12 June 2017 11:49

Call by value and call by reference in C

Written by
Rate this item
(0 votes)

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 and call by reference in c

Let's understand call by value and call by reference in c language one by one.


Call by value in C

In call by value, original value is not modified.

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().

Let's try to understand the concept of call by value in c language by the example given below:

 
  1. #include <stdio.h>  
  2. #include <conio.h>  
  3. void change(int num) {  
  4.     printf("Before adding value inside function num=%d \n",num);  
  5.     num=num+100;  
  6.     printf("After adding value inside function num=%d \n", num);  
  7. }  
  8.   
  9. int main() {  
  10.     int x=100;  
  11.     clrscr();  
  12.   
  13.     printf("Before function call x=%d \n", x);  
  14.     change(x);//passing value in function  
  15.     printf("After function call x=%d \n", x);  
  16.   
  17.     getch();  
  18.     return 0;  
  19. }  

Output

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

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.

Note: To understand the call by reference, you must have the basic knowledge of pointers.

Let's try to understand the concept of call by reference in c language by the example given below:

 
  1. #include <stdio.h>  
  2. #include <conio.h>  
  3. void change(int *num) {  
  4.     printf("Before adding value inside function num=%d \n",*num);  
  5.     (*num) += 100;  
  6.     printf("After adding value inside function num=%d \n", *num);  
  7. }  
  8.   
  9. int main() {  
  10.     int x=100;  
  11.     clrscr();  
  12.   
  13.     printf("Before function call x=%d \n", x);  
  14.     change(&x);//passing reference in function  
  15.     printf("After function call x=%d \n", x);  
  16.   
  17.     getch();  
  18.     return 0;  
  19. }  

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

No. Call by value Call by reference
1 A copy of value is passed to the function An address of value is passed to the function
2 Changes made inside the function is not reflected on other functions Changes made inside the function is reflected outside the function also
3 Actual and formal arguments will be created in different memory location Actual and formal arguments will be created in same memory location
Read 447 times
Anish Sir

"I am delighted once again to pen the welcome note to the Tosh!Yas Technologies ."

 Call +91 74 88 34 7779  | Email : anishsingh@live.com

toshiyas.in
More in this category: « functions in C Recursion in C »

Leave a comment

Make sure you enter all the required information, indicated by an asterisk (*). HTML code is not allowed.