Skip to main content

Call by Value and Call by Reference in C

2 min read Updated May 29, 2025
Share:
On this page (13sections)

Introduction

C passes function arguments by value — the called function receives a copy of each argument. To modify the caller’s variables from inside a function, you pass pointers (addresses). This pattern is often called call by reference in C textbooks, though technically C always passes the pointer by value.

Call by Value

Changes inside the function do not affect the original variable:

#include <stdio.h>

void increment(int n) {
    n = n + 1;
    printf("Inside function: %d\n", n);
}

int main(void) {
    int x = 10;
    increment(x);
    printf("In main: %d\n", x);
    return 0;
}

Sample Output

Inside function: 11
In main: 10

x in main remains 10 because only a copy was passed.

Call by Reference (Using Pointers)

Pass the address so the function can modify the original:

#include <stdio.h>

void increment(int *n) {
    *n = *n + 1;
    printf("Inside function: %d\n", *n);
}

int main(void) {
    int x = 10;
    increment(&x);
    printf("In main: %d\n", x);
    return 0;
}

Sample Output

Inside function: 11
In main: 11

Swap Example — Why Pointers Matter

#include <stdio.h>

void swap(int *a, int *b) {
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main(void) {
    int x = 5, y = 10;
    printf("Before: x=%d, y=%d\n", x, y);
    swap(&x, &y);
    printf("After:  x=%d, y=%d\n", x, y);
    return 0;
}

Sample Output

Before: x=5, y=10
After:  x=10, y=5

Without pointers, swap cannot change the caller’s variables. See the full Swap Using Pointers program.

Returning Values vs Using Pointers

ApproachWhen to use
Return valueSingle result (int square(int n))
Pointer parameterModify caller’s variable or return multiple results
Return pointerReturn dynamically allocated data (caller must free)

Arrays Are Passed by Reference Automatically

When you pass an array to a function, C passes a pointer to the first element — modifications inside the function affect the original array:

void doubleArray(int arr[], int size) {
    for (int i = 0; i < size; i++)
        arr[i] *= 2;
}

Best Practices

  • Use pointers when the function must modify caller data.
  • Mark read-only pointer parameters as const char *str when not modifying.
  • Document which parameters are input-only vs output.
  • Check for NULL pointers before dereferencing.

Common Mistakes

  • Forgetting & when passing a variable for modification.
  • Dereferencing an uninitialized pointer.
  • Returning address of a local variable (dangling pointer).

Continue learning with these related tutorials and programs:

Frequently Asked Questions

Does C support pass by reference?
C passes all arguments by value. You simulate pass-by-reference by passing a pointer — the pointer value is copied, but it points to the original variable.
Why does swap fail without pointers?
A swap function receives copies of the variables. Changing the copies does not affect the originals unless you pass addresses (pointers).

Related Tutorials

Search tutorials