Jump to content

Help understanding parameters


granGripau
 Share

Recommended Posts

I keep thinking I understand parameters, but then I'll go through a script and get confused atparameters changing names with variables and arguments etc.Can someone explain to me why/how parameters can just change names and still be the same, orhow the parameters and variables and arguments can somehow link together although there is no clear name ==. For example, this is perfectly clear to me:

function check_alert(pcheck,car) {window.alert("You make $"+pcheck+" and have a "+car);}check_alert(1200,"Corvette");

but I just can't wrap my head around:

function check_alert(pcheck) {window.alert("You make $"+pcheck);}var paycheck=1200;check_alert(paycheck);

I don't get how pcheck and paycheck relate here - or why js understands this withoutexplicitly linking the two.It's especially confusing if there are tons of other functions, it seems like everything would get mixed up.Can someone help me understand?Thanks!

Link to comment
Share on other sites

The name of the parameter specified in the parentheses is the name of a variable local to the function. It cannot be read outside the function, and when the function terminates, the variable is destroyed, and its memory is deallocated.When you pass data to a function in the form of a variable, the data gets copied from the original variable into the function parameter. (Not strictly true, but good enough for now.) The two variables are completely distinct. They can be named anything.The advantage here is that if you change the value of pcheck inside the function, the value of paycheck (outside the function) does not change.

Edited by Deirdre's Dad
Link to comment
Share on other sites

The names don't mean anything. Names are to help you understand the code, not the computer. The values are the important part. When you do this:var paycheck=1200;check_alert(paycheck);You're not sending the name "paycheck" to the function, you're sending the value 1200. The function never knows the name of the variable that held that value, if there even was one. It doesn't care about variable names, only values. That's why you can call a function just by passing values instead of variables:check_alert(1200,"Corvette");The only thing you're doing here:function check_alert(pcheck)Is telling the computer that inside the function you want to call the first value "pcheck". That doesn't create any sort of link, you're just saying that, when the function runs, inside of the function, you want to refer to the first value that was passed as "pcheck". Again, that's to help you, the computer doesn't care what it's called. In fact, it doesn't even need a name at all, it's possible to define a function like this:function test()where you don't list any parameters, but still access everything that was passed to it.

function test(){  for (var i = 0; i < arguments.length; i++)  {	alert(arguments[i]);  }}test('one', 'two', 'three');

That will show alert boxes for the three values, even though the test function does not have any parameter names defined. Again, the names are just for you, there's no relationship between whatever you decide to name the arguments that get passed to the function and the values which actually get sent.

Link to comment
Share on other sites

Ok, Thanks justsomeguy and Deirdre's Dad!!! :) The responses definitely helped alot, I now get the code I previously posted although it still seems like arbitrary names would cause confusion when dealing with more complex code. Especially if you're stepping in to someone elses code.I guess I'll have to cross that bridge when I get to it...Thanks again

Link to comment
Share on other sites

the names are arbitrary for a reason, and don't even need to be declared. As JSG was pointing out, the convenience is only there for us, and personally, makes a functions purpose a bit clearer from the outset by having incoming parameters defined in the constructor.Of course all of this hedges on the developer using clear, concise, and meaningful variable, parameter, etc names.

Edited by thescientist
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...