To function's right (in parens)...what?why?


#1

What is the part to the right of a function in parenthesis? What does it actually mean/do? Why is it necessary?

Some examples from the book:
• void showCookTimeForTurkey (int pounds) …p. 34 makes perfect sense
• void singSongFor(int numberOfBottles) …p.36 now it references something established by coder
• float fahrenheitFromCelcius(float cel) …p.40 even though the return is “fahr”
• from CHALLENGE: float remainingAngle (angleA, angleB) …p42 even though that naming is used in int main

???


#2

[quote]What is the part to the right of a function in parenthesis? What does it actually mean/do? Why is it necessary?
[/quote]
That’s where you declare the parameters in the parameter list.

Every function has a return-value type, and a parameter list between a pair of parentheses.

Here is the syntax for function declaration:

ReturnValueType FunctionName (ParameterList)

For example, declare some functions:

void Foo ();   // No return value, no parameter list

unsigned long SumNumbersTo (const unsigned long);

unsigned long BiggerOf (const unsigned long, const unsigned long);

NSString * RandomStringWithMaxLength (const unsigned long);

Define some functions:

void Foo ()
{
   printf ("Hello, I am %s\n", __func__);
}

unsigned long SumNumbersTo (const unsigned long N)
{
   if (N == 0) {
        return 0;
   }
   else {
        return N + SumNumbersTo (N - 1);
   }
}

unsigned long BiggerOf (const unsigned long A, const unsigned long B)
{
   return A >= B ? A : B;
}

#3

Hmmm…I’m sure those are great examples, but I think I’m going to need an even simpler explanation than that. :blush:


#4

I’d like someone to explain in Lehman’s terms this exercise:

[code]#include <stdio.h>

float fahrenheitFromCelsius (float cel)

{
float fahr = cel * 1.8 + 32.0;
printf("%f Celcius is %f Fahrenheit\n", cel, fahr);
return fahr;
}

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

float freezeInC = 0;
float freezeInF = fahrenheitFromCelsius(freezeInC);
printf("Water freezes at %f degrees Fahrenheit.\n", freezeInF);

return 0;
}[/code]

specifically: float freezeInF = fahrenheitFromCelsius(freezeInC) - i get what’s going on, i just don’t get why. I understand here that we’re calling a function and in doing so we’re passing the value of freezeInC. It’s obvious to me the function is using the value of freezeInC, 0, and assigning that to “cel” - but how does that work? Is it because the parameters are in the same place?


#5

actually just seen very good explanation in the ‘return’ thread…