Function Arguments in C programming language

A function's arguments are used to receive the necessary values by the function call.

While calling a function, there are two ways in which arguments can be passed to a function −

Type Description
Call by Value This method copies the actual value of an argument into the formal parameter of the function. The actual parameters cannot be modified here.
Call by Reference This method copies the address of an argument into the formal parameter. Inside the function, the address is used to access the actual argument used in the call. This means that changes made to the parameter affect the argument. Address operator(&) is used in the parameter of the called function.


Example : ( Call by Value )
#include<stdio.h>
int addition(int num1, int num2); // function declaration
int main()
{
      int num1=3, num2=7, answer;
      answer = addition(num1,num2); // function call
      printf("The addition of two numbers is : %d\n",answer);
      return 0;
}
int addition(int a,int b)
{
      return a + b;
}

Output :
The addition of two numbers is : 10


Example : ( Call by Reference )
#include<stdio.h>
int addition(int *num1, int *num2); // function declaration
int main()
{
      int num1=3, num2=7, answer;
      answer = addition(&num1,&num2); // function call
      printf("The addition of two numbers is : %d\n",answer);
      return 0;
}
int addition(int *a,int *b)
{
      return *a + *b;
}

Output :
The addition of two numbers is : 10

0 Comments