#include /* Declaration of types of the arguments and the value returned by a function If function is placed after main, then it should be declared before main. */ double average(double a, double b); int main( void ) { double x,y,z, avg; printf("Enter 3 floating point numbers\n"); scanf("%lf %lf %lf", &x, &y, &z); /* We demonstrate how we can call an external function average(a,b). Since this function returns a value, the produced value can be stored in a variable, printed, used in an arithmetical expression or used as an argument. */ printf("Example 1: average of 10.0 and 20.0 = %lf\n", average(10.0, 20.0) ); /* The return value from average function is not captured anywhere. If we need it later in our program, we can keep it in a variable. In the following example, we call function average a few times, use return values to do some computation and save result in the variable avg. */ avg = (average(x,y) + average(y,z) + average(x,z))/3.0 ; printf("Example 2: avg = %lf\n", avg); printf("Example 3: compute average of x*x and y/2 = %lf\n", average(x*x,y/2.0) ); printf("Example 4: nested average = %lf\n", average(average(x,y), average(y,z)) ); return(0); } /* We can place function definition anywhere in the program if function was declared before main(). */ double average(double a, double b) { double result; /* variable result declared within this function is local to this function. It cannot be accessed or modifed by any other function */ result = (a + b)/ 2.0 ; return( result ); }