OnlineCBT Logo OnlineCBT
C Tutorial
C · LESSON 10

Call by Value vs Call by Reference

By Ratan Updated: 15 Oct 2026

Pass variables by value vs passing memory addresses using pointers (Call by Reference).

Level: Beginner to Advanced
Duration: ~60 Mins Deep-Dive

Important correction: C passes every argument by value

The chapter title uses the common phrase “call by reference”, but C has no reference-parameter mechanism like C++. Passing a pointer copies an address value into a parameter. The function can use that copied address to modify the caller's object. Understanding this distinction explains why one swap function fails and another works.

A swap that changes only local copies

void swap_values(int a, int b) {
    int temporary = a;
    a = b;
    b = temporary;
}

Calling this function with x and y initializes separate local objects a and b. Swapping those locals leaves x and y unchanged. A return value is one way to transfer a calculated result; pointers are useful when a function must update more than one existing object.

Working program with pointer parameters

#include 
void swap_objects(int *a, int *b) {
    int temporary = *a;
    *a = *b;
    *b = temporary;
}
int main(void) {
    int x = 10, y = 20;
    swap_objects(&x, &y);
    printf("x=%d y=%d\n", x, y);
    return 0;
}

Expected output: x=20 y=10. The caller passes addresses. Inside the function, *a designates the object at the address stored in a. The parameters themselves are still local copies of pointer values. This example's interface requires both pointers to refer to valid int objects.

Why assigning to a pointer is different

In a function taking int *p, *p = 7 changes the pointed-to int. By contrast, p = NULL changes only the local pointer parameter. If a function must change the caller's pointer itself, it needs an appropriate additional level of indirection or must return the replacement pointer.

Read-only input and aliasing

A parameter const int *p prevents modification of the int through p. It does not make the caller's object universally immutable. Two pointer arguments can refer to the same object; a function must either support that case or document why it is disallowed. The swap implementation above works when both arguments point to the same int: the value remains unchanged.

Practice and diagnosis

  1. Write void add_one(int *value) with ++*value. With x=5, calling add_one(&x) should leave x=6.
  2. Why does add_one(x) fail type checking? The interface needs an address, not an int value.
  3. May you pass NULL to swap_objects? No: dereferencing it violates the function's precondition.
  4. Draw boxes for x, y, a and b, then connect each pointer box to its target. This reveals which assignments change caller storage.

हिन्दी संकेत: C में address भी copy होकर जाता है। Pointer की value बदलना और उसके target की value बदलना अलग operations हैं।

📝 Live Lesson Practice

HTML/CSS JavaScript Python C++ C PHP
⌨️ Practice Inputs (लाइव इनपुट भरें) (खाली होने पर RED, भरने पर GREEN underline)
💻 Code Editor (Monaco VS Code Engine)
👀 Live Preview
Address Contact

+91 7877547686

E-mail

onlinecbtportal@gmail.com

Helpline Number

+91 7877547686


Click To Download
Get it on Google Play

ऐप डाउनलोड करने
के लिए Google Play पर
उपलब्ध है

WhatsApp Chat