Nearby lessons
48 of 72C - 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?
- Declare variables in the main() function.
- Pass their addresses to another function using the & operator.
- Receive those addresses using pointer parameters.
- Use the * operator to access or modify the original variables.
- 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.
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.
- 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.