Parameter Passing Techniques in C (original) (raw)
Last Updated : 13 May, 2025
In C, passing values to a function means providing data to the function when it is called so that the function can use or manipulate that data. Here:
- **Formal Parameters: Variables used in parameter list in a function declaration/definition as placeholders. Also called only parameters.
- **Actual Parameters: The actual expression or values passed in during a function call. Also called **arguments.
There are two main techniques for passing parameters to functions in C:
Table of Content
Pass By Value
In this method, a copy of the argument is passed to the function. The function works on this copy, so any changes made to the parameter inside the function do not affect the original argument. It is also known as **call by value.
**Example:
C `
#include <stdio.h>
// Function that takes parameters by value void func(int val) {
// Changing the value
val = 123;
}
int main() { int x = 1;
// Passing x by value to func()
func(x);
printf("%d", x);
return 0;
}
`
As we can see, the value of **x remains unchanged because the function **func() takes its parameter **val by value, meaning a **copy of x is passed to the function. When **val is modified inside **func(), it only alters the copy, not the original variable **x in **main(). As a result, printf("%d", x) outputs 1, the original value of x.
This method is simple to understand and implement but it is **inefficient for large arrays or structures because making copies of them is costly.
**Note: Languages like C, C++, and Java support this type of parameter passing. Java in fact is strictly call by value.
Pass by Pointers
This method uses a pointer as a parameter to receive the address of the data that is passed to the function in the function call as argument. This allows the function to access and modify the content at that particular memory location, hence, modifications done in the function are reflected in the original value. It is also known as **call by pointers.
**Example:
C `
#include <stdio.h>
// Function that takes parameters by pointer void func(int* val) {
// Changing the value
*val = 123;
}
int main() { int x = 1;
// Passing address of x
func(&x);
printf("%d", x);
return 0;
}
`
As we can see, the value of x changes because func() takes a pointer **int* val, which receives the **memory address of x. Inside **func(), ***val = 123 dereferences the pointer and modifies the value at that address, directly altering **x in **main(). Thus, **printf("%d", x) outputs **123.
This method is a bit complex and error prone as it involves pointers, but it is efficient when passing large arrays or structure. It also allows functions to modify the variable declared somewhere else.
**Note: This method is sometimes called as Pass by (or Call by) Reference. But it is not to be confused with Call by Reference from other programming languages like C++.
Call by Value vs Call by Reference
The following table lists primary differences between the call-by-value and call-by-reference methods of parameter passing in C:
Difference | Call-by-Value | Call-by-Reference |
---|---|---|
Definition | Passes a copy of the argument value to the function. | Passes the address (reference) of the argument. |
Effect on Original Argument | Original argument remains unchanged after the function call. | Original argument can be modified within the function. |
Memory Usage | Requires more memory as a copy of the argument is created. | More memory-efficient as only the address is passed. |