Parameters


#1

Hi everyone,
I’m new to the site and I recently purchased this book and I enjoy it very much. I’m having trouble understanding the parameters, like specifically on page 33 with the showCookTimeforTurkey function. What’s the point of the pounds in the parenthesis to the right of it, I just don’t understand it, as well as the continued functions of the showCookTimeForTurkey on pages 34-35. I was wondering if someone could give me a quick explanation about why the parameters are needed on page 36 and the function about the celcius and farenheit. I know I’m asking for a lot, but I just need help understanding why it is needed and what it does.


#2

A function is good for executing the same sequence of statements to perform some computation, whenever needed. A function can also return a value after it finishes its computation.

Some computations require input; a function can specify this input as part of its declaration:

// Multiply two numbers and return the result // double multiplyNumbers (const double A, const double B);
And must also specify it as part of its definition:

// Multiply two numbers and return the result
//
double multiplyNumbers (const double A, const double B)
{
   return A * B;
}

If a function does not require input and does not return a value, we write it like this:

void startOrbitting ();

Or like this:

void startOrbitting (void);

It is important to know that when writing a function declaration or definition there are syntactical rules that must be obeyed, just as there are syntactical rules that must be obeyed in human languages for uttering intelligible statements.

[Accelerate your learning and become a competent programmer: pretty-function.org]


#3

But I’m having trouble understanding parameters like on page 33. Can you explain what pounds does in the function

void showTimeForTurkey(int pounds)
{
     int necessaryMinutes = 15 + 15 * pounds;
     printf("Cook for %d minutes.\n", necessaryMinutes);
}

#4

[quote]void showTimeForTurkey (int pounds) { int necessaryMinutes = 15 + 15 * pounds; printf("Cook for %d minutes.\n", necessaryMinutes); }[/quote]
pounds is the name of the input you pass to the function. As you can see, the function is using it in its computation.

// Show cooking time for two pounds of turkey
showTimeForTurkey (2); // pass 2 as input - pounds will be 2

// Show cooking time for five pounds of turkey
showTimeForTurkey (5); // pass 5 as input - pounds will be 5

#5

Thank you so much, I get it now