Passing Arguments to Functions in C (By Value and By Reference)
In C programming, when you pass arguments to a function, there are two ways to do it:
-
- Pass by Value
- Pass by Reference
Each method has its own behavior, and understanding the difference is important for writing efficient and predictable code. Let’s dive into both of these concepts.
1. Passing Arguments by Value
When you pass by value, the function receives a copy of the argument. Changes made to the parameter inside the function do not affect the original variable outside the function.
How It Works:
- A copy of the variable is passed to the function.
- Any changes to this copy do not affect the original variable.
Example of Pass by Value:
#include <stdio.h>
// Function Declaration
void modify_value(int a);
int main() {
int x = 10;
printf("Before function call: x = %d\n", x); // x is 10
modify_value(x); // Pass x by value
printf("After function call: x = %d\n", x); // x is still 10 (no change)
return 0;
}
// Function Definition
void modify_value(int a) {
a = 20; // Changing the copy of x
printf("Inside function: a = %d\n", a); // a is changed, but x in main is not
}
Output:
Before function call: x = 10
Inside function: a = 20
After function call: x = 10
Explanation:
- The function
modify_value()
receives a copy ofx
. - Changing
a
inside the function does not affectx
in themain()
function because it only changes the local copy.
Key Points about Pass by Value:
- The original value of the variable is not modified.
- Used for simple operations where you don’t need to modify the original data.
2. Passing Arguments by Reference
When you pass by reference, the function receives the address of the argument, allowing it to modify the original variable directly. This is done using pointers.
How It Works:
- The address of the variable is passed to the function.
- Changes made to the parameter inside the function do affect the original variable.
Example of Pass by Reference:
#include <stdio.h>
// Function Declaration
void modify_value_by_reference(int *a);
int main() {
int x = 10;
printf("Before function call: x = %d\n", x); // x is 10
modify_value_by_reference(&x); // Pass x by reference (address of x)
printf("After function call: x = %d\n", x); // x is now 20 (changed)
return 0;
}
// Function Definition
void modify_value_by_reference(int *a) {
*a = 20; // Dereferencing the pointer to change the original value of x
printf("Inside function: a = %d\n", *a); // a (dereferenced) is now 20
}
Output:
Before function call: x = 10
Inside function: a = 20
After function call: x = 20
Explanation:
- The function
modify_value_by_reference()
takes a pointer tox
(int *a
). - Inside the function,
*a = 20
changes the value at the address ofx
, sox
is modified directly inmain()
.
Key Points about Pass by Reference:
- The original value of the variable is modified.
- Useful when you want to change the value of the variable passed to the function.
- Requires the use of pointers to pass addresses of variables.
Differences Between Pass by Value and Pass by Reference
Feature | Pass by Value | Pass by Reference |
---|---|---|
What is Passed | A copy of the value | The address of the variable |
Original Value Changed | No | Yes |
Use of Pointers | No | Yes |
Memory Usage | Requires more memory (for copies) | Requires less memory (no copies) |
Example | void func(int a) | void func(int *a) |
When to Use Pass by Value
- When you don’t want to modify the original variable.
- When the function doesn’t need to know or change the state of the variable in the calling function.
- For simple data types like
int
,char
,float
, where the overhead of copying is minimal.
When to Use Pass by Reference
- When you need to modify the original variable.
- When you want to avoid copying large data structures (like arrays or structs) and instead pass their reference (pointer).
- For returning multiple values from a function (since C only allows one return value, you can use pass by reference to “return” additional values).
Example: Pass by Value vs Pass by Reference with Arrays
In C, arrays are always passed by reference. Even if you don’t use pointers explicitly, passing an array to a function gives the function access to the original array.
Example: Passing Arrays by Reference:
#include <stdio.h>
// Function Declaration
void modify_array(int arr[], int size);
int main() {
int my_array[3] = {1, 2, 3};
printf("Before function call: ");
for (int i = 0; i < 3; i++) {
printf("%d ", my_array[i]);
}
modify_array(my_array, 3); // Pass array by reference
printf("\nAfter function call: ");
for (int i = 0; i < 3; i++) {
printf("%d ", my_array[i]);
}
return 0;
}
// Function Definition
void modify_array(int arr[], int size) {
for (int i = 0; i < size; i++) {
arr[i] = arr[i] * 2; // Modify the original array
}
}
Output:
Before function call: 1 2 3
After function call: 2 4 6
Explanation:
- Arrays are passed by reference in C, so any changes made to the array inside
modify_array()
will reflect in the original array inmain()
.
Summary
- Pass by Value: A copy of the variable is passed to the function. The original variable is not affected.
- Pass by Reference: The address of the variable is passed to the function, allowing the function to modify the original variable using pointers.
- Pass by Reference is especially useful for large data structures (arrays, structs) and when you need to modify the original data or return multiple values.
By understanding these concepts, you can choose the appropriate method to pass arguments to functions depending on your needs in C programming!