#include /* In this example, we do not declare function before we define it because function's definition appears before main() is defined. */ /* Note that a separate type should be specified for each parameter of the function, even if all parameters have the same type. */ double average(double a, double b) { double res; /* variable res declared within this function is local to this function. It cannot be accessed or modifed by any other function */ res = (a + b)/ 2.0 ; return( res ); } 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 can we 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(a,b) 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); }