![]() |
VOOZH | about |
C function can receive some values to work on from its caller. These values are called function parameters or arguments and the process of supplying these values is called passing parameter/arguments.
Function parameters must be specified in the function definition inside the parenthesis as shown:
Here, the variables declared inside () are placeholders. They are not the actual values but instead variables that will be assigned the value that is received. They are also called formal arguments or simply parameters.
After parameters are defined in the function definition, we have to pass the exact same number of values in the function call. The values passed here are called actual arguments or simply arguments.
The type and the sequence of the passed values should be same as in the function definition, otherwise, an error may occur.
10
Let's take another example in which we pass multiple values:
20 + 10 = 30
In C, there are 2 ways in which we can pass parameters:
In all the above examples, we have used pass by value method in which the copy of the value of the variable is created in the function. On the other hand, in pass by pointer method, we pass the address of the argument instead of the copy.
Example
20 + 99
As we can see, only the value of num2 is changed which was passed by pointer instead of normal pass by value. This concept requires the knowledge of pointers in C. Refer to this article to know more - Parameter Passing Techniques in C
Function parameters are created as soon as the function is called. Their lifetime and scope are as same as that of local variables defined inside functions. They are stored inside the function's call stack frame and removed when it is destroyed.
Output
main.c: In function βsumβ:
main.c:7:9: error: βaβ redeclared as different kind of symbol
7 | int a;
| ^
main.c:3:13: note: previous definition of βaβ with type βintβ
3 | int sum(int a, int b) {
| ~~~~^
main.c: In function βmainβ:
main.c:16:28: error: βaβ undeclared (first use in this function)
16 | printf("%d + %d = %d", a, b, res);
| ^
main.c:16:31: error: βbβ undeclared (first use in this function)
16 | printf("%d + %d = %d", a, b, res);
| ^
In the above code, two error occurs:
Function declaration is generally used when the function is called before its definition. In this case, it is optional to include function parameters.
The above statement tells the compiler about a function's name and return type. Mentioning parameters in the declaration is optional but valid.
This type of function declaration is known as a function prototype or function signature. If it appears before the function call, we can define the function anywhere in the program.
Note: It is compulsory to include parameters inside the function definition.