• A function call is very important in programming.
  • When we need to call a function, it is called inside a program.
  • A function which is being called by the parent functions is known as called functions
  • Function which is being called is known as calling function.
  • A function is called by the program whenever it is needed.
  • Functions are called by its name in the main program.
  • We can pass parameters to the functions that are being called in the main function.

Syntax

Add(a, b) // a and b are the parameters 

Calling a function in C program

Add.c

#include <stdio.h>  
int add(int a, int b);
void main()
{

int sum;
int a, b;
printf(" Enter the first and second number \n");
scanf("%d %d", &a, &b);
sum = add(a, b); // call add() function
printf( "The sum of the two number is %d", sum);
}
int add(int n1, int n2) // pass n1 and n2 parameter
{
int c;
c = n1 + n2;
return c;
}

Output

Call by value

  • When an single or multiple values of an argument are copied into formal parameters of a function, this type of functions is known as Call By Value.
  • It does not change the value of the function`s actual parameter.

Call by Value in C programming

Call_Value.c

#include <stdio.h>  
int main()
{
int x = 10, y = 20;
printf (" x = %d, y = %d from main before calling the function", x, y);
CallValue(x, y);
printf( "\n x = %d, y = %d from main after calling the function", x, y);
}
int CallValue( int x, int y)
{
x = x + 5;
y = y + 5;
printf (" \nx = %d, y = %d from modular function", x, y);
}

Output

Call By Reference

  • As name suggests call by reference is a method, where the address of the actual argument is copied into the function call’s formal parameter.
  • This type of method is known as Call by Reference.
  • If we make some changes in the formal parameters, it shows the effect in the value of the actual parameter.

Call by Reference in C programming

Call_Ref.c

#include <stdio.h>  
int main()
{
int x = 10, y = 20;
printf (" x = %d, y = %d from main before calling the function", x, y);
CallValue (&x, &y);
printf( "\n x = %d, y = %d from main after calling the function", x, y);
}
int CallRef( int *a, int *b)
{
*a = *a + 5;
*b = *b + 5;
printf (" \nx = %d, y = %d from modular function", *a, *b);
}

Output

Categorized in: