Announcement

Collapse
No announcement yet.

Functions and parameters

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Functions and parameters

    Hi

    Can I update function parameters in a function ... I have some code but it does need seem to pass back variables updated in functions ... in the example below c1 and c2 are not updated following the call to updatemyvariables??????

    Thanks - any help appreciated

    Paul

    var c1; //global variable
    var c2; //global variable

    function preMain() {
    c1 = 0;
    c2 = 0;
    }

    function main() {

    updatemyvariable(c1,c2);


    }

    function updatemyvariable(x1,x2) {

    x1 = x1+1;
    x2 = x2+1;
    }

  • #2
    Paul
    Assuming I understood what you are trying to do here is how you would need to write the script.
    Alex

    PHP Code:
    var c1
    var 
    c2
     
    function 
    preMain() {
        
    c1 0;
        
    c2 0;
    }
     
    function 
    main() {
        
    updatemyvariable(c1,c2);
        
    debugPrintln(c1+"  "+c2);
    }
     
    function 
    updatemyvariable(x1,x2) {
        
    c1 x1+1;
        
    c2 x2+1;

    Comment


    • #3
      Alex

      no not really - I have a buch of global arrays and an update function that adds an element to any array etc ... I want to have one function be able to pass in 4 of the arrays at a time and update them in the function otherwise i have to repeat the update code for each array ...
      I cannot know in the function which global variable is being passed in so i cannot reference it directly ????

      Thanks

      Paul

      var c2;
      var c1;

      function main () {

      updatemyvars(c1,c2,open(0),close(0));

      }

      function updatemyvars(x,y,a,b) {

      x = x + a;
      y = y + b;

      }

      Comment


      • #4
        Alex

        In other words ... in the update function I don't know what is being passed in so I cannot reference it directly...

        var c1;
        var c2;
        var c3;

        function preMain() {

        c1 = 0;
        c2 = 0;
        c3 = 0;

        }

        function main () {

        if (getMinute() == 1) updatemyvariable (c1,c2);

        // here i want c2 = c2 + open(0);

        else if (getMinute() == 3) updatemyvariable (c2,c3);

        // here i want c2 = c2 + close(0);

        }

        function updatemyvariable (x,y) {

        x = x+ close(0);
        y = y + open(0);

        }

        Comment


        • #5
          Paul
          As I understand it when you pass to a function a variable that is a number this is passed in by value ie the actual number is passed to the function and not the variable that represents the number. In this case the passed value will be modified by the function but only locally while the external variable will not be updated by the function.
          In order to update the external variable this needs to be an object so that you can pass it to the function by reference ie pass the actual variable and not its value. In this case the changes applied to the variable in the function will be global. Enclosed below is an example of how you could accomplish this.
          You can read more on the subject of passing variables in JavaScript in this article I found on the net.
          Alex

          PHP Code:
          var c1 = new Object(); 
          var 
          c2 = new Object(); 
           
          function 
          preMain() {
              
          c1.myValue 0;
              
          c2.myValue 0;
          }
           
          function 
          main() {
              
          updatemyvariable(c1,c2);
              
          debugPrintln(c1.myValue+"  "+c2.myValue);
          }
           
          function 
          updatemyvariable(x1,x2) {
              
          x1.myValue x1.myValue+1;
              
          x2.myValue x2.myValue+1;

          Comment


          • #6
            Alex

            Thanks again ... that makes perfect sense

            Paul

            Comment


            • #7
              Paul
              You are most welcome
              Alex

              Comment

              Working...
              X