Nearby lessons

48 of 72

C - Call by Reference

Call by Reference in C explains how pointers allow a function to modify the original variables declared in another function. This tutorial covers pointer basics, the & and * operators, swapping two numbers, examples, common mistakes, and exam-style MCQs.

Call by Reference

Meaning of Call by Reference

Definition: Call by reference is a method in which a function can access and modify the original variable through its reference or address.

In C, arguments are always passed by value. However, we can achieve call-by-reference behaviour by passing a variable's address to a function using a pointer.

The & operator obtains the address of a variable, and the * operator accesses the value stored at that address through a pointer.

How Does Call by Reference Work?

  1. Declare variables in the main() function.
  2. Pass their addresses to another function using the & operator.
  3. Receive those addresses using pointer parameters.
  4. Use the * operator to access or modify the original variables.
  5. Return to main() and observe the updated values.

Difference Between Call by Value and Call by Reference

Point Call by Value Call by Reference (Using Pointers in C)
What is passed? A copy of the value A copy of the pointer containing the address
Can the original variable be modified? Not through the copied parameter alone Yes, by dereferencing the pointer
Operators Normal variables & to obtain an address and * to access its value
Common use Calculations using copied values Swapping values and updating original variables

Example: Swapping Two Numbers

Swapping means exchanging the values of two variables. Here, a contains 10 and b contains 20. The swap() function exchanges their values using pointers.

Understanding the Code

  • int *x, int *y: These parameters are pointers that receive addresses.
  • swap(&a, &b): Passes the addresses of a and b.
  • int temp = *x: Saves the original value pointed to by x.
  • *x = *y: Copies the value pointed to by y into the original variable pointed to by x.
  • *y = temp: Places the saved value into the original variable pointed to by y.
Remember: C does not have true reference parameters like C++. In C, we achieve the same practical result by passing pointers. The pointer itself is passed by value, but it lets the function access and modify the original variable.
Example01
CCode Cell
1#include <stdio.h>
2 
3void swap(int *x, int *y)
4{
5 int temp = *x;
6 *x = *y;
7 *y = temp;
8}
9 
10int main()
11{
12 int a = 10, b = 20;
13 
14 printf("Before swap: a=%d b=%d\n", a, b);
15 
16 swap(&a, &b);
17 
18 printf("After swap : a=%d b=%d\n", a, b);
19 
20 return 0;
21}
Output
Before swap: a=10 b=20
After swap : a=20 b=10

Common Mistakes in Call by Reference

1. Forgetting the Address-of Operator

When calling the swap() function, pass the addresses of the variables using &.

swap(&a, &b);

Passing swap(a, b) would pass integer values instead of the addresses expected by the pointer parameters.

2. Forgetting the Dereference Operator

Inside the function, use *x and *y to access the original values. The variables x and y themselves hold addresses.

3. Using an Invalid Pointer

Never dereference an uninitialized pointer or a NULL pointer. Doing so causes undefined behaviour.

4. Forgetting to Return from main()

Use return 0; at the end of main() to indicate successful program execution.

Exam Tip: Remember the difference between swap(a, b) and swap(&a, &b). The second form passes addresses and is appropriate for the pointer-based swap() function shown in this tutorial.
📝 Key Takeaways
  • A pointer is a variable that stores a memory address.
  • The & operator obtains the address of a variable.
  • The * operator declares a pointer or accesses the value at an address.
  • C passes all function arguments by value.
  • Call by reference is commonly demonstrated in C by passing pointers.
  • A function can modify an original variable by dereferencing its pointer parameter.
  • The swap() function is a classic example of using pointers to modify original variables.
  • Initialize pointers properly and check for NULL before dereferencing pointers that may be NULL.

🧠 Test Your Knowledge

4 Questions
Progress: 0 / 4