Call by Value & Call by Reference
In C, when a function receives a value from the caller, it can receive it in two ways:
Call by Value and Call by Reference.
These two methods decide whether the function works on a copy of the data or the original data.
What is Call by Value?
In Call by Value, the actual value is copied into the functionβs parameter. Any changes made inside the function do not affect the original variable.
- Default method in C
- Function gets a separate copy
- Original data remains unchanged
- Safer but less efficient for large data
void change(int x){
x = x + 10;
}
Even if you modify x, the original value does not change.
What is Call by Reference?
In Call by Reference, the address of a variable is passed to the function. This allows the function to modify the original data.
- Function receives the address
- Changes reflect in the original variable
- Used in swapping, dynamic memory, arrays
void change(int *p){
*p = *p + 10;
}
Modifying *p affects the actual variable because pointer refers to original memory.
Key Differences
- Call by Value: Works on a copy
- Call by Reference: Works on original variable
- Changes inside the function affect original only when using pointers
- Call by Reference allows efficient handling of large data
Where Call by Value is Used?
- Simple calculations
- Printing values
- No modification needed
- Functions with constants
Where Call by Reference is Used?
- Swapping values
- Modifying arrays
- Updating strings
- Returning multiple values
- Dynamic memory concepts
Call by Value & Call by Reference β 15 Examples
1. Call by Value β Simple Change
#include <stdio.h>
void change(int x){
x = x + 10;
}
int main(){
int a = 5;
change(a);
printf("%d", a);
return 0;
}
2. Call by Reference β Modify Original
#include <stdio.h>
void update(int *p){
*p = *p + 10;
}
int main(){
int a = 5;
update(&a);
printf("%d", a);
return 0;
}
3. Call by Value β Swap (Fails)
#include <stdio.h>
void swap(int x, int y){
int t = x;
x = y;
y = t;
}
int main(){
int a = 10, b = 20;
swap(a, b);
printf("%d %d", a, b);
return 0;
}
4. Call by Reference β Swap (Works)
#include <stdio.h>
void swap(int *x, int *y){
int t = *x;
*x = *y;
*y = t;
}
int main(){
int a = 10, b = 20;
swap(&a, &b);
printf("%d %d", a, b);
return 0;
}
5. Modify Two Values Using Reference
#include <stdio.h>
void update(int *x, int *y){
*x += 5;
*y += 7;
}
int main(){
int a = 3, b = 4;
update(&a, &b);
printf("%d %d", a, b);
return 0;
}
6. Call by Value β No Effect
#include <stdio.h>
void modify(int n){
n = 100;
}
int main(){
int x = 20;
modify(x);
printf("%d", x);
return 0;
}
7. Reference to Change Array Elements
#include <stdio.h>
void change(int arr[]){
arr[0] = 99;
}
int main(){
int A[3] = {1,2,3};
change(A);
printf("%d", A[0]);
return 0;
}
8. Call by Reference β Increment
#include <stdio.h>
void increment(int *p){
(*p)++;
}
int main(){
int n = 10;
increment(&n);
printf("%d", n);
return 0;
}
9. Passing Address of Float
#include <stdio.h>
void change(float *p){
*p = *p * 2;
}
int main(){
float x = 4.5;
change(&x);
printf("%.2f", x);
return 0;
}
10. Reference β Update String First Character
#include <stdio.h>
void modify(char *s){
s[0] = 'Z';
}
int main(){
char name[] = "Sourav";
modify(name);
printf("%s", name);
return 0;
}
11. Value β Pass Structure by Value
#include <stdio.h>
struct Student { int roll; };
void set(struct Student s){
s.roll = 999;
}
int main(){
struct Student st = {10};
set(st);
printf("%d", st.roll);
return 0;
}
12. Reference β Pass Structure by Address
#include <stdio.h>
struct Student { int roll; };
void set(struct Student *s){
s->roll = 999;
}
int main(){
struct Student st = {10};
set(&st);
printf("%d", st.roll);
return 0;
}
13. Reference β Add Through Pointer
#include <stdio.h>
void add(int *x){
*x += 50;
}
int main(){
int a = 25;
add(&a);
printf("%d", a);
return 0;
}
14. Value β Local Copy Only
#include <stdio.h>
void display(int x){
x += 100;
printf("Inside: %d\n", x);
}
int main(){
int a = 10;
display(a);
printf("Outside: %d", a);
return 0;
}
15. Reference β Return Multiple Values
#include <stdio.h>
void results(int m1, int m2, int *sum, int *avg){
*sum = m1 + m2;
*avg = (m1 + m2) / 2;
}
int main(){
int sum, avg;
results(50, 70, &sum, &avg);
printf("%d %d", sum, avg);
return 0;
}
Real-World Analogy
- Call by Value: You receive a photocopy β changes affect copy only
- Call by Reference: You receive the original document β changes affect real file
Practice Questions
- Explain Call by Value with an example.
- What is Call by Reference? Why is it needed?
- Give differences between & and * in reference functions.
- Which method is used by default in C?
- Why is Call by Reference used in swapping?
Practice Task:
Create two functions:
1οΈβ£ One that tries to swap using Call by Value (fails)
2οΈβ£ One that swaps using Call by Reference (works)
Then compare the output.