Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Objective-C Basics (Retired) Functional Programming in C Functions

Rami Bakri
Rami Bakri
782 Points

How Xcode know which int variable is being referred to?

In this tutorial, we refer to 'int a' and 'int b'... in this case they are 'int foo' and 'int bar'.

I just want to double check my takeaway here - 'int a' is never explicitly referred to as being 'int foo'. Thus, does Xcode simply recognise the first declared int variable as int a?

I appreciate this is a super entry level question but I would just prefer to be 100% sure before moving on.

Code is below in case of need to reference.

Thanks in advance


int funky_math(int a, int b, int c);

int main() { int foo = 24; int bar = 35;

printf ("funky math %d\n", funky_math(foo, bar, check));
return 0;

}

int funky_math(int a, int b, int c) { return a + b + 343; }

2 Answers

When you declare the funky_math function before the main function, you set it to take three arguments - int a, int b and int c - in that order. Then, when you call this function inside the main function, you provide the function call with three arguments - foo, bar, check - in that order.

The variabes int a, int b and int c will be used as local variables inside the function (as seen after the main function, where you implement funky_math). When calling this function, Xcode will assign the foo variable to int a, the bar variable to int b and the check variable to int c because that's the order of the arguments you provided. If you had provided another order, Xcode would assign accordingly.

Therefore, when you call the function with the arguments foo, bar and check, Xcode knows that the value of int a will be the value that foo has at this point (same goes for the rest of the arguments).

Does this clear things up for you?

Rami Bakri
Rami Bakri
782 Points

That did clear everything up. Thanks Eirik, super useful explanation.

No problem, glad to help :)