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.

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.

void change(int *p){
    *p = *p + 10;
}

Modifying *p affects the actual variable because pointer refers to original memory.

Key Differences

Where Call by Value is Used?

Where Call by Reference is Used?

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

Practice Questions

  1. Explain Call by Value with an example.
  2. What is Call by Reference? Why is it needed?
  3. Give differences between & and * in reference functions.
  4. Which method is used by default in C?
  5. 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.