Sending arguments to a function in C language
Variables declared in a function are allocated in memory stack and local to that function only. Variables declared in one function can’t be accessed from other function directly.
We can’t realize the purpose of functions unless there is communication among the functions in terms of data. In simple, a function must be capable to send the data to another function.
By using a technique called passing arguments or parameters, we can send data from one function to another.
A list of variables is specified with the function calling statement called actual arguments.
Similar list of variables in terms of number and type are specified with the called by function called formal arguments.
The values of actual arguments are assigned to the formal arguments in the same sequence.
I believe, the best way to learn is through an example.
Example:
#include<stdio.h> int main() { int x=10,y=20; display(x,y); /* sending arguments */ return 0; } display(int p,int q) /* receiving arguments */ { printf("%d",p); printf("\n%d",q); }
Output:
10
20
Example explained:
Here in this example, variables x ,y are the local variables to main(), these can’t be directly accessed from the sub function display().
To send the values of x, y into the sub function display(), x, y are written in the calling statement. The values of which are send and assigned to the variables p, q of function display(). This technique of sending data from one function to another is called passing arguments or parameters
x,y are the variables that hold actual values, so called actual arguments. The values of p, q are assigned by the actual arguments, so called formal arguments.
Different issues in sending arguments:
Pass by value:
Only the values of actual arguments are assigned to the formal arguments. The change in formal arguments doesn’t make any change in actual arguments because actual and formal arguments are different memory locations.
Example:
#include<stdio.h> int main() { int x=20,y=40; demo(x,y); /* sending arguments */ printf("x=%d",x); /* printing x, y after demo() execution */ printf("\ny=%d",y); return 0; } demo(int p,int q) { p=p+10; /* modifying formal arguments */ q=q+10; }
Output:
x=20
y=40
Example explained:
Here x,y are the variables belongs to main(), sending as arguments to the function demo(). The values of x, y were not changed though the formal arguments p, q are modified in the function demo() because actual and formal arguments are different with different memory locations.
Actual arguments can be constants
Example:
#include<stdio.h> int main() { display(10,20); return 0; } display(int p,int q) { printf("%d\n%d",p,q); }
Output:
10
20
Example explained:
Here the actual arguments are constants 10, 20. These are successfully assigned to the formal arguments p, q.
Actual arguments can be expressions
Example:
#include<stdio.h> int main() { int p=10,q=20,r=40,s=20; display(p+q,r-s); /* 30, 20 are send as arguments */ return 0; } display(int x,int y) { printf("%d\n%d",x,y); }
Output:
30
20
Example explained:
Here the actual arguments are expressions, the results of which 30, 20 are send as arguments and assigned to the formal arguments x, y.
Formal arguments can’t be constants and expressions
Actual arguments can be constants, variables and expressions because only their values are send as arguments to the function. Formal arguments must be variables because values can be assigned only to the variables but not to the constants and expressions.
Example:
#include<stdio.h> int main() { int x=20,y=40; display(x,y); return 0; } display(int p+10,int q-10) { printf("%d\n%d",p,q); }
Output:
Error
Example explained:
Here trying to assign actual arguments to the expression, resulted error.
Number, type of actual arguments must match with the number, type of formal arguments
Example:
#include<stdio.h> int main() { float x=25.25,y=56.89; display(x,y); return 0; } display(int p,char q) { printf("%d\n%c",p,q); }
Output:
error: conflicting types for ‘display’
display(int p,char q)
Example explained:
Here the sub function display accept two arguments that are int and char types but, trying to send two float type of arguments which resulted error.
Default formal arguments type
If formal arguments are not declared of any type then the compiler treats them as int type variables.
Example:
#include<stdio.h> int main() { int x=5,y=20; display(x,y); return 0; } display(p,q) /* Not declaring the formal arguments */ { printf("%d\n%d",p,q); }
Output:
5
20
Example explained:
Here formal arguments are not declared in the function display(). It must result error but, not happening so because compiler treats both p, q as int type arguments.