Question about "return"


#1

In this example the “return fahr” will be putting the value 32.0 in the “printf” of the main() function? printf(“Water freezes at %f degrees Fahrenheit\n”, freezeInF); %f = 32.0

float fahrenheitFromCelsius(float cel)
{
float fahr = cel * 1.8 + 32.0;
printf("%f Celsius is %f Fahrenheit\n", cel, fahr);

return fahr;

}

int main(int argc, const char * argv[])
{
float freezeInC = 0;
float freezeInF = fahrenheitFromCelsius(freezeInC);
printf(“Water freezes at %f degrees Fahrenheit\n”, freezeInF);

return 0;

}


#2

Some clever guy will answer this better tan me, but i think that´s right.
Maybe you’ll get it clearer if take a look to this thread:
viewtopic.php?f=137&t=4057

Noobs helps noobs! :smiley:


#3

Don’t forget about the clever girls :sunglasses:

What happens here is that first freezeInC gets filled with 0 (I would change that line into float freezeInC = 0.0 since it’s a float and not an int).

In the next line, the variable freezeInC is used as a parameter in the function fahrenheitFromCelsius, that function first calculates fahr (32.0), then prints the line “0.0 Celsius is 32.0 Fahrenheit” and finally returns fahr, which is 32.0. The returned value from fahr is stored in the float variable freezeInF.

In the 3rd line in main, there’s the printf, and the variable freezeInF is used there, not the fahr result from the function. Those values are the same, but there’s no direct link between fahr and the printf function in main. If you change the value of freezeInF to 50.0, that is the value that’s being used.


#4

Is there a typo here?

shouldn’t “cel” be replaced with “freezeInC”?

if not, where is “cel” getting its value at?

**Nevermind, think I understand now. I crammed so much info into my head in one sitting, I muddled myself with confusion. I see that the arguments (or values) get passed into the functions parameter, thus the parameter (whatever the variable name may be) takes on the value.