When calling a function in C, arguments can be passed in two ways, by calling by value and by calling by reference. This tutorial guides you about these two ways to pass parameters to a function and demonstrates them with examples.
Type | Description |
---|---|
Call by Value |
|
Call by Reference |
|
Call by Value
Example:
#include<stdio.h>
//function declaration
int addition(int num1, int num2);
int main()
{
//local variable definition
int answer;
int num1 = 10;
int num2 = 5;
//calling a function to get addition value
answer = addition(num1,num2);
printf("The addition of two numbers is: %d\n",answer);
return 0;
}
//function returning the addition of two numbers
int addition(int a,int b)
{
return a + b;
}
Program Output:
The addition of two numbers is: 15
Call by Reference
Example:
#include<stdio.h>
//function declaration
int addition(int *num1, int *num2);
int main()
{
//local variable definition
int answer;
int num1 = 10;
int num2 = 5;
//calling a function to get addition value
answer = addition(&num1,&num2);
printf("The addition of two numbers is: %d\n",answer);
return 0;
}
//function returning the addition of two numbers
int addition(int *a,int *b)
{
return *a + *b;
}
Program Output:
The addition of two numbers is: 15