Challenge question


#1

I am also new to programming. I have to say that after reading various Objective C books I never understood fully why a value, other than void, is used at the beginning of a function or variable. Thanks to your explanation it finally makes sense.
Here is what I did for the challenge:
#include <stdio.h>

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

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;

}

Here’s my question - I declared the parameters angleA, angleB in the function remainingAngle to be of type float (I hope this is the correct way to say this). Since they are local variables within the function it is necessary to redeclare angleA & angleB inside main. Is this accurate? My confusion is caused by using the same names (angleA, angleB) in the function as well as main.

So, I altered the names of the parameters to:
#include <stdio.h>

float remainingAngle (float value1, value2)
{
float result = 180 - (value1 + value2);
return result;
}

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;

}

Both versions resulted in the correct answer, is the second method preferable to avoid confusion?
Thanks for taking the time to read this,
Bill


#2

I think that the answer to your question is all about the scoping of the variables here.

angleA, angleB, and angleC are just names that we use in the IDE so that it makes sense to us. As far as the compiler is concerned the name doesn’t matter as they are each local variables for each function.

In other words, each variable is contained inside its respective function. If you had declared a global or static for angleA and angleB then things could get messy quickly and encapsulation would be all kinds of busted as functions could get the values of seemingly internal data changed around without them knowing it.

From a style standpoint it makes good coding sense to have logical and separate names for things. Imagine you come back to code you wrote a year ago and it was full of variables like ‘angleA’ and ‘f’. For much MUCH more on this I don’t hesitate to recommend getting a copy of Code Complete.

Just for fun, here was my solution to the 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

Forgive me for jumping in here but I am new to C and Objective C and some of what I am seeing here is confusing. I suspect this is the case because of the way things I have learned in other languages. So please be patient with me. I was moving right along in the book until I got to this challenge. If angleA and angleB are already declared and the variable already set as 30 and 60 respectively then why does this need to be done again. My first considered solution was as simple as

float finalAnswer = 180-(angleA + angleB);
return finalAnswer

Why does float angleA = 30.0; not declare and set the variable?

Thanks :confused:


#4

[quote=“kbestle”]Forgive me for jumping in here but I am new to C and Objective C and some of what I am seeing here is confusing. I suspect this is the case because of the way things I have learned in other languages. So please be patient with me. I was moving right along in the book until I got to this challenge. If angleA and angleB are already declared and the variable already set as 30 and 60 respectively then why does this need to be done again. My first considered solution was as simple as

float finalAnswer = 180-(angleA + angleB);
return finalAnswer

Why does float angleA = 30.0; not declare and set the variable?

Thanks :confused:[/quote]

Oh it does set it, but this chapter is about then passing arguments into functions. Since the variables are scoped to only exist inside their own function you need to pass them in.

The function in this case is super simple, but the goal of the challenge isn’t to just compute Angle C, it’s to computer Angle C by passing values to a function.

That help some?


#5

OK, Now I understand that the point is to pass the variable.

I am still confused however.

//This code makes sense and results in an answer of 90.0. You are passing a known and defined variable. Where angleA=30 and angleB=60
float remainingAngle(float angleA, float angleB)

//This code also works, resulting in an answer of 90.0, but I don’t understand how. The value of the two variables has never been set.
float remainingAngle(float ang1, float ang2)

Help me understand why and how this works? :question:


#6

[quote=“kbestle”]OK, Now I understand that the point is to pass the variable.

I am still confused however.

//This code makes sense and results in an answer of 90.0. You are passing a known and defined variable. Where angleA=30 and angleB=60
float remainingAngle(float angleA, float angleB)

//This code also works, resulting in an answer of 90.0, but I don’t understand how. The value of the two variables has never been set.
float remainingAngle(float ang1, float ang2)

Help me understand why and how this works? :question:[/quote]

The second example works because you are setting the value of ang1 and ang2 when you pass them in. Those are variables that are local to the scope of the remainingAngle function. Angle1 and Angle2 are not global variables.

Think about it this way (Ignoring the way that variables are actually passed for simplicities sake.)…

Let’s say you have a box with 5 bolts in it. It’s your box and you have it with you. The box is your variable and it contains 5 bolts. (Think ang1)
Now let’s assume that I need some bolts for a project I’m doing for you. The project is the function.
When you bring me the bolts I can just tell you to put them in my bucket. The bucket is my variable and it contains the amount of bolts you put in it. (Think Angle1)

My project (function) doesn’t care that the bolts came from a box, it only knows to look in the bucket.
Your bolt box is all you care about and the fact that it holds your bolts.

All of this starts to make more sense when you get to objects. Data encapsulation like this is one of the fundamental tenants of OOP. When you pass data to another object, method, or function you don’t need --or even want-- to know what the target does with it as long as it gives back the result you are looking for.


#7

Wow! I think I got it.

//This function passes angleA & B which are defined.
float angleC = remainingAngle(angleA, angleB)

//This function calls the function “remainingAngle” which contains all the needed info.
float remainingAngle(float ang1, float ang2)
{
return 180 - (ang1 + ang2);
}
The two variables ang1 & 2 are just there to provide a place to pass the “box” to, from your example.

Do I get it?

Thanks for your patience. I could continue with more questions, but I am going to move on, hoping that this book does not teach in a linear format, but in the end it will all come together. :slight_smile:


#8

[quote=“kbestle”]Wow! I think I got it.

//This function passes angleA & B which are defined.
float angleC = remainingAngle(angleA, angleB)

//This function calls the function “remainingAngle” which contains all the needed info.
float remainingAngle(float ang1, float ang2)
{
return 180 - (ang1 + ang2);
}
The two variables ang1 & 2 are just there to provide a place to pass the “box” to, from your example.

Do I get it?

Thanks for your patience. I could continue with more questions, but I am going to move on, hoping that this book does not teach in a linear format, but in the end it will all come together. :slight_smile:[/quote]

You got it! ang1 and ang2 are local to the function and just give you a way to pass in a value to it. In a few chapters when you start making classes this will make more sense as then the functions will be in a separate file from main.m.

(Going a bit non-linear to the book for a moment here. :smiley: )
Remember that with object-oriented programming the object sending the message don’t want to know anything about how the receiver works. Something like this…

main.m: Oh man. I wonder what the third angle to this triangle is? Oh hey! That class over there has a method (a function) that can figure it out.
Triangle class: Yeah, just put your angles into ang1 and ang2 and I’ll give you back a float with the third angle.
main.m: OK take 30.0 for ang1 and 60.0 for ang2.
Triangle class: Thanks, here is the float 90.0

Here main.m doesn’t care at all about how the angle is computed. It just knows it will get a float back if it calls the method/function and gives it the two floats it requires for input.

So far this book has been great! I really like the short chapter lengths. Aaron has done a great job and this is the first time in several attempts to learn Obj-C (Or any compiled language) that I really feel like I’m getting it!


#9

What do you guys think?



#include <stdio.h>

float remainingAngel(angelA, angelB)
{
    float total = 180.00;
    float remaining = total - angelA - angelB;
    
    return remaining;
}

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

    float angelA = 30.0;
    float angelB = 60.0;
    
    float angelC = remainingAngel(angelA, angelB);
    printf("The third angel is %.2f", angelC);
    
    return 0;
}


#10

My solution … I think it is more ‘descriptive’ and natural.

#include <stdio.h>

// declare a static variable, accesible only in this file

static float totalAngle = 180.00;

// passing arguments into functions because the variables are only exist inside their own function

float remainingAngle(float angleParamPassIn1, float angleParamPassIn2)
{
float result = totalAngle - (angleParamPassIn1 +angleParamPassIn2);
return result;
}

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;
}


#11

Why do we have to put “%.2f” instead of “%f” ?

printf(“The third angle is %.2f\n”, angleC);


#12

Try both and see the difference.


#13

Thank you!