In C programming, functions play a crucial role in structuring and organizing code. A key aspect of working with functions is understanding how parameters are passed between the caller and the callee. Parameter passing allows data to flow into or out of a function, facilitating modular and reusable code.
C provides multiple ways to pass parameters, each suited for specific scenarios. These include call by value, where a copy of the argument is passed, and call by reference, where the function operates on the actual memory address of the variable. Moreover, parameters can be categorized into formal parameters, defined within the function, and actual parameters, supplied by the caller during the function call.
There are different ways of passing parameters inside and outside the function
——————————————————————————————
#include <stdio.h>
int add(int a, int b) { // a and b are parameters //callee
return a + b; // Returns the sum
}
int main() {
int result = add(5, 3);
printf(“The sum is: %d\n”, result);
return 0;
}
In the above program a and b are passing the arguments inside the function
Result is the argument passing outside the funcitons,
Formal parameter are parameter passed inside the definition of the function.
Actual parameter are the arguments passed inside the function when it called by the caller
IN: Passes information from caller to the callee.
Out :passed informaiton from callee to caller
InOut:The caller tells the callee the value of the variable, which the callee may update.
// Function that modifies the value of the variable passed by the caller
void updateValue(int *x) {
*x = *x + 10; // Update the value at the memory location pointed to by x
}
int main() {
int num = 5;
printf(“Before update: %d\n”, num); // Output the value before modification
updateValue(&num);
printf(“After update: %d\n”, num); // Output the updated value of ‘num’
return 0;
}
In –passing the address from caller to callee
Out-by using pointers we are dereferencing and updating the value by callee.
Methods of Parameter Passing in C:
// C program to illustrate
// call by value
#include <stdio.h>
int add(int a, int b) { // a and b are parameters //callee
return a + b; // Returns the sum
}
int main() {
int result = add(5, 3);
printf(“The sum is: %d\n”, result);
return 0;
}
void updateValue(int *x) {
*x = *x + 10; // Update the value at the memory location pointed to by x
}
int main() {
int num = 5;
printf(“Before update: %d\n”, num); // Output the value before modification
updateValue(&num);
printf(“After update: %d\n”, num); // Output the updated value of ‘num’
return 0;
}
Indian Institute of Embedded Systems – IIES