Pages

January 22, 2013

Function with parameter with return


syntax of function with parameters and return value

This function can also return a value from the called function to the calling function i.e. the output which is printed in the main function whenever the value will be returned to the main function it can be printed thereafter. Now the function type should not be void because function will return a value. Therefore, it’s overall type is decided by the type of value it returned (int, float etc.).


While the function is called in the main function then it would be assigned to a corresponding variable of same type and there after its value will be printed after the calling and this variable will contain the overall result of the calculation. It means there is no need to print the final result in the functional part. 


/*Program to enter values by the user and observe the function */

#include<stdio.h>
#include<conio.h>
int sum(int x,int y)
{
    int add;
    add=x+y;
    return add;
}
void main()
{
     int a,b,c;
     printf("\n enter any value of a and b");
     scanf("%d%d",&a,&b);
     c=sum(a,b);
     printf("\n addition of two numbers is %d",c);
     getch();
     } 
output of a program with parameter and return of arithmetic operation in C language

No comments:

Post a Comment