# Dumbfounded On Challenge

#1

i was busy for a week at work with non programming related stuff, and i haven’t gone back to the book for a week.

and it was stuck because i didn’t know i can call float A and b in the function outside the main.

would u be able to call float A and float B on a different function per say? or would you have to declare them locally? since its float A and B are already in the main.

``````
// lets say this is the other function

float totalAngle (float angleA, float angleB, float angleC)
{
float total = angleA+angleB+angleC;

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

}

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 %.2f\n", angleC);
return 0;
}
``````

and also do i have to write float before each angle at the function? I’m pretty sure there is a different way, but I’m not sure.

#2

You’ve got an extra function in there at totalAngle().

Also remember that these are all local variables you are creating, so the angleB in each function is unique to that function unless you declare it as a global or static beforehand. All of this can get confusing really fast, so it’s best to use variable names that are unique and clear in their meanings.

Here was my solution to this challenge.

[code]#include <stdio.h>

float remainingAngle(float ang1, float ang2)
{
return 180 - (ang1 + ang2);
}

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 %.2f\n", angleC);
return 0;
``````

}[/code]

#3

floatA and floatB are both local variables inside of the scope of the main function. If you tried to reference them in another function, you will get an “undeclared identifier” error.

If you wanted to use floatA and floatB in a different function, you could either change their scope by declaring them outside of the main function, or you could pass them as an argument to the function.

#4

I was able to complete this challenge, but I have a question:

In the example above from macshome, how does the program understand ang1 and ang2?

How does it know the values of ang1 and ang2 if they aren’t define elsewhere in the code? Just trying to further my understanding.

Thank you for the wonderful book and this forum!

#5

Ahh. ang1 and ang2 are just variables inside that given function.

To anthropomorphize things a bit here the function says, “If you give me two floats I will give you one back.”. The rest of the program doesn’t care what the function calls the floats after it gives them to it, just that it gets a float back.

You could define the variables in the function as ‘cat’ and ‘dog’ and it wouldn’t matter to the execution of the program!

#6

You can think go the function parameters ang1 and ang2 as placeholders for the values you call the function with later on - it doesn’t matter how you name these placeholders you could also call them ernie and bert, but it wouldn’t make sense. Better use self-explanatory names. When you call this function in main() these placeholders/variables will be fill with the values of angleA and angleB, because the function is called like that:

You could also call the function like this:

The result will be the same. The point is, ang1 and ang2 are local variables only visible and known by the function - here remainingAngle.

cu
Vertex

#7

Hi everyone,

I thought I would also post my code as I came up with something slightly different. I think I should change the type of function, but I still get my desired result.

[code]#include <stdio.h>
int angleA;
int angleB;
void angleC(angleA, angleB){
int remainingAngle = 180 - angleA - angleB;
printf(“The first two angles are %i and %i.\n”, angleA, angleB);
printf(“The remaining angle is %i.\n”,remainingAngle);

}

int main(int argc, const char * argv[])
{
angleA = 35;
angleB = 56;
angleC(angleA,angleB);
return 0;

}
[/code]