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) Pointers and Memory Structs

Why declare variables c and r in main() ?

Hi,

I was wondering why we need to declare c and r as variables to then use our function using them as arguments. Why not simply use our function with the values we want to assign to c and r in them ? In other words, why :

float c[] = {1, 2, 3};
float r = 4;
Sphere ball = makeSphere(c, r);

and not :

Sphere ball = makeSphere([1, 2, 3], 4);

I tried that second one and it doesn't work. Is that because of the array ?

Thanks,

Antoine

1 Answer

Denis Kiselev
Denis Kiselev
12,737 Points

C language is quite old, and have no some syntax sugar, like more modern languages like Swift, C# and so on. Even Objective-C have more sugar than C. Why? C should be conservative to preserve huge codebase.

In C you can not use inline declaration of constant array like makeShere( {1,2,3}, 4);

But for second argument - "float r", it's completely ok to pass constant as second argument to makeSphere.

One note: not sure why arguments in function and variables in main are all have same names - its not required. All you need is to match type of passing arguments, not name.