Skip to content
StudyFame

Call by Value vs Call by Reference

When you pass a variable to a function, does the function get the original variable or just a copy? The answer decides whether the function can change your variable.

In this lesson you will learn the two ways of passing arguments, call by value and call by reference, with the famous swap example that every exam and interview asks about.

What You Will Learn

  • What call by value means.
  • What call by reference means (using pointers).
  • Why a normal swap function doesn't work.
  • How to swap using pointers.
  • How to "return" two values from one function.

Call by Value vs Call by Reference Explained

Exam definition: In call by value, a copy of the argument's value is passed, so changes inside the function do not affect the original variable. In call by reference, the address of the variable is passed (using pointers), so the function can change the original variable.

Memory trick: Call by value = giving your friend a photocopy of your notes: they can scribble on it, your original stays clean. Call by reference = giving your friend your house address: they can go there and actually rearrange your furniture.

Pointers in one minute

  • &x = the address of variable x.
  • int *p = p is a pointer: a variable that stores an address of an int.
  • *p = the value at that address. Changing *p changes the original variable.

(You will study pointers in depth in Module 10. This is all you need for now.)

Syntax of Both Methods

/* call by value */
void change(int x)    { x = 100; }      /* changes only the copy */
change(a);

/* call by reference */
void change(int *x)   { *x = 100; }     /* changes the original */
change(&a);

Example 1: Swap with Call by Value (Doesn't Work) vs Call by Reference (Works)

#include <stdio.h>

void swap_value(int a, int b)        /* receives copies */
{
    int temp = a;
    a = b;
    b = temp;
}

void swap_reference(int *a, int *b)  /* receives addresses */
{
    int temp = *a;
    *a = *b;
    *b = temp;
}

int main(void)
{
    int x = 10, y = 20;

    swap_value(x, y);
    printf("After call by value    : x = %d, y = %d\n", x, y);

    swap_reference(&x, &y);
    printf("After call by reference: x = %d, y = %d\n", x, y);

    return 0;
}

Output

After call by value    : x = 10, y = 20
After call by reference: x = 20, y = 10

Code Explanation

  1. swap_value(x, y): copies of 10 and 20 go into a and b. The function swaps the copies. When it ends, the copies disappear. x and y in main are unchanged.
  2. swap_reference(&x, &y): the addresses of x and y are passed. Inside, *a means "the variable at address a", which is x itself.
  3. *a = *b; puts y's value into x, and *b = temp; puts the old x into y. The originals are really swapped.

Practise more with our swapping of two numbers in C and swapping using pointers programs.

How Arguments are Passed in Memory

  • Each function call gets its own space on the stack for its parameters.
  • By value: the parameter is a new variable holding a copy. It lives in the function's space and is destroyed when the function returns.
  • By reference: the parameter holds an address that points back into main()'s space, so writing through it reaches the original.
  • Technically, C always passes by value; "call by reference" in C means passing a pointer by value. The pointer is copied, but it still points to the original.

Example 2: Returning Two Results with Pointers

A function can return only one value. With call by reference, it can fill in several variables:

#include <stdio.h>

void sum_and_product(int a, int b, int *sum, int *product)
{
    *sum = a + b;
    *product = a * b;
}

int main(void)
{
    int s, p;

    sum_and_product(6, 7, &s, &p);
    printf("Sum = %d, Product = %d\n", s, p);

    return 0;
}

Output

Sum = 13, Product = 42

a and b are passed by value (we only read them). sum and product are passed by reference so the function can store results in s and p. This is exactly how scanf("%d", &n) works.

Difference Between Call by Value and Call by Reference

PointCall by valueCall by reference
What is passedA copy of the valueThe address of the variable
Original changed?NoYes
Parameter typeNormal variable (int a)Pointer (int *a)
Call written asf(x)f(&x)
SafetyOriginal is protectedOriginal can be changed by mistake
Used forInputs the function only readsChanging variables, returning many values, big data

Important Points to Remember

  • Call by value: a copy is passed; the original is not changed.
  • Call by reference: the address is passed; the original can be changed.
  • In C, call by reference is done with pointers (& in the call, * in the function).
  • A normal swap function fails with call by value.
  • Use pointers to "return" more than one value.

Common Mistakes Beginners Make

MistakeWhat happensFix
Expecting a by-value function to change the originalNo changePass the address and use pointers
swap_reference(x, y) without &Warning/crash: passing int as pointerswap_reference(&x, &y)
Writing a = b instead of *a = *bSwaps the pointer copies, not the valuesUse * to reach the values
Passing everything by referenceFunctions change data by mistakeUse by value for read-only inputs

Call by Value and Reference Practice Questions

  1. What is the difference between call by value and call by reference?
  2. Why doesn't a swap function work with call by value?
  3. Which operator gives the address of a variable?
  4. How can a function give back two results?
  5. Why does scanf need & before variables?
Show answers
  1. Call by value passes a copy (original unchanged); call by reference passes the address (original can change).
  2. It swaps only the copies inside the function; the copies are destroyed when the function ends.
  3. & (address-of operator).
  4. By taking pointer parameters and storing the results through them.
  5. Because scanf uses call by reference: it needs the address to store the input in the original variable.

Coding Exercise

Write a function void double_it(int *n) that doubles the variable passed to it. Call it on a variable with value 21 and print the result.

Show solution
#include <stdio.h>

void double_it(int *n)
{
    *n = *n * 2;
}

int main(void)
{
    int value = 21;
    double_it(&value);
    printf("value = %d\n", value);
    return 0;
}
value = 42

Quick Quiz: Call by Value vs Call by Reference (MCQ)

  1. In call by value, the function receives:

    A) The address   B) A copy of the value   C) The original variable   D) Nothing

  2. Call by reference in C is done using:

    A) Arrays only   B) Pointers   C) Macros   D) Loops

  3. After void f(int a) { a = 5; } and int x = 1; f(x);, x is:

    A) 1   B) 5   C) 0   D) Garbage

  4. After void f(int *a) { *a = 5; } and int x = 1; f(&x);, x is:

    A) 1   B) 5   C) 0   D) Address

  5. Which function uses call by reference?

    A) printf   B) scanf   C) puts   D) sqrt

Show answers
  1. B: a copy of the value.
  2. B: pointers.
  3. A: 1.
  4. B: 5.
  5. B: scanf.

Quick Revision for Exams and Interviews

Read this before your exam. The highlighted words are the answer.

Must-remember facts

Call by valueCopy passed; original unchanged
Call by referenceAddress passed; original can change
In C, reference viaPointers: f(&x) and int *p
Swap by valueDoesn't work
Swap by referenceWorks
Return many valuesPointer parameters
scanf usesCall by reference

Frequently asked questions

What is the difference between call by value and call by reference?
By value passes a copy (original safe); by reference passes the address (original can be changed).
Why doesn't swap work with call by value?
It swaps only the copies, which are destroyed when the function returns.
Does C really support call by reference?
C always passes by value; "by reference" means passing a pointer, which still points to the original.
How can a function return more than one value?
Pass pointers and store the results through them (or return a structure).

Remember

  • By value = photocopy; by reference = house address.
  • & in the call, * in the function.

Next Topic

Next lesson: Recursion in C. You will learn recursion: a function that calls itself.