Passing Function Arguments in C Programming

A function sometime needs data from calling function to perform it's task. It accepts data from calling function by declare variables that accept the values of the calling functions. These variables are called the formal parameters of the function.

In C, parameter(arguments) refers to data which is passed to function while calling function. The formal parameters are similar to local variables inside the function scope and are created when control enters into the function and gets destroyed upon exit.

A Function in C can be called in two ways.

  • Call by value

  • Call by reference
First of all we should understand the meaning of actual and formal parameters
  • Formal parameter : This is the argument which is used inside body of function definition. It's is limited to the scope of function. It gets created when control enters function and gets destroyed when control exits from function.

  • Actual parameter : This is the argument which is used while calling a function.

Call by value

In call by value method a copy of actual arguments is passed into formal arguments in the function definition. Any change in the formal parameters of the function have no effect on the value of actual argument. Call by value is the default method of passing parameters in C. Different memories are allocated for the formal and actual parameters.

C Program to Pass Arguments as Call by Value
#include<stdio.h>
#include<conio.h>
 
void getDoubleValue(int F){
   F = F*2;
   printf("F(Formal Parameter) = %d\n", F);
}

int main(){
   int A;
   printf("Enter a numbers\n");
   scanf("%d", &A);
   /* Calling function using call by value */
   getDoubleValue(A);
   /* Any change in the value of formal parameter(F)
   will not effect the value of actual parameter(A) */
   printf("A(Actual Parameter) = %d\n", A);
   
   getch();
   return 0;
}
Above program find sum of all integers from 1 to N, using while loop.

Output
Enter a numbers
5
F(Formal Parameter) = 10
A(Actual Parameter) = 5

Above program shows that, their is no change in the value of A(Actual parameter) even if the value of F(Formal parameter) changed inside function.

Call by reference

In call by reference method the address of the variable is passed to the formal arguments of a function. Any change in the formal parameters of the function will effect the value of actual argument. Same memory location is accessed by the formal and actual parameters.

C Program to Pass Arguments as Call by Reference
#include<stdio.h>
#include<conio.h>
 
void getDoubleValue(int *F){
   *F = *F + 2;
   printf("F(Formal Parameter) = %d\n", *F);
}

int main(){
   int A;
   printf("Enter a numbers\n");
   scanf("%d", &A);
   /* Calling function using call by reference */
   getDoubleValue(&A);
   /* Any change in the value of formal parameter(F)
   will effect the value of actual parameter(A) */
   printf("A(Actual Parameter) = %d\n", A);
   
   getch();
   return 0;
}
Above program find sum of all integers from 1 to N, using while loop.

Output
Enter a numbers
7
F(Formal Parameter) = 9
A(Actual Parameter) = 9

Above program shows that, If we call a function using call by reference then any change in the value of formal parameters will change the value of actual parameter. Both actual and formal parameters are pointing to same memory location.