# Turkey Timer: Q Re 'turkeyWeight' and 'pounds'

#1

I’ve been struggling with understanding this for a while so I’m hoping someone can explain it. We have the following code…

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

void showCookTimeForTurkey(int pounds)
{
int necessaryMinutes = 15 + 15 * pounds;
printf(“Cook for %d minutes. \n”, necessaryMinutes);

}

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

``````int totalWeight = 10;
int gibletsWeight = 1;
int turkeyWeight = totalWeight - gibletsWeight;
showCookTimeForTurkey(turkeyWeight);

return 0;[/b][/color]
``````

I understand that the ‘main()’ actions first then the ‘showCookTimeForTurkey()’. At the end of ‘main ()’ we have ‘showCookTimeForTurkey(turkeyWeight);’ or I suppose you can also read this as ‘showCookTimeForTurkey(9);’

‘showCookTimeForTurkey()’ is called and the integer ‘9’ (i.e. turkeyWeight) is passed through. But in the function ‘showCookTimeForTurkey()’, we seem to be using the argument (??) ‘pounds’ instead of, what I would expect, ‘turkeyWeight’.

What is the relationship between ‘turkeyWeight’ and ‘pounds’?

Why not just replace ‘pounds’ with ‘turkeyWeight’ to keep it simple’?

I’m sure there’s an explanation so I really hope someone is able to explain it!

#2

There is not any relationship. However, pounds is a local variable that exists within the scope of the showCookTimeForTurkey() function, and turkeyWeight also is a local variable but it exists within the scope of the main() function. These two variables are totally independent of each other, even if they had the same name!

However, when the showCookTimeForTurkey() function is called with turkeyWeight as the argument, the value of turkeyWeight is copied into pounds so that the function can access the same value. (In Objective-C, arguments to functions are passed by value.)

You could do that but then you would lose the information about the unit of weight.

#3

Thanks Ibex10!