...

View Full Version : Javascript Function problem



savin
01-28-2006, 06:31 PM
I am running into a small problem with javascript functions. I'm obviously doing something stupid probably because of my background in other programming languages.

My problem is the function does not seem to be returning the values. I have recreated the problem in a very small page. I have 5 global variables x1, x2, x3, x4 and x5 and three local variables in the function g1, g2, g3, g4 and g5 and g4 and g5 don't seem to be returned since x4 and x5 remain unchanged.
***************

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script type="text/javascript">

var x1 = 5;
var x2 = 6;
var x3 = 7;
var x4 = 8;
var x5 = 9;


function do_calcs() {
CalcNorms(x1, x2, x3, x4, x5);
}


function CalcNorms(g1, g2, g3, g4, g5) {

alert("passed g4 and g5 values = " + g4 + " and " + g5);
g4 = g1 * g2 * g3;
g5 = g1 + g2 + g3;
alert("calculated g4 and g5 values = " + g4 +" and " + g5);
}


</script>
</head>

<body>

<script language=Javascript type="Text/Javascript">
do_calcs();
alert("returned x4 and x5 values = " + x4 +" and " + x5);
</script>

</body>
</html>

What am I doing wrong?

Any help would be much appreciated.

fci
01-28-2006, 07:45 PM
well, you're not actually returning anything and you're passing the global variables as arguments which makes them 'local' variables.

anyway, here is a modified version using arrays:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
<title>Untitled</title>
<script type="text/javascript">

var x = [ 5, 6, 7, 8, 9 ];


function do_calcs(args) {
return CalcNorms(args);
}


function CalcNorms(args) {
alert("returned g4 and g5 values = " + args[3] +" and " + args[4]);
args[3] = args[0] * args[1] * args[2];
args[4] = args[0] + args[1] + args[2];
alert("returned g4 and g5 values = " + args[3] +" and " + args[4]);
return args;
}


</script>
</head>

<body>
<script type="Text/Javascript">
x = do_calcs(x);
alert("returned x4 and x5 values = " + x[3] +" and " + x[4]);
</script>
</body>
</html>

savin
01-28-2006, 08:12 PM
fci,

Thanks. I have tried what you suggested and it works. But I still don't quite understand where mine is failing.

If I understand correctly nothing passed back from a function unless a Return statement is added. Is this correct?

How would this work if I did not use an array?

Lets say I had


<script>
var global_x = 6;
var global_y = 10;
function my_main_function () {
my_sub_function (global_x, global_y);
}
function my_sub_function(x,y) {
x = y * x;
Return ??;
}
</script>

What should my Return statement look like to get the local x and y returned so that global_x and global_y get changed?

My reference manual said that functions work like subroutines in javascript if you leave the return statement out -- but obviously its wrong LOL since it doesn't work.

BTW I could swap to using arrays but its a bit of pain since I'm interactign with a sub system through tags that are not producing arrays as variables.

Thanks for any help. I know I'm being a bit thick here.

Savin

GJay
01-28-2006, 08:35 PM
When you pass arguments to a function, you pass their value, not the variables themselves, so (in this case at least) whatever you do inside the function stays inside the function.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum