Created
January 12, 2017 16:43
-
-
Save s3f/f8b6e79aa1e3d43f20e6ae8a57288667 to your computer and use it in GitHub Desktop.
Chapter 32- Returning Data From Your Functions created by s3f - https://repl.it/FHcM/9
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Returning Values | |
The return statement terminates the execution of a function and returns control to the calling function usually main(). | |
The following program demonstrates functions returning a value by passing 3 floating point numbers and calculating the average and returns the answer. | |
*/ | |
#include <stdio.h> | |
float gradeAvg(float test1, float test2, float test3); | |
main() | |
{ | |
float grade1, grade2, grade3; | |
float average; | |
printf("What was the grade on the first exam? "); | |
scanf(" %f", &grade1); | |
printf("What was the grade on the second exam? "); | |
scanf(" %f", &grade2); | |
printf("What was the grade on the final exam? "); | |
scanf(" %f", &grade3); | |
// Now we pass the 3 grades to the function and return the average | |
average = gradeAvg(grade1, grade2, grade3); | |
printf("\nWith those three test scores, the average is %.2f", average); | |
return 0; | |
} | |
/* ******************************* */ | |
float | |
gradeAvg(float test1, float test2, float test3) | |
// Receives the values of the 3 grades | |
{ | |
float localAverage; | |
localAverage = (test1 + test2 + test3) / 3; | |
return (localAverage); // Returns the average to main | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment