Call by Value and Call by Reference in C

Call by value and call by reference in C are two important ways of understanding how function arguments behave when data is passed to a function. This topic becomes very important once you begin writing functions that modify values, swap numbers, work with arrays, or update data outside the function body.

Many beginners think a function can directly change any variable passed to it, but that is not automatically true in C. By default, C uses call by value. To achieve reference-like behavior, pointers are used. Understanding this difference is essential for writing correct programs and avoiding confusion when values do not change as expected after a function call. In this article, we will understand call by value, call by reference behavior using pointers, key differences, examples, advantages, and common mistakes.

What is Call by Value in C?

Call by value in C means the function receives a copy of the actual argument. The function works on its own local copy, so changes made inside the function do not affect the original variable in the calling function.

void showValue(int x)
{
    x = x + 5;
}

Here, x is only a local copy. Even if the function changes x, the original variable used in the call remains unchanged.

In call by value, the function gets data, not direct access to the original variable.

How Call by Value Works in C

When a function is called using normal arguments, the values of the arguments are copied into the function parameters.

  1. The original variable stores some value in the caller.
  2. That value is copied into the function parameter.
  3. The function works on the copied parameter.
  4. When the function ends, the original variable remains unchanged.
#include <stdio.h>

void update(int x)
{
    x = 50;
    printf("Inside function: %d\n", x);
}

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

The output shows that the value changes inside the function, but the original variable a in main() remains 10.

What is Call by Reference in C?

Strictly speaking, C does not provide true call by reference the way some other languages do. However, C can achieve reference-like behavior by passing the address of a variable to a function using pointers. This is commonly called call by reference in C programming discussions.

When the address of a variable is passed, the function can access and modify the original variable through that address.

void update(int *x)
{
    *x = 50;
}

In this case, x points to the original variable, so changing *x changes the actual variable outside the function.

How Call by Reference Behavior Works in C

The idea is simple: instead of passing the value itself, the program passes the address of the variable.

  1. The caller passes the address of the variable using &.
  2. The function receives that address in a pointer parameter.
  3. The function uses dereferencing with * to access the original variable.
  4. Changes made through the pointer affect the actual variable.
#include <stdio.h>

void update(int *x)
{
    *x = 50;
    printf("Inside function: %d\n", *x);
}

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

This time, the value of a changes in main() because the function is working with the original variable through its address.

Difference Between Call by Value and Call by Reference in C

PointCall by valueCall by reference style
What is passedCopy of the valueAddress of the variable
Effect of modificationOriginal variable does not changeOriginal variable can change
Parameter typeNormal variablePointer parameter
Typical useSafe read or isolated computationWhen original data must be modified

This difference is one of the most important ideas in C function behavior.

Example of Call by Value in C

The following program shows call by value clearly.

#include <stdio.h>

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

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

Even though n becomes 6 inside the function, the original variable a in main() remains 5.

Example of Call by Reference in C

The following program uses a pointer to modify the original variable.

#include <stdio.h>

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

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

Here, both the function and main() observe the updated value because the original variable is being changed.

Swapping Two Numbers Using Call by Reference in C

Swapping two variables is a classic example where call by reference style is needed.

#include <stdio.h>

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

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

If swap were written using call by value, the original variables would not be exchanged in main().

Why C Uses Call by Value by Default

C uses call by value by default because it is simple and predictable. The function receives its own copy of the values, which prevents accidental modification of the caller’s data unless the programmer explicitly chooses pointer-based access.

This design gives the programmer control. If modification is needed, the programmer uses pointers intentionally.

Advantages of Call by Value in C

  • protects original data from unintended modification
  • simple to understand for basic computations
  • good for small values and read-only processing
  • reduces side effects in many cases

Advantages of Call by Reference Style in C

  • allows direct modification of original variables
  • useful for swapping and multiple output effects
  • avoids copying large data structures in some cases
  • essential for dynamic data manipulation and pointer-based programming

Common Mistakes in Call by Value and Call by Reference

  • expecting a call-by-value function to change the original variable
  • forgetting to use & while passing the address
  • forgetting to dereference with * inside the function
  • confusing pointer variables with normal variables
  • using incorrect pointer types in function parameters
MistakeProblemBetter approach
Passing a instead of &aFunction receives a value instead of an addressPass the address for reference-style behavior
Using x = 50 instead of *x = 50Pointer variable changes, not the original dataDereference correctly to modify the target
Assuming both methods behave the sameLeads to wrong expectations in outputTrace whether a value copy or address is being passed

Most mistakes in this topic happen because the programmer does not clearly track whether the function has a copy of the value or access to the original variable.

Best Practices for Using Call by Value and Call by Reference in C

  • Use call by value when the function should not modify the original variable.
  • Use pointer-based call by reference style when modification is required.
  • Choose meaningful parameter names for pointer arguments.
  • Be explicit with & and * so the code is easy to read.
  • Test function behavior with small examples when learning pointer-based updates.

A good rule is simple: if the function should only use data, pass the value. If the function should modify the caller’s data, pass the address.

FAQs

What is call by value in C?

Call by value in C means the function receives a copy of the argument, so changes inside the function do not affect the original variable.

Does C support true call by reference?

C does not support true call by reference directly, but it achieves reference-like behavior by passing addresses through pointers.

What is call by reference in C?

In common C usage, call by reference means passing the address of a variable to a function so the function can modify the original variable.

Why is swap usually done using call by reference style in C?

Because swapping must change the original variables, which is not possible with ordinary call by value.

What symbol is used to pass an address in C?

The & operator is used to pass the address of a variable.

What symbol is used to access the value through a pointer?

The * operator is used to dereference a pointer and access the value stored at the pointed address.