So when I am doing the freeze water example everything made sense except when I created a global or static variable for lastTemperature other than the fact that when I gave it a value, it would make no difference in the results. So there is a point when I give the value of 50 to lastTemperature and then proceed to make it equal to cel.
The value of 50 *1.8 + 32 = 122 but I still get the value of 32 as if I am multiplying by 0. I realize that this is just a function to tell the difference between fahr and Celsius, however, within my function I tell it to print the value of fahr and cel in the printf line printf("%f Celcius is %f Fahrenheit\n", cel, fahr); as well as printf("%f\n",fahr); but I only get the value of 0 for cel and 32 for farh. Please explain why the global variable lastTemperature with a value of 50 won’t redefine what happens in my function?
#import <Foundation/Foundation.h>
#include<stdio.h>
#include<stdlib.h>
//Declare a golbal variable
//float lastTemperature;
//declare a global variable
float lastTemperature = 50;
float fahrenheitFromCelcius (float cel)
{
lastTemperature = cel;
float fahr = cel * 1.8 +32;
printf("%f Celcius is %f Fahrenheit\n", cel, fahr);
printf("%f\n",fahr);
return fahr;
}
int main(int argc, const char * argv[])
{
float freezeInC = 0;
float freezeInF = fahrenheitFromCelcius(freezeInC);
printf("Water freezes at %f degrees in Fahrenheit.\n", freezeInF);
printf("The last temperature converted was %f.\n", lastTemperature);
return 0;
}
MY RESULTS PRINT AS FOLLOWS:
0.000000 Celcius is 32.000000 Fahrenheit
32.000000
Water freezes at 32.000000 degrees in Fahrenheit.
The last temperature converted was 0.000000.
Program ended with exit code: 0
Please help me!