#include<stdio.h>
main()
{
int y ;
y = mul (10, 5); / * function call * /
printf (“Multiplication is:%d \n”, y) ;
}
int mul(int x, int y)
{
int p ; / * local variables x=10, y=5 * /
p= x * y ;
return(p);
}
Multiplication is: 50
When the compiler encounters a function call the control is transferred to the function mul (), this function is then executed line by line as described and the value of p is returned, when the return statement is encountered. This value is assigned to y.
Parameters can also be used to send values to the calling programs. Parameter list contains declaration of variables separated by commas and surrounded by parenthesis.
A function need not always receive values form the calling program. In such cases, functions have no formal parameters. To indicate that the parameter list is empty we use the keyword void between parentheses as,
Syntax:
void printline(void)
{
………………
………………
}
This function neither receives any input values nor returns back any value.
Many compilers accept an empty set of parenthesis without specifying anything as void printline().
There are two ways in which we can pass arguments to the function :
• Call by value
• Call by reference.
In this type value of actual arguments are passed to the formal arguments and the operation is done on the formal arguments. Any change made in the formal arguments does not affect the actual arguments because formal arguments are photocopy of actual arguments. Hence when the function is called by the call by value method, it does not affect the actual contents of the actual arguments. Changes made in the formal arguments are local to the block of called function. Once control returns back to the calling function the changes made vanish.
In call by value, original value is not modified.
Example program to send values by call by value: In this program, the values of the variables “m” and “n” are passed to the function “swap”. These values are copied to formal parameters “a” and “b” in swap function and used.
#include<stdio.h>
void swap(int a, int b);
int main()
{
int m,n;
// calling swap function by value
printf(“Enter m & n values :”);
scanf(“%d%d”, &m, &n);
printf(“ values before swap m = %d \n and n = %d”, m, n);
swap(m, n);
}
void swap(int a, int b)
{
int tmp;
tmp = a;
a = b;
b = tmp;
printf(“ \n values after swap m = %d\n and n = %d”, a, b);
}
Enter m & n vales : 5 10
Values before swap m=5 and n=10
Values after swap m=10 and n=5
In this type instead of passing values, addresses are passed. Function operates on addresses rather than values. Here the formal arguments are pointers to the actual arguments. In this type, formal arguments point to the actual argument. Hence changes made in the arguments are permanent.
In call by reference, original value is modified because we pass reference (address).
#include<stdio.h>
void swap(int *a, int *b);
int main()
{
int m,n;
printf(“Enter m and n values:”);
scanf(“%d%d”, &m, &n);
printf(“values before swap m = %d \n and n = %d”, m, n);
swap(&m, &n);
}
void swap(int *a, int *b)
{
int tmp;
tmp = *a;
*a = *b;
*b = tmp;
printf(“\n values after swap a = %d \n and b = %d”, *a, *b);
}
Enter m and n values : 20 40
Values before swap m=20 and n=40
Values after swap a=40 and n=20
S.No | Call by value | Call by reference |
---|---|---|
1 | A copy of value is passed to the function | An address of value is passed to the function |
2 | Changes made inside the function is not reflected on other functions | Changes made inside the function is reflected outside the function also |
3 | Actual and formal arguments will be created in different memory location | Actual and formal arguments will be created in same memory location |