Header Ads Widget

Example of function in c.

 1) Program using function named  'sum' that add two input integer and return their sum. 

#include<stdio.h>

// Global declaration of sum function
int sum(int,int); 
int main()
{
     int a,b,c;
     printf("Enter the value of a and b");
     scanf("%d%d",&a,&b);
     c=sum(a,b);   // function calling 
     printf("sum of a and b is %d",c);
     return 0;
 
}
// End of main function 
// function definition of sum

int sum(int x,int y)
//where x and y are formal argument 
{
    int z;
    z=x+y;
    return z;
}

Output :--

Enter the value of a and b
10 5
sum of a and b is 15

2.  program using a function that  compare two input value 

#include<stdio.h>
int main()
{
    void  compare(int, int);//function declaration
    int a,b;
    printf("Enter the value of a and b ");
    scanf("%d%d",&a,&b);
    compare(a,b); // function calling
    return 0; 
}// end of main function

// function definition compare 
void compare(int x,int y)
{
    if(x<y)
    {
        printf("%d is greater than %d ",x,y);
    }
    else 
    {
        if(x==y)
        {
            printf("%d is equal to %d ",x,y);
        }
        else 
        {
          printf(" %d is smaller than %d",x,y); 
        }
    }
}




Recommended Post:

Key points:-

Cracking the coding interview:-

 Array and string:-

Tree and graph:-

Hackerearth Problems:-

Hackerrank Problems:-

Data structure:-

 MCQs:-