Answer in brief
C uses pass by value: each parameter receives a value supplied by the corresponding argument. An ordinary integer parameter is a separate object from the caller’s integer variable.
Working
- The caller evaluates the argument expression.
- The resulting value is used for the parameter according to the declared type.
- The function works with its parameter object. Assigning a new value to that parameter does not assign to the caller’s variable.
Example trace
main starts with number = 10 and calls change(number). The parameter n receives 10. Inside change, n = 25 changes only n, so the first line prints 25. After the function returns, number in main still contains 10. This shows the difference between a copied value and the original object.
Advantages
A function can use and update a working parameter without unintentionally replacing the caller’s ordinary value. This helps keep calculations separate. For example, a digit-processing function can repeatedly divide its local copy while the original input remains available to main.
Pointer clarification
Passing an address does not change C into pass by reference. The address is a pointer value, and that value is copied into a pointer parameter. The function can use the copied pointer to access the same caller-owned object. Changing the pointer parameter itself is different from changing the object to which it points.
Complexity
The fixed-size integer example uses constant work and a fixed number of objects, so time and extra space are O(1). The copying cost of an argument depends on its type; do not assume every possible argument is a single small integer.
C program and output
/* CodeBhavya | PPS AR25 | Learning example */
#include <stdio.h>
void change(int n)
{
n = 25;
printf("Inside = %d\n", n);
}
int main(void)
{
int number = 10;
change(number);
printf("Outside = %d\n", number);
return 0;
}
Sample input and output
This example uses values already written in the program.
Output
Inside = 25
Outside = 10