C Programming • Arrays & Functions
C Programming / Call by reference

Call by reference

Notes 3 Arrays & Functions

Definition: In C, the technique commonly called call by reference passes the address of a variable to a function using a pointer. The function can then access and modify the original variable through that address.

Notes

Definition: In C, the technique commonly called call by reference passes the address of a variable to a function using a pointer. The function can then access and modify the original variable through that address.

💡 Example: If the address of x is passed to a function, the function can use a pointer to change the actual value stored in x.

Why is Call by Reference Used?

Definition: Address-based parameter passing is used when a function needs to modify the original variable of the calling function or when working with large data without making a separate copy of the data.

💡 Example: A swap() function can exchange the values of two original variables by receiving their addresses.

Pointers and Addresses

Definition: A pointer is a variable that stores the memory address of another variable.

💡 Example: In int *p;, p is a pointer to an integer.

Address-of Operator (&)

Definition: The & operator returns the memory address of a variable.

💡 Example: &x means the address of variable x.

Dereference Operator (*)

Definition: The * operator is used with a pointer to access the value stored at the address held by that pointer.

💡 Example: If p stores the address of x, then *p accesses the value of x.

How Call by Reference Works

Original Variable ↓ Address of Variable ↓ Function receives Address ↓ Pointer Accesses Original Data ↓ Original Variable Can Change

Simple Example

The function receives the address of x and changes the value stored at that address.

#include <stdio.h>

void change(int *p)
{
    *p = 20;                // modify the original variable
}

int main()
{
    int x = 10;

    change(&x);             // pass address of x

    printf("x = %d
", x);

    return 0;
}

Output

x = 20
📌 Why did x change? The function received the address of x. Using *p, it modified the value stored at that address.

Call by Value vs Call by Reference

Call by Value Call by Reference (Address-Based)
Value is passed. Address is passed.
Function works with a copy. Function can access the original variable through a pointer.
Original variable normally remains unchanged by parameter assignment. Original variable can be modified.
Example: change(x) Example: change(&x)

Using Pointer Parameters

A pointer parameter is used when a function needs access to the caller's variable through its address.

💡 Example: void change(int *p) declares p as a pointer to an integer.

Practical Example — Swap Two Numbers

Problem Statement

Write a C program to swap two numbers using a function and pointers. The original values in main() should be changed.

Learning Outcomes

  • Understand address-based parameter passing.
  • Use pointers as function parameters.
  • Modify original variables from inside a function.

Hint

Pass the addresses of the two variables to swap(). Use pointer dereferencing and a temporary variable to exchange their values.

Theory

To modify the caller's variables, pass their addresses to the function. The pointer parameters can then access and update the original values.

Program

#include <stdio.h>

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

    temp = *a;              // store the value at address a
    *a = *b;                // copy value at b into a
    *b = temp;              // place original a value into b
}

int main()
{
    int x, y;

    printf("Enter two numbers: ");
    scanf("%d %d", &x, &y);

    printf("Before swap: x = %d, y = %d
", x, y);

    swap(&x, &y);         // pass addresses of x and y

    printf("After swap: x = %d, y = %d
", x, y);

    return 0;
}

Expected Output

Enter two numbers: 10 20
Before swap: x = 10, y = 20
After swap: x = 20, y = 10

Note

The function receives the addresses of x and y. Therefore, changes made through *a and *b affect the original variables.

Key Symbols to Remember

Symbol Meaning Example
& Address of a variable &x
* Pointer declaration / dereference int *p, *p

Important Points for Exam

  • C uses pointers and addresses for the technique commonly called call by reference.
  • The address of the variable is passed to the function.
  • A pointer parameter receives that address.
  • The dereference operator * accesses the original value.
  • Changes through the pointer can modify the caller's variable.
  • The & operator is used to obtain a variable's address.
🎯 Easy Rule:

&x → address of x
int *p → pointer that stores an address
*p → value at that address

Quick Revision

Concept Remember
Common name Call by reference / address-based passing.
What is passed? Address of the variable.
Used with Pointers.
Can original change? Yes, through the pointer.

Important Exam Questions

Short Answer Questions

  1. What is call by reference?
  2. How is call by reference achieved in C?
  3. What is a pointer parameter?
  4. What is the use of the & operator?
  5. What is the use of the * dereference operator?
  6. How can a function modify the original variable?

Long Answer Questions

  1. Explain call by reference in C with a suitable example.
  2. Explain how pointers are used to modify original variables through a function.
  3. Write a C program to swap two numbers using pointers.
  4. Differentiate between call by value and call by reference.
🎥 Recommended Learning

Watch a beginner-friendly explanation of pointers and call by reference in C.

▶ Watch: Call by Reference in C — Hindi

📝 Handwritten Notes

A short handwritten-style revision sheet for pointers and call by reference will be provided here.

🧠 Mind Map

Use the mind map for quick revision of address, pointer, dereference and original variable modification.