Part 2, C Function Arguments-1
Part 2, C Function Arguments-1
1
C Function Arguments
While calling a function, the arguments can be passed to a function in two ways:
Call by value
Call by reference
Call by Value
- The actual parameters are passed to the function.
- The actual parameters cannot be modified in the call function.
Call by Reference
- Instead of copying the actual parameters value, its address is passed to
function as parameters..
- Address operator(&) is used in the parameter of the called function.
- Changes in function parameter will change the value of the original variables.
2
Call by Value
Example:
#include<stdio.h>
int main()
{
//local variable definitions
int answer;
int x = 10;
int y = 5;
int z = 15;
answer = sum (x, y, z); //calling a function to get the sum of values
printf("The sum of three numbers is: %d\n", answer);
return 0;
} 3
//The function returning the addition of three numbers
Program Output:
The addition of three numbers is: 30
4
Call by Reference
Example:
#include<stdio.h>
int main()
{
//local variable definitions
int answer;
int x = 10;
int y = 5;
5
//function returning the addition of two numbers
int sum (int *a, int *b)
{
return *a + *b;
}
Program Output:
The addition of two numbers is: 15