# Challenge?

#21

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?

Thanks for any insight into this challenge.

—Evan

#22

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:

1. Define a variable named angleC of type float;
2. Call the function, passing it two arguments: the values of angleA and angleB;
3. 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:

``````float remainingAngle (float angle1, float angle2)
{
return 180 - (angle1 + angle2);
}``````
1. The function has two parameters: angle1 and angle2 - both of type float;
2. They will result in the creation of two local variables when the function begins execution;
3. Those variables receive their initial values from the arguments passed to the function;
4. They will be destroyed when the function returns.

#23

Thanks, ibex10.

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.

#24

Another solution:

main.c

[code]#include <stdio.h>

float remainingAngle(float angleA, float angleB)
{
return (180 - (angleA + angleB));
}

int main(int argc, const char * argv[])
{
float angleA = 30.0;
float angleB = 60.0;
float angleC = remainingAngle(angleA, angleB);
printf(“The third angle is %2.f\n”, angleC);
return 0;
}
[/code]
Console Log:
The third angle is 90
Program ended with exit code: 0

#25

My question is what is the purpose of the code line:

float angleC = remainingAngle (angleA, angleB)

Couldn’t you omit that line of code and just have the next one read:

printf(“the third angle is %.2f\n”, remainingAngle (angleA, AngleB)

When I made that change, the program continued to work, so what is the purpose of it?

#26

That statement is meant to illustrate that you can assign the value returned by a function to an appropriately-typed variable.

It is equivalent to these two statements: a declaration plus an assignment.

``````float angleC;
angleC = remainingAngle (angleA, angleB);``````

#27

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!

#28

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?

#29

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.

#30

You declare/define a function as returning a value of certain type when you want to say that the function returns a value of that type:

[size=150]ReturnValueType FunctionName ‘(’ OptParamList ‘)’[/size]

For example, you declare/define a function as returning void when you want to say that the function does not return a value:

``````// Declare
void printComplementOfAngle (float);
...

// Define
void printComplementOfAngle (float angle)
{
printf ("%f - %f = %f\n", 90.0f, angle, 90.0f - angle);
}``````

As another example, you declare/define a function as returning float when you want to say that the function returns a value of type float:

``````// Declare
float complementOfAngle (float);

// Define
float complementOfAngle (float angle)
{
return  90.0f - angle;
}``````

[Become a competent programmer faster than you can imagine: pretty-function.org]

#31

Thank you! That helps explain it better. It also helped to continue reading on in the book and see more examples. I appreciate the help!

You declare/define a function as returning a value of certain type when you want to say that the function returns a value of that type:

[size=150]ReturnValueType FunctionName ‘(’ OptParamList ‘)’[/size]

For example, you declare/define a function as returning void when you want to say that the function does not return a value:

``````// Declare
void printComplementOfAngle (float);
...

// Define
void printComplementOfAngle (float angle)
{
printf ("%f - %f = %f\n", 90.0f, angle, 90.0f - angle);
}``````

As another example, you declare/define a function as returning float when you want to say that the function returns a value of type float:

``````// Declare
float complementOfAngle (float);

// Define
float complementOfAngle (float angle)
{
return  90.0f - angle;
}``````

[Become a competent programmer faster than you can imagine: pretty-function.org][/quote]

#32

Code:

[code]// Chapter 5 - Challenge
// Triangle

#include <stdio.h>

float remainingAngle (float angleA, float angleB){

``````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]

#33

I know I’m like the first one to comment on this thread in two years, but in case anyone is out there, I did this with success!

#34

Better to declare the types of parameters explicitly, otherwise they will be typed as int.

``````float remainingAngle (float angleA, float angleB) {
...
}
``````

Also, you can copy and paste code; there is no need to paste an image.
Don’t forget to paste the code between three back ticks, like this ```…```:

``````printf ("%s\n", "Time flies like an arrow, Fruit flies like a banana.");
``````

#35

i did the challenge my code in :

``````#include <stdio.h>
#include <stdlib.h>

float remainingAngle(float A, float b){

float t = A + b ;

return t;
}
int main(int argc, const char * argv[]) {

float angleA = 30.0;
float angleB = 60.0;

float angleC = remainingAngle(angleA,angleB);

printf("The trird angle is %2.f \n", angleC);

return EXIT_SUCCESS;
}
``````

successful App

#36

Thank you for this great info. Look at these codes in my website.