Announcement

Collapse
No announcement yet.

Parameter Passing

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

  • Parameter Passing

    How do I return a calculated value from a routine()


    vara=5;
    varb=10;
    goDoSomething(vara, varb, varc)
    //come back with varc
    debugPrintln(varc);


    goDoSomething(vara, varb, varc){
    varc=vara+varb;
    }

    I keep getting undefined for varc

  • #2
    var varc = goDoSomething(vara, varb);
    Garth

    Comment


    • #3
      David:

      Per Garth's note, you would also need to actually return something from your function.....

      function main() {
      ..
      ..
      ...
      var MyVar = goDoSomething( a, b );
      ...
      }

      function goDoSomething( x, y ) {
      return( x+y);
      }

      Chris

      Comment


      • #4
        Thanks, that makes sense. I havent actually got it to work but I will in the am.

        If I want to return 2 variables, can I do that too.

        The method you showed looked like it only could return one variable at a time.

        Comment


        • #5
          That worked great, now if I can learn about returning 2 variables, that would be supah!

          Comment


          • #6
            One word: "arrays"
            Garth

            Comment


            • #7
              Hi Dave:

              You can also use Objects to accomplish that task:

              //declare an object with 3 elements
              var myObject = { age: 25, height: 175, eyes: "blue" };

              //print it out to verify
              debugPrint("Age: "+myObject.age + "Height: "+myObject.height + "\n");

              //call your function
              myTestFunc( myObject );

              //print it again to see the changes made
              debugPrint("Age: "+myObject.age + "Height: "+myObject.height + "\n");


              ...
              ...
              ...
              }

              //This func, by using Objects, can change and return multiple values
              function myTestFunc( anObject ) {
              anObject.age = 45;
              anObject.weight = 225;
              }


              Hope this helps.

              Chris

              Comment


              • #8
                ..... and to take it one step further, you can also create arrays of objects which can make data manipulation much easier.
                And arrays of objects can be sorted by any of the elements which is nice.

                For example:

                var aBase = new Array();

                for (x=0; x<50; x++) {
                aBase[x] = new Object;
                with (aBase[x]) {
                sName = "frog"+x;
                nValue = x*x;
                nIQ = x/(Value);
                }
                }


                aBase.sort( mySortFunc );

                ...
                ...
                ...

                }

                //sort the object array based on IQ
                function mySortFunc( argv, argc ) {
                return( argv.nIQ > argc.nIQ ? 1 : -1 );
                }


                Chris

                Comment

                Working...
                X