I was initially confused about this section: float angleC = remainingAngle(angleA, angleB);
From other examples in the chapter I knew that “remainingAngle” was supposed to be the function, but I couldn’t figure out what to do with “(angleA, angleB);”. Is it necessary to repeat both of those variables in the function so you can use them in the calculation? Does repeating them in the function make them global or just local variables?
Learning to read function-invocation statements and function declarations/definitions correctly is very important when learning to write code in any programming language.
The above statement, invoking a function, can be read as follows:
Define a variable named angleC of type float;
Call the function, passing it two arguments: the values of angleA and angleB;
Initialise the value of the variable with the value that the function returns.
Now go and look at the definition of the the function, which may look like this:
Your explanation helped me understand how to interpret what I was reading. It makes sense now. I’m going to re-read the chapter and make it more concrete.
Like a lot of the people here I got it to work by declaring four more floats (including the function name one) and then doing – return floatC = 180 - (floatA + floatB);
However, looking at some of the posts here, I wish that the authors had not used 30 and 60 as the original values as this seems to have caused a few people to just add these two together instead of thinking about it as a programming problem, where the original values could have been anything.
For those people, if you’re reading this, please try changing the original values and you’ll find your “program” no longer works!
i did the challenge without looking at the code in the book. i built this code from scratch and it was successful:
[code]#import <Foundation/Foundation.h>
//measure the third interior angle of a triangle #include <stdio.h> #include <stdlib.h>
void showRemainingAngle(int angle)
{
int thirdAngle = 180 - angle;
printf(“the third angle is %d.\n”, thirdAngle);
}
int main(int argc, const char * argv[])
{
int angleOne = 15;
int angleTwo = 70;
int subTotalAngle = angleOne + angleTwo;
showRemainingAngle(subTotalAngle);
}
[/code]
is there anything wrong with doing it this way?
I’m a little confused on when I should use “void” or “float”. I noticed on this challenge it could work either way. What is the main difference between using these two? I appreciate any help.
float angleC = 180 - (angleA + angleB); // calculates the missing angle
return angleC; // returns the value of angleC
}
int main(int argc, const char * argv[]) {
float angleA = 30.0;
float angleB = 60.0;
float angleC = remainingAngle(angleA, angleB); // The returned value goes here
printf("The third angle is %.2f\n", angleC);
return 0;
}[/code]
Console Output:
[quote]The third angle is 90.00
Program ended with exit code: 0[/quote]