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 trialJesse Vorvick
6,047 PointsQuestion about the parameter's name being repeated throughout its function
I was wondering this too. I get what you are saying about the parameter being a placeholder and that from the "perspective" of the computer the name of the parameter is unimportant. What I still don't quite understand is how this code runs properly even though the parameter name is repeated throughout the loop. For example in
function printSongs( songs ) {
var listHTML = '<ol>';
for ( var i = 0; i < songs.length; i += 1) {
listHTML += '<li>' + songs[i][0] + ' by ' + songs[i][1] + '</li>';
}
listHTML += '</ol>';
print(listHTML);
}
we have "songs.length," "songs[i][0]," etc. I would think at least these would need to say "playList.length" or "playList[i][0]," yet the code runs fine.
My question is, would it be accurate to say that when an argument is passed to a function, any time the parameter's name is repeated throughout the duration of the function, the computer treats it as though the parameter's name is that of the argument until the function ends?
2 Answers
Steven Parker
231,236 PointsYour "question" is spot on — it sounds like you understand the concept exactly.
And it's important that the parameter name is used consistently within the function and not the argument name, so that the function will perform correctly when it is called with a different argument.
Kate C
13,589 PointsIn this workspace, there is function print(message) and also printSongs (playList) , which seem doing the same thing? Plus, function printList(songs).....
I am still thinking why we need so many parameters.....If parameters are not that important since argument is doing the thing. Also, from previous workspace of the same playlist example, there is function print(html) {document.write(html);}
Steven Parker
231,236 PointsParameters are very important, and arguments cannot be used without them.