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

How can the function make an alert with the variable pcheck when the global variable is named paycheck, I'm confused ?

6
Contributors
21
Replies
91
Views
3 Years
Discussion Span
Last Post by Troy III
0

You are passing by value to this function. So the value (1200) gets passed into the function, not the actual variable. The function does not perform any manipulations on the variable and, even if it did, the effects would not be visible outside the function.

0

All that is happening is the global variable value is being passed to the function, correct ?

The function check_alert has a parameter called (pcheck), it is a little confusing, but I'll get this figured out :)

0
function paper (log) {
    window.alert("you like "+log);
}
var arnold = going;
paper(arnold);

Shouldn't this make a window box called "you like going" ?

0

You are missing quotes.

 var arnold = "going";

It will display an alert message with the string of characters.

Edited by JorgeM

0

How can the function make an alert with the variable pcheck when the global variable is named paycheck, I'm confused ?

Well, argument names of the function would be useless if they wouldn't be able to receive value from other pointers or variables.
It works because you've just passed the value of 'paycheck' varible to the 'pcheck' argument name which will now pass that *value to the *function using it.

It's the same as if you've used a straitforward input to the named argument of the function, like:
check_alert(1200);

0

Troy could you explain that again ? :)

I just did! :)

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

// alert val: "You make $1200"

you have passed the paycheck value to the function argument named pcheck which will now carry this (1200) value to your alert message as it aleready did.
p.s.: You are not passing 'names' but 'values' of variables. The variable name is just a handle to its value. 'paycheck' and 'pcheck' are now handling the same value.

Edited by Troy III

0

In this case, 'paycheck' is indeed 'global' and can be acessed from inside a function. Try this:

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

Think about it...
Now try this

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

Ooops, someone is stealing from me!
To avoid the danger of a function corrupting the global variable, it's safer to use a different variable inside the function (pcheck in the original example) by passing it as an 'argument' which turns its value into a local variable within the function.

0

Hey thanks, I did more tests and I understand it better then I originally did, not completely, but better :)

nautical - what is this line 'paycheck = paycheck - 10;', a loop ?

0

It's there to demonstrate that if you use a variable defined outside the function instead of passing it in as a parameter, then your variable can be changed from inside the function. Many other languages would not be able to access the variable paycheck from inside the function. Compare with the following:

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

In this case pc = pc - 10 has no effect on the global variable so the alert keeps showing 1200.

I took a quick look for an article to help. Try this:
http://coding.smashingmagazine.com/2009/08/01/what-you-need-to-know-about-javascript-scope/

0

If the answer as to why you used pc = pc - 10 is on that page, you lost me, I couldn't find it.

0

Don't worry about it, read the article. In the last example, it was only there to show that changing pc inside the function has no effect on the value which is passed in the next time the function is used. Do you understand the answer to your original question now?

0

I understand my question. I also understand what you meant in your code, the only thing I don't get is the pc = pc -10 syntax, I won't fret over it, yet :)

0

Now I understand. In your first example when the function was being called it would subtract 10 from the global variable since the function was not calling any parameters.

I have to begin to take some of this knowledge and finish my site up, time is moving :(

Edited by Siberian

0

he was off the topic for being busy rediscovering the tap water, as if we never heard of the existing difference between global and local variables, since like forever.

but by "pc = pc - 10;" he meant: "pc -= 10;"! completely off topic of course.

0

No I didn't
I meant what I said. It exposed that Siberian doesn't (yet) understand the assign operator vs algebra

0

He probably didn't, but his question was on a separate subject: how does or can the argument variable named pcheck carry or receive the value of a variable named paycheck when a given function is alerting the value by using pcheck keyword, whereas in the function argument we are inserting a variable with a different name, namely paycheck.
The answer is that JavaScript variables are not carriers or presentations of an object being passed by but simple tokens pointing or referring to a given object; and that paycheck token is simply passing its reference of an existing object (same object: number literal 12000 ) to another token which will be, now also pointing to that same object.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.