Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Javascript Function problem

    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.
    ***************
    Code:
    <!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.

  • #2
    fci
    fci is offline
    Senior Coder
    Join Date
    Aug 2004
    Location
    Twin Cities
    Posts
    1,345
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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:
    Code:
    <!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>

  • #3
    New to the CF scene
    Join Date
    Jan 2006
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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

    Code:
    <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

  • #4
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •