Example 2 : Function returning integer data type



Program : Function returning integer value.

#include<stdio.h>

int add(int,int);

int main()
 {
 int n1=10,n2=20,sum;

 sum = add(n1,n2);
 printf("\nAddition is : %d",sum);

 return(0);
 }

int add(int n1,int n2)
{
  return(n1+n2);
}

Explanation :


Prototype Declaration :

int add(int,int);

Integer Data Type -
[arrowlist]

  • Check return type in prototype declaration.
  • We have written type as integer.
  • So we need to assign function call to any integer variable.
[/arrowlist]
sum = add(n1,n2);
[arrowlist]
  • sum is integer variable .
  • add(n1,n2) will return integer value.
  • Result of the function is accessible inside the add() function only , but we want to use that result inside main.
  • So we are using assignment statement to store value returned by function.
[/arrowlist]