Hi there,
I am brand new in programming since one month, and after having tried two other books for real beginners, I am currently trying the Big Nerd Ranch Books.
Why, when we produce the following code, it works without having to “float” the angles A and B?
I see it works, but why do we have to “float” certain variables and other not?
[code]#include <stdio.h>
float remainingAngle(angleA, angleB)
{
float angleC = 180.0-(angleA + angleB);
return angleC;
} // then, you know the rest, it’s the content of the challenge already written[/code]
[quote]float remainingAngle (angleA, angleB)
{
float angleC = 180.0-(angleA + angleB);
return angleC;
}
[/quote]
Rather than giving you the answer, I’ll let you discover by experimenting with arguments you pass to the function.
Try the following code:
float otherAngle = remainingAngle (10, 30);
printf ("other angle = %f\n", otherAngle);
// what did you get for other angle?
otherAngle = remainingAngle (10.2, 30.3);
printf ("other angle = %f\n", otherAngle);
// what did you get for other angle?
What did you observe?
Now. put back the floats in and experiment again:
float remainingAngle (float angleA, float angleB)
{
float angleC = 180.0-(angleA + angleB);
return angleC;
}
What did you observe?
What can you conclude?
Thank you Sandro for you answer. Perhaps I didn’t express correctly my question. I don’t doubt of using variables instead of using numbers, because we can reuse them quickly and easily, and after that we just change them at the first place and all the other places are changed at the same time.
My question is why this code :
float remainingAngle (angleA, angleB)
{
float angleC = 180.0-(angleA + angleB);
return angleC;
}
is the same than this code :
float remainingAngle (float angleA, float angleB)
{
float angleC = 180.0-(angleA + angleB);
return angleC;
}
The only difference is the word “float” in front of the variables angleA and angleB when they appear for the first time.
Thank you for your updated answer.
They are not the same.
The first function:
[quote]
float remainingAngle (angleA, angleB)
{
float angleC = 180.0-(angleA + angleB);
return angleC;
}
[/quote]
is the same as this function:
float remainingAngle (int angleA, int angleB)
{
float angleC = 180.0-(angleA + angleB);
return angleC;
}
Compare the types of its parameters with those of the second function:
float remainingAngle (float angleA, float angleB)
{
float angleC = 180.0-(angleA + angleB);
return angleC;
}