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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting results from a function

Status
Not open for further replies.

gns100

Programmer
Aug 11, 2003
40
US
Simple question. I'm trying to return the result for calculation of "xout" from the function test() below. Instead of the result "xo = 4), I get xo is "undefined", I'm missing something basic here. Eventually, I want to return the results of many calculations from the function test().

Any help would be appreciated. Thanks.

Code:
<html>
<head>
<title>Test Function</title>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">

function test(xin, xout){
	alert("xin = " + xin);
	xout=xin*2;
}

var x=2;
var xo;

javascript:test(x, xo);

alert("x = " + x);
alert("xo = " + xo);
</SCRIPT>
</body>
</html>
 
It should be more like this:
Code:
<html>
<head>
<title>Test Function</title>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">

function test(xin){
    var xout;
    alert("xin = " + xin);
    xout=xin*2;
    return xout;
}

var x=2;
var xo;

xo=test(x);

alert("x = " + x);
alert("xo = " + xo);
</SCRIPT>
</body>
</html>

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Thanks Adam for the suggestion - I got it working. However, as I mentioned I want to transfer more than one variable. I have modified the example below to illustrate. I need both xo and yo.

Code:
<html>
<head>
<title>Test Function</title>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">

function test(xin, yin){
	var xout;
	var yout;
	alert("xin = " + xin);
	alert("yin = " + yin);
	xout=xin*2;
	yout=yin*4;
	return xout;
}

var x=2;
var y=3;
var xo;
var yo;

xo = test(x, y);

alert("x = " + x);
alert("y = " + y);
alert("xo = " + xo);
alert("yo = " + yo);
</SCRIPT>
</body>
</html>
 
Ok, I missed that part. "return" only returns one object so if you wanted to return more than one thing, you could return an array of objects, but since xo and yo are declared outside of a function, they're global. That means you can change them whenever you want. You don't need to return anything.
Code:
<html>
<head>
<title>Test Function</title>
</head>
<body>
<SCRIPT LANGUAGE="JavaScript">

function test(xin, yin){
    alert("xin = " + xin);
    alert("yin = " + yin);
    [b]xo[/b]=xin*2;
    [b]yo[/b]=yin*4;
}

var x=2;
var y=3;
var xo;
var yo;

test(x, y);

alert("x = " + x);
alert("y = " + y);
alert("xo = " + xo);
alert("yo = " + yo);
</SCRIPT>
</body>
</html>

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top