C Programming Absolute Beginner's Guide (3rd Edition)

(Romina) #1
#include <stdio.h>
float gradeAve(float test1, float test2, float test3);
main()
{
float grade1, grade2, grade3;
float average;
printf("What was the grade on the first test? ");
scanf(" %f", &grade1);
printf("What was the grade on the second test? ");
scanf(" %f", &grade2);
printf("What was the grade on the third test? ");
scanf(" %f", &grade3);
//Pass the three grades to the function and return the average
average = gradeAve(grade1, grade2, grade3);
printf("\nWith those three test scores, the average is %.2f",
average);
return 0;
}
/******************************************************************/
float gradeAve(float test1, float test2, float test3)
// Receives the values of three grades
{
float localAverage;
localAverage = (test1+test2+test3)/3;
return (localAverage); // Returns the average to main
}

Here is sample output from this program:


Click here to view code image


What was the grade on the first test? 95
What was the grade on the second test? 88
What was the grade on the third test? 91
With those three test scores, the average is 91.33.

Note

Notice that main() assigned the gradeAve() return value to average. main()
had to do something with the value that was returned from gradeAve().

You can put an expression after return as well as variables. This:

Free download pdf