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 trialJoseph Michelini
Python Development Techdegree Graduate 18,692 PointsWill I encounter a problem if a function parameter shares a name with a global variable?
In other words, does JS know if a function parameter is just a random placeholder word I made up, or if I'm passing it a variable declared somewhere else in the program? Does the syntax look exactly the same?
Thanks!
1 Answer
Steven Parker
231,261 PointsIf a parameter has the same name as a global variable, it is said to "shadow" the global. This means that within the function, that name will refer to the parameter and not the global variable. This won't make any technical difference to your code as long as you don't need access to the global from within the function.
It could, however, lead to some confusion for other developers reading the code.
Joseph Michelini
Python Development Techdegree Graduate 18,692 PointsJoseph Michelini
Python Development Techdegree Graduate 18,692 PointsThanks Steven! Just to be clear, if a global variable has been declared, and a function has been declared with a parameter that shares a name with the global variable, when I call the function and pass an argument, does the function pass my argument or the value of the global variable?
Steven Parker
231,261 PointsSteven Parker
231,261 PointsThe function receives your argument. That then "shadows" the global variable.
Joseph Michelini
Python Development Techdegree Graduate 18,692 PointsJoseph Michelini
Python Development Techdegree Graduate 18,692 PointsThank you Steven!