BCA SCHOOL
Programming Tutorials • Notes • Practical • Books
LATEST TUTORIALS

Call by Value and Call by Reference in C: Swapping & Pointers | Unit 3 | BKNMU BCA

by BCA School



BKNMU Junagadh  |  BCA Sem 1  |  Unit 3

Call by Value vs Call by Reference in C Programming

Complete Guide to Parameter Passing Mechanisms, Memory Isolation, Pointer Referencing, and Swapping Logic

BKNMU Junagadh BCA Sem 1 Unit 3 Parameter Passing Pointers & Addresses Memory Management
In simple words: When calling a function in C, arguments can be supplied in two distinct ways: by sending a duplicate copy of the variable's value (Call by Value) or by passing the actual physical memory address of the variable using pointers (Call by Reference). This determines whether modifications inside the called function will alter the original data in main(), making it one of the most critical exam topics for BKNMU BCA Semester 1 (Unit 3).
📖 Core Architectural Pillars of Parameter Passing

The mechanics of argument evaluation are categorized into four structural components:

Call by Value

1. Value Copying

Passes a duplicate copy of data to formal parameters. Operations in the function do not affect original variables in caller scope.

Call by Reference

2. Address Passing

Passes memory addresses (&) to pointer formal parameters (*). Direct dereferencing permanently mutates original variables.

Memory Isolation

3. Stack Protection

Call by value allocates distinct, separate stack frames, preventing inadvertent side-effects and ensuring total data isolation.

Multi-Value Return

4. Pointer Side-Effects

Call by reference allows a sub-routine to effectively modify and return multiple variable values simultaneously without return statements.

1️⃣ Master Table: Call by Value vs. Call by Reference

A complete side-by-side comparison of operational behaviors, syntax differences, and memory effects:

Comparison Factor Call by Value Call by Reference
Argument Passed Passes actual value copies of variables (e.g. swap(a, b)). Passes memory addresses of variables (e.g. swap(&a, &b)).
Parameter Receiving Formal parameters are standard variables (e.g. int x). Formal parameters are pointer variables (e.g. int *x).
Original Data Impact Original variables in caller remain completely unmodified. Original variables in caller are directly altered via dereference.
Memory Allocation Allocates separate memory locations on stack for parameters. Shares same memory locations; pointers refer to original cells.
Safety & Isolation High data security; sub-functions cannot corrupt caller state. Lower isolation; unintended alterations mutate caller state.
Default Mechanism C language operates exclusively by value as default mechanism. Simulated in C using pointer addresses and dereferencing (*).
💻 Practical Code Examples: Swapping Demonstration

1. Call by Value (Original values will NOT change):

#include <stdio.h>

// Formal parameters x and y are independent copies
void swapByValue(int x, int y) {
    int temp = x;
    x = y;
    y = temp;
    printf("Inside swapByValue: x = %d, y = %d\n", x, y);
}

int main() {
    int a = 10, b = 20;

    printf("Before calling: a = %d, b = %d\n", a, b);
    swapByValue(a, b); // Copies of a and b are passed
    printf("After calling: a = %d, b = %d (NO CHANGE)\n", a, b);

    return 0;
}

2. Call by Reference (Original values WILL change):

#include <stdio.h>

// Formal parameters are pointers holding addresses
void swapByReference(int *x, int *y) {
    int temp = *x;
    *x = *y;   // Mutating value directly at address x
    *y = temp; // Mutating value directly at address y
}

int main() {
    int a = 10, b = 20;

    printf("Before calling: a = %d, b = %d\n", a, b);
    swapByReference(&a, &b); // Addresses of a and b are passed
    printf("After calling: a = %d, b = %d (SWAPPED)\n", a, b);

    return 0;
}
🔄 Step-by-Step Flow: Memory Execution Lifecycle
1

Step 1: Argument Evaluation

In Call by Value, the CPU evaluates variable contents (e.g. 10). In Call by Reference, the CPU evaluates pointer memory offsets (e.g. 0x7ffd).

2

Step 2: Stack Allocation & Assignment

A new stack frame is created. Value passing assigns numbers into fresh variables; reference passing binds pointers directly to caller addresses.

3

Step 3: Stack Teardown on Return

When the called function finishes, its stack frame is destroyed. Value copies vanish entirely, while reference mutations remain permanent in main().

💡 Critical Exam Insight: Pure C strictly supports only Call by Value natively. "Call by Reference" in C is simulated by passing the value of memory addresses (&) into pointer variables (*).
⚠️ Common Exam Mistake: Missing the Asterisk (*): When modifying reference parameters, forgetting the dereference operator (e.g., writing x = y; instead of *x = *y;) will only swap local pointer addresses inside the function without changing actual caller variable contents.
📝 Quick Revision — Key Exam Points
  • Call by Value: Passes value duplicate; caller variables remain unmodified.
  • Call by Reference: Passes memory address; caller variables are altered.
  • Address-of Operator (&): Used in the calling statement to pass the memory location.
  • Dereference Operator (*): Used in the called function header and body to access target values.
  • Use Case: Use Call by Value for read-only calculations; use Call by Reference when multiple variables must be modified.