Definition: Parameter passing is the process of providing data from the calling function to the parameters of the called function.
Definition: Parameter passing is the process of providing data from the calling function to the parameters of the called function.
add(x, y), the values of x and
y are passed to the corresponding parameters of
the add() function.
Functions often need data from the calling program to perform their task. Parameter passing provides that data to the function.
A function can receive one or more parameters and use them inside its body.
square(int n), n is the parameter that
receives the value supplied during the function call.
int square(int n)
{
return n * n;
}
In the following example, the values of x and
y are passed to a and b.
int add(int a, int b)
{
return a + b;
}
int result = add(x, y);
x → ay → b
In C programming, parameter passing is commonly discussed in terms of passing values and using addresses to allow a function to work with the caller's data.
| Method | What is Passed? | Effect on Original Variable |
|---|---|---|
| Call by Value | Value of the variable | Changes inside function do not change the original variable. |
| Address-based passing | Address of the variable | Function can modify the original variable. |
The direction of data flow depends on how the function is called and how its parameters are used.
A function may return a calculated value to the calling function. This allows the calling code to use the result after the function completes.
result = add(10, 20); stores the value returned by
add() in result.
Write a C program that accepts two numbers, passes them to a user-defined function, and displays their product.
Create a function multiply() with two integer parameters.
Pass the two input variables during the function call.
When a function is called, the arguments supplied by the caller are associated with the parameters defined by the called function. The function then uses those parameters to perform its task.
#include <stdio.h>
int multiply(int a, int b); // function declaration
int main()
{
int x, y, result;
printf("Enter two numbers: ");
scanf("%d %d", &x, &y);
result = multiply(x, y); // pass actual arguments
printf("Product = %d
", result);
return 0;
}
int multiply(int a, int b)
{
return a * b; // calculate and return product
}
Enter two numbers: 6 8 Product = 48
Here, x and y are the actual arguments,
while a and b are the formal parameters.
The function receives the data, performs the multiplication and
returns the result.
| Term | Remember |
|---|---|
| Parameter Passing | Providing data to a function. |
| Actual Argument | Value or variable supplied in function call. |
| Formal Parameter | Variable that receives the supplied data. |
| Return Value | Result sent back to the calling function. |
Watch a beginner-friendly explanation of parameter passing in C functions.
A short handwritten-style revision sheet for parameter passing will be provided here.
Use the mind map for quick revision of actual arguments, formal parameters and return values.