Challenge queries


#1

As a beginner to this, I’m having a few issues with this challenge. I really want to figure this out on my own as I need to fully understand this but I hope someone can clarify this. In the code there is the line…

float angleC = remainingAngle(angleA, angleB);

…what does this line mean? ‘angleC’ is equal to ‘remainingAngle’…what exactly is being stated within the brackets? Is it a formula? :confused:

Many thanks!


#2

No worries, I got it, heres my answer…

What I learnt is that the two ‘tokens’ in the brackets only relate to the main() code.

The fact that there are two tokens associated with remainingAngle is passed to the function where two tokens are also received but for the purpose of the function, these two tokens could be called something else.

I can imagine this is really useful when using one function for several pieces of code.

[color=#0000FF]#include <stdio.h>

float remainingAngle(angleA, angleB)
{
int angleC= 180 - angleA - angleB;
return angleC;
}

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;

}
[/color]


#3

As far as I can understand it (I’m only a beginner at programming), when main calls the function, it “supplies” the two arguments (tokens) in brackets around remainingAngle(angleA, angleB), which have already been declared/defined in main. In the function, these could and should have a different name to avoid confusion. I used angle1, angle2 and angle3.

#include <stdio.h>

float remainingAngle(angle1, angle2)
{
int angle3 = 180 - angle1 - angle2;
return angle3;
}

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;

}

In this instance the function is passed the values of angleA and angleB but it can be reused with different values. Even main.c could use it again. I think it is generally a good idea to use different names in the function otherwise it might be thought that the two share a value.

Hope this helps! It’s a bit late but I got stuck on it myself at first and I thought I’d try to share what I discovered.


#4

[quote]float remainingAngle (angle1, angle2) { int angle3 = 180 - angle1 - angle2; return angle3; } [/quote]
Needs some corrections:

float remainingAngle (float angle1, float angle2) { float angle3 = 180 - angle1 - angle2; return angle3; }
Always declare explicitly the types of the formal parameters of your function.


#5

[quote=“ibex10”][quote]float remainingAngle (angle1, angle2) { int angle3 = 180 - angle1 - angle2; return angle3; } [/quote]
Needs some corrections:

float remainingAngle (float angle1, float angle2) { float angle3 = 180 - angle1 - angle2; return angle3; }
Always declare explicitly the types of the formal parameters of your function.[/quote]
Ok I have fixed my own code.
Just wondering why this is necessary? Is it something to do with reusability? I would have thought that main would be able to supply the types as part of the arguments it passes to the function. Wouldn’t that make the function more reusable? Because then one would be able to use the function with an int or a long or a float etc.

Many thanks for the help.


#6

Nothing to do with reusability, but rather the rules of the language.

The following two definitions are the same:

int Foo (int A)
{
   return 5;
}
Foo (A)   
{
   return 5;
}

To see what this means, run the following programs and carefully inspect their output.

Using default param type and return-value type:

Foo (A)
{
    printf ("%s: A = %f\n", __PRETTY_FUNCTION__, (double)A);
    return 5.755;
}

int main (int argc, const char * argv[])
{
    double rv = Foo (2);
    printf ("%s: rv = %f\n", __PRETTY_FUNCTION__, rv);

    rv = Foo (2.723);
    printf ("%s: rv = %f\n", __PRETTY_FUNCTION__, rv);
    
    return 0;
}

Using explicit types:

double Foo (double A)
{
    printf ("%s: A = %f\n", __PRETTY_FUNCTION__, A);
    return 5.755;
}

int main (int argc, const char * argv[])
{
    double rv = Foo (2);
    printf ("%s: rv = %f\n", __PRETTY_FUNCTION__, rv);

    rv = Foo (2.723);
    printf ("%s: rv = %f\n", __PRETTY_FUNCTION__, rv);
    
    return 0;
}

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


#7

Ok I see the difference in the output but I’m a bit stuck on that code, firstly what is PRETTY_FUNCTION? I see it is the name of your website but what is it doing exactly? It seems to be a recital of the name of the function? I haven’t got that far in the book so I’m not sure if it’s is covered. Sorry if this is off topic…
Appreciate the help.