 # Page 40 - degrees - code question

Hello,

In the code for degrees on page 40, there is a float in main () called freezeInC. By some permutation, this float becomes float cel before main. I do not understand why? I rewrote the code. Here it is, and it successfully builds with the same output:

#include <stdio.h>

float fahrenheitFromCelsius(float freezeInC)

{
float fahr = freezeInC * 1.8 + 32;
printf("%f Celsius is %f fahrenheit.\n", freezeInC, 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;
}

Is this not more logical? How does the code know to replace float cel above main with float freezeInC? I suppose a bonus point question , might be, why does float fahr get returned and immediately mutated into float freezeInF?

Thanks in Advance,

Stefan

Here, I am assuming that the conversion function in the book was written like this:

``````float fahrenheitFromCelsius (float cel)
{
float fahr = cel * 1.8 + 32;
printf ("%f Celsius is %f fahrenheit.\n", cel, fahr);
return fahr;
}
``````

And you are wondering why it was not written like this:

``````float fahrenheitFromCelsius (float freezeInC)
{
float fahr = freezeInC * 1.8 + 32;
printf ("%f Celsius is %f fahrenheit.\n", freezeInC, fahr);
return fahr;
}
``````

No.

There is nothing wrong with the function you wrote; it converts the passed argument in degrees celsius to corresponding degrees in fahrenheit.
However, the parameter `freezeInC`, if literally taken, seems to be referring to the freezing point of some substance, which is confusing.

So better to stick with the code as written in the book.

Argument passing rule. The value of an argument you provide to the function at the call point is passed to the function under the name of the corresponding parameter.

For example, given:

``````void foo (float jibber, float jabber) {
printf ("jibber = %f jabber = %f\n", jibber, jabber);
}
``````
``````const float bar    = 3;
const float jibber = 9;
foo (bar, jibber);
``````

The code above will produce the output:

``````jibber = 3.000000 jabber = 9.000000
``````

If a function returns some value, you can capture that value by using it in an expression.

``````// capture returned value and print
float freezeInF = fahrenheitFromCelsius (0);
printf ("Water freezes at %f degrees fahrenheit.\n", freezeInF);

// capture returned value and print
printf ("Water boils at %f degrees fahrenheit.\n", fahrenheitFromCelsius (100));

// throw away returned value
fahrenheitFromCelsius (50);
``````

The argument passing rule is what I was looking for in the first question. There is no mention of it in the book which is supposed to be for people trying to learn. Thankfully we have this forum. So I do see how it passes the value of the argument I think. Because main () runs until it gets stuck at the ( ), I suppose this ques it to go looking above for a way to continue. It finds (float cel) in the first line and so passes the value found below at the sticking point to the corresponding set of first found ( ). As to the literal taking of freezeInC referring to the freezing point of some substance, that is in fact what it is referring to, and so above, where my version was float fahr = freezeInC * 1.8 + 32; it makes perfect sense, since the freezing point of water in Celsius is 0, and multiplied by 1.8 plus 32 is correct. I do not see how cel makes more or less sense than freezeInC, and I get the argument passing rule I think, but not changing the floats name without good cause I would think was good form, it being easier to see what is going on. I guess in summary, if freezeInC is a good enough name for the value down below, I do not see how shifting it up top makes it confusing, since it remains true. Looking at it more, I do see how in my form, you might expect fahr to be freezeInF, which then leads to a new question. If it is true that code is meant to take a larger problem and make it an easier problem by reducing the number of steps it takes to get from A to B, is this code example doing that?, and if not, but rather the code example is there as an exercise to help us apply rules and see how they work, then why are the rules being applied not mentioned?

1 Like

The Objective-C being a super set of the C Programming Language, the book quietly assumes that reader is already familiar with C.

Ask yourself this question: Can this function be used to convert other temperature values in C to values in F?

``````int main (int argc, const char * argv[]) {

// C to F conversion
float fahrenheitFromCelsius (float freezeInC);

// Coldest temp in June, it was 0 degree celcius
fahrenheitFromCelsius (0);

// Hottest temp in June
fahrenheitFromCelsius (17);

// Hottest temp in January
fahrenheitFromCelsius (49);

return 0;
}

float fahrenheitFromCelsius (float freezeInC)
{
float fahr = freezeInC * 1.8 + 32;
printf ("%f Celsius is %f fahrenheit.\n", freezeInC, fahr);
return fahr;
}
``````

The answer is yes. Therefore, it makes sense not to use freezeInC but to use a neutral and meaningful name such as cel or celcius. Using freezeInC gives the wrong impression that the function is used only for converting freezing point temperatures.

``````int main (int argc, const char * argv[]) {

// C to F conversion
float fahrenheitFromCelsius (float cel);

// Coldest temp in June, it was 0 degree celcius
fahrenheitFromCelsius (0);

// Hottest temp in June
fahrenheitFromCelsius (17);

// Hottest temp in January
fahrenheitFromCelsius (49);

return 0;
}

float fahrenheitFromCelsius (float cel)
{
float fahr = cel * 1.8 + 32;
printf ("%f Celsius is %f fahrenheit.\n", cel, fahr);
return fahr;
}
``````

The name of a function parameter can be any valid identifier, provided the name is not already taken in the same scope.

Thanks. Your pointing out that the scope of the code allows conversions of other Celsius values to Fahrenheit makes sense of the change. Still, the code is set forth as a means for determining freezeInC. Your explanation suggests a scenario in which the coder set out to achieve a narrow goal and realized that the code was more capable. Perhaps such is the case. If your observation makes clear the original goal, that of determining any Fahrenheit value from Celsius, my original question could be reversed and it could be suggested that freezeInC should have ben cel.

As to the book being written for programmers, I did not get that impression from the book description, or the reviews, or the fact that they start out in C so as to move on to Objective-C, their not wanting to assume you know C. Anyway, it is not too difficult, and the book coupled with your responses make it well worth going thru.

Thanks,

Stefan