Saturday, February 23, 2013

Prototype

As we all know, before using a variable we need to declare it. Similarly, before using a function we need to declare the function. This declaration of the function is called as function prototyping.
Consider the above program. There is one statement before the main() function.
The statement is: float fnGetAverage(int, int);
This statement is called function prototype. By doing this we are telling the compiler that we will use the function having following features:
1) The function name would be fnGetAverage
2) The function will return float value
3) The function will contain two int arguments.

If we do not wish to use function prototyping then we need to define the function before calling it. This can be done as follows:

float fnGetAverage(int a, int b){ //function definition
float c;
c = (a+b)/2; //calculating average
return c; //returning the value
}
void main(){
int x,y;
float z;
printf(“\nEnter the values for x and y”);
scanf(“%d %d”, &x, &y);
z = fnGetAverage(x, y); //function call
printf(“\nThe average is %d”, z);
}

No comments:

Post a Comment