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 trialAaron Batchelder
3,240 PointsFoo Bar vs. int a & int b
Why wouldn't we put int a and int in the place of foo and bar? Intuitively I would have expected to see them used in the body of the function.
2 Answers
Robert Bojor
Courses Plus Student 29,439 PointsHi Aaron,
You can do it if you want, use a and b instead of foo and bar but as time passes and you would come back to the code this will be misleading... You have to remember that variables inside a function are not shared within other functions, they are local only to that function, it's called namespace.
In the case of this video and its example, the int a and int b that are used as parameters of the funky_math function will only be available in that function and their values, even if changed, will not be reflected outside the function. Have a look at the example below:
int funky_math(int a, int b);
int main()
{
int a = 1;
int b = 1;
int lots = funky_math(a, b);
printf("a = %d \nb = %d \n", a, b);
printf("funky math %d\n", lots);
return 0;
}
int funky_math(int a, int b) {
a += 1;
b += 1;
return a + b;
}
// The output from running this would be
// a = 1
// b = 1
// funky math 4
As you can see, even with the values of a and b incremented by one inside the funky_math function, the original values from inside the main function are still kept.
In the beginning it might seem logical to use the same names for the variables but in the long run you will get used to longer and longer names to keep them unique.
Alex Cevallos
16,551 PointsHey Aaron, what specific time are you referring to in the video? I'll be happy to help
Aaron Batchelder
3,240 PointsAaron Batchelder
3,240 PointsThanks so much Robert, you're answer is fantastic! I actually didn't know about namespace. It makes a lot more sense to me now.