Jump to content
Larry Ullman's Book Forums

Function - By Value Or Reference !


Recommended Posts

A noob question i think i am goin to ask

 

Passing by value means that actual variable (in the function call) is not passed to the function but rather the variable` value is.

 

means in local scope it works but not outside of the local scope ??

I really didnt get it :S

Link to comment
Share on other sites

It's a little bit hard to understand the difference. I really like the way one of my teachers explained it.

 

When you assign something to a variable, a memory slot is reserved for the piece of information. Think of it as a drawer in a cabinet. Each drawer is labeled, and inside you find the data.

 

- When you pass by reference, you are sending the whole drawer and it's content. Any change to the drawer's contents will change it for anyone knowing the drawer. When you pass by reference, you are referencing the slot in memory.

- When you pass by value, you are taking out the content and sending it along. You basically clone or copy the information. If you change the content, the original will be preserved in memory. Only the copy is changed.

 

In terms of real data, if you pass an array by value, the array is copied. When you pass by reference, you are changing the original.

 

Scope is not really important here. The way the data is passed is what's important. JavaScript is a little strange in that way, so it's not a great language to understand the principle between the two. You can read a Stack Overflow about it here: http://stackoverflow.com/questions/518000/is-javascript-a-pass-by-reference-or-pass-by-value-language

Link to comment
Share on other sites

Edit: I responded the same time as Antonio, so sorry for any overlap in our answers.

 

Passing by value means that the value you pass to the function gets copied, and the copy of the value is assigned to the corresponding parameter in the function header.

This matters because any changes you make to that parameter will not affect the variable the value is assigned to outside the function.

 

If you pass by reference though (which you can't truly do in JS*), then any changes you make to the parameter within the function will be reflected in the variable that holds that value outside of the function.

 

*: In PHP, you can add & before a parameter to signify that the value should be passed by reference. You cannot do this in JS. You can however pass a value that is handled by reference behind the scenes, and achieve the same basic effect. For example:

function changeName(localObj) {
  localObj.name = 'Henry';
}

var obj = { name: 'Bill' };

changeName(obj);

console.log(obj.name); // Will echo 'Henry', not 'Bill'.
Link to comment
Share on other sites

Yes, a variable local to a function can never be used outside the function.

 

What we're talking about here though is passing an object to a function, changing the object in the function, and then seeing that the change is reflected in the object outside the function.

Link to comment
Share on other sites

 Share

×
×
  • Create New...