Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Variable Instances With Function

Status
Not open for further replies.

BDRichardson

Programmer
Jul 23, 2003
46
0
0
GB
Hi,

Please can anyone confirm as to whether when you pass a variable to a function as an argument, whether the variable becomes another instance of the variable?

For example, I declare a global string variable, and pass it to a function as an argument. I wish to change the value of the global variable. I find that I can change the value of the variable within the function, but the actual variable outside of the function does not change. It would seem that the variable becomes a local variable, which is a copy of the global variable.

Can anyone please make some sense of what is actually happening?
 
Variables that are strings, integers, etc are passed in by value - meaning they become local variables of the function. Objects are passed in by reference - meaning the local variable of the function is a pointer to the same object in memory. If you want to change a global variable that is not an object, I suggest using a return value in the function and reassign the global variable. For example:
Code:
function change(arg){
  arg += " World!";
  return arg;
}
var myGlobal = "Hello";

[b]myGlobal = change(myGlobal);[/b]

Adam
 
You are referring to passing variables "by reference" or "by value". The latter means you pass a reference to the variable object itself, so any changes happen outside the function. The former means you simply pass the value of the variable, so any changes are not affected outside the function.

So variable types (such as strings, booleans, and numbers) always pass "by value". Other objects, such as arrays, pass "by reference" - meaning any changes made will be reflected outside the function. Try this simple demo for more information:

Code:
<html>
<head>
	<script type="text/javascript">
		function changeString(someString) {
			someString = 'bibble';
		}

		var someGlobalString = 'wibble';
		alert(someGlobalString);			// should alert 'wibble';
		changeString(someGlobalString);
		alert(someGlobalString);			// should still alert 'wibble';

		function changeArray(someArray) {
			someArray = [1, 2, 3];
		}

		var someGlobalArray = [];
		alert(someGlobalArray.length);			// should alert 0;
		changeArray(someGlobalArray);
		alert(someGlobalArray.length);			// should still alert 0;

		function changeArrayElement(someArray) {
			someArray[0] = 123;
		}

		var someGlobalArray2 = [];
		alert(someGlobalArray2.length);			// should alert 0;
		changeArrayElement(someGlobalArray2);
		alert(someGlobalArray2.length);			// should alert 1;

	</script>
</head>
<body></body>
</html>

You will notice that the first array example sets the whole array inside the function to a new array, and so the global is unchanged... whereas the second is setting a single element of the array, and so it is changed.

Hope this helps,
Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

[tt]Dan's Page [blue]@[/blue] Code Couch
[/tt]
 
I thought as much. I am familiar with these principals from C#.

I would have used the return value from a function is there was only one value change, however in this circumstance, the same code needed to change more than one value.

I figured the best solution to my problem was to pass an object as an argument, and apply new custom attributes to the object. Effectively replacing the global variables with object attributes.

Many thanks guys for your assistance.

;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top