Call by Reference is mainly implemented using pointers in C. It is commonly used for swapping values, modifying arrays, reducing memory usage, and improving program efficiency.
Table of Contents
What is Call by Reference?
Call by Reference is a technique in which the address of variables is passed to a function instead of actual values. Since the function works with original memory locations, any modification inside the function changes the original variables.C language achieves Call by Reference through pointers.
Syntax:
return_type function_name(data_type *pointer_name)Example Syntax:
{
// code
}
void update(int *num)In the above example:
{
*num = 50;
}
- int *num is a pointer parameter.
- *num = 50 changes the original value stored at that address.
How Call by Reference Works?
The following steps explain how Call by Reference works:- A variable is created in the main function.
- The address of the variable is passed using the & operator.
- The function receives the address in a pointer variable.
- The dereference operator * is used to access and modify the original value.
- Changes made inside the function are reflected in the calling function.
Program to Demonstrate Call by Reference
Write a C program to change the value of a variable inside a function using Call by Reference.
// C program to implement call by reference
#include stdio.h
void changeValue(int *num)
{
*num = 100;
}
int main()
{
int number = 20;
printf("Before function call: %d\n", number);
changeValue(&number);
printf("After function call: %d", number);
return 0;
}
Output:
Explanation:Before function call: 20
After function call: 100
- Variable number initially stores value 20.
- &number sends the memory address of the variable to the function.
- Function parameter num stores that address.
- *num = 100 changes the value stored at that memory location.
- Since the original memory location is modified, the value of number becomes 100.
Program to Swap Two Numbers Using Call by Reference
Write a C program to swap two numbers using Call by Reference.
// C program to swap two numbers using
// call by reference
#include stdio.h
void swap(int *a, int *b)
{
int temp;
temp = *a;
*a = *b;
*b = temp;
}
int main()
{
int x = 10, y = 20;
printf("Before swapping\n");
printf("x = %d y = %d\n", x, y);
swap(&x, &y);
printf("After swapping\n");
printf("x = %d y = %d", x, y);
return 0;
}
Output:
Explanation:Before swapping
x = 10 y = 20
After swapping
x = 20 y = 10
- Variables x and y contain values 10 and 20.
- Their addresses are passed to the swap() function.
- Pointer variables a and b access original values using *a and *b.
- Values are exchanged using a temporary variable.
- Original variables are updated successfully.
Memory Representation of Call by Reference
1. Suppose variable number stores value 20.2. When changeValue(&number) is called:Variable Value Address
number 20 1000
3. After executing:num stores address 1000
*num accesses value at address 1000
4. Memory becomes:*num = 100;
This shows that the original value changes because the function works directly on the same memory location.Variable Value Address
number 100 1000
Difference Between Call by Value and Call by Reference
| Feature | Call by Value | Call by Reference |
|---|---|---|
| Meaning | A copy of the variable is passed to the function. | The address of the original variable is passed to the function. |
| Effect on Original Value | Changes inside the function do not affect the original variable. | Changes inside the function directly affect the original variable. |
| Memory Usage | More memory is used because duplicate copies are created. | Less memory is used because only addresses are passed. |
| Speed | Execution may be slower for large data because copying takes time. | Execution is faster because copying of data is avoided. |
| Use of Pointers | Pointers are not required. | Pointers are required. |
| Safety | Original data remains protected from accidental modification. | Original data can be modified directly. |
| Data Sharing | Function works on separate copies of data. | Function works on actual memory locations. |
| Multiple Value Modification | Multiple variables cannot be modified directly. | Multiple variables can be modified in a single function. |
| Suitable For | Small programs and simple calculations. | Large programs, arrays, structures, and swapping operations. |
| Example Function Call | function(a); | function(&a); |
Advantages of Call by Reference in C
- Original Values Can Be Modified: Functions can directly update actual variables without returning values.
- Reduces Memory Consumption: Only addresses are passed instead of copying complete data.
- Improves Program Efficiency: Programs become faster when working with arrays and structures.
- Allows Multiple Outputs: A single function can modify multiple variables.
- Useful for Dynamic Data Handling: Pointers make memory handling easier in advanced programming.
Limitations of Call by Reference
- Pointer Handling Can Be Complex: Beginners may find pointers difficult to understand.
- Risk of Unwanted Changes: Original data can accidentally change if pointers are misused.
- Debugging Becomes Difficult: Pointer-related errors are sometimes hard to trace.
- Invalid Memory Access Can Occur: Wrong pointer usage may lead to crashes or undefined behavior.
Common Mistakes in Call by Reference
1. Forgetting the Address Operator: The function requires an address, not a normal value.2. Missing Pointer Symbol in Function Parameter: Pointer parameters are required to receive addresses.Incorrect:
changeValue(number);
Correct:
changeValue(&number);
3. Forgetting Dereference Operator: * accesses the value stored at the address.Incorrect:
void changeValue(int num)
Correct:
void changeValue(int *num)
4. Using Uninitialized Pointers: Incorrect pointers may access random memory locations and crash the program.Incorrect:
num = 50;
Correct:
*num = 50;
5. Passing Wrong Pointer Types: Pointer data types should match variable data types.
Example:
This creates type mismatch problems.int num = 10;
float *ptr;
Conclusion
Call by Reference in C is an important concept that allows functions to work directly with original variables using pointers. It improves efficiency, reduces memory usage, and makes complex operations easier. Understanding Call by Reference is essential for mastering pointers and advanced programming concepts in C language.Frequently Asked Questions
1. What is Call by Reference in C?2. Does C support Call by Reference directly?Call by Reference is a method where memory addresses are passed to functions so original values can be modified.
3. Why is the & operator used?No, C uses pointers to implement Call by Reference.
4. Why is the * operator important?The & operator is used to send the address of a variable.
5. What are the advantages of Call by Reference?The * operator accesses or modifies the value stored at a memory address.
It reduces memory usage, improves efficiency, and allows modification of original variables.
0 Comments