Returning multiple results from a subroutine ?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dr John Stockton

    #1

    Returning multiple results from a subroutine ?


    What are the best ways of returning multiple results from a subroutine ?

    I've been using
    ... return [a, b, c] }
    which is inelegant.

    I'm used to Pascal's
    procedure X(const A, B : integer; var C, D : byte) ;
    where A, B are inputs only, and C, D are in/out.

    The needed parameters tend to be numbers, and don't need to be objects.

    ... return {aa:a, bb:b, cc:c} } // seems OK

    but with that the parameter names aa, bb, cc are not visible in the
    calling statement, which is a defect.

    I'm specifically trying to extend, neaten, & improve <URL:http://www.mer
    lyn.demon.co.uk/js-date5#DDTA>.

    --
    © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
    <URL:http://jibbering.com/faq/> Jim Ley's FAQ for news:comp.lang. javascript
    <URL:http://www.merlyn.demo n.co.uk/js-index.htm> JS maths, dates, sources.
    <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/JS/&c., FAQ topics, links.
  • asdf asdf

    #2
    Re: Returning multiple results from a subroutine ?

    You could pass in a function pointer to a callback.

    You could implement your method as a mutator on a javascript class
    (the new internal state would be your return values).

    hope that helps,
    Ron

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Returning multiple results from a subroutine ?

      b0b0b0b@yahoo.c om (asdf asdf) writes:
      [color=blue]
      > You could pass in a function pointer to a callback.[/color]

      Ah, Continuation Passing Style :) My first thought too, but not something
      I would recommend in a language without tail-call optimization.
      [color=blue]
      > You could implement your method as a mutator on a javascript class
      > (the new internal state would be your return values).[/color]

      If you want to emulate call-by-name (the "var" parameter of Pascal or
      &-parameter of C++), you can pass a return-object, and set the values
      as propertie of it, instead of creating the object in the called
      function. It is the closest to a Pascal "var"-parameter that I can
      find.

      I miss the tuples and simultaneous assignments of languages like SML
      and Perl. There is no equivalent in Javascript. The closest would be
      a returning an object and using a "with" statement.

      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • David

        #4
        Re: Returning multiple results from a subroutine ?

        It may be less elegant than what you have now, but you could put all
        the numbers into an array. Then just pass a single variable (the array
        name) in and out of the sub-routine and access the elements of the
        array as required.

        David

        Comment

        • rh

          #5
          Re: Returning multiple results from a subroutine ?

          Lasse Reichstein Nielsen <lrn@hotpop.com > wrote in message news:<65jj6vgn. fsf@hotpop.com> ...[color=blue]
          > b0b0b0b@yahoo.c om (asdf asdf) writes:
          >[/color]
          [color=blue]
          >
          > I miss the tuples and simultaneous assignments of languages like SML
          > and Perl. There is no equivalent in Javascript. The closest would be
          > a returning an object and using a "with" statement.
          >[/color]

          Or, at the risk of offending those who so far only moderately despise
          "with", and highly despise "eval", you could alternatively re-manifest
          the return object properties into caller variables by:

          for (var j in retObj) eval(j +"=retObj['"+j+"']");

          which might come a little closer to call by name than the use of
          "with". That's in the vein of a somewhat neutral observation, as
          opposed to a recommendation, however.

          (By the way, I fully concur with the attempt to stamp out the evil of
          [completely unnecessary use of] eval. Nonetheless, I have run into
          cases where only the name of a variable is known, and in the absence
          of an available reference, have taken the reasonable course and
          eval'ed in order to assign/retrieve a value. If there's a good
          alternative, the doh'lt is yet to discover it.)

          ../rh
          [color=blue]
          > /L[/color]

          Comment

          • Douglas Crockford

            #6
            Re: Returning multiple results from a subroutine ?

            > (By the way, I fully concur with the attempt to stamp out the evil of[color=blue]
            > [completely unnecessary use of] eval. Nonetheless, I have run into
            > cases where only the name of a variable is known, and in the absence
            > of an available reference, have taken the reasonable course and
            > eval'ed in order to assign/retrieve a value. If there's a good
            > alternative, the doh'lt is yet to discover it.)[/color]

            var global = this; // This is more portable than relying on 'window'.
            ....
            var variable_name = "foo";
            var value = global[variable_name];



            Comment

            • Lasse Reichstein Nielsen

              #7
              Re: Returning multiple results from a subroutine ?

              codeHanger@yaho o.ca (rh) writes:
              [color=blue]
              > Or, at the risk of offending those who so far only moderately despise
              > "with", and highly despise "eval",[/color]

              That would be me :)
              [color=blue]
              > you could alternatively re-manifest the return object properties
              > into caller variables by:
              >
              > for (var j in retObj) eval(j +"=retObj['"+j+"']");[/color]

              This differs from using "with" in two ways:

              - You only get the values of the enumerable properties of retObj.
              Using "with" makes all the properties visible. That is not so much of
              a difference, since the non-enumerable properties of a new object (the
              ones in Object.prototyp e) are already visible as properties of the
              global object.

              - It assigns the values to local variables if they exist, but those
              properties that don't have a local variable of the same name are
              created as global variables. With a "with", they are all made local
              variables, but will shadow preexisting local variables of the same
              name.

              This is actually a use for eval that I can't find a way around:
              setting the value of a local variable given its name as a string.
              It is also bad coding practice. Local variables should be free to
              be renamed without changing the how the program works, but this
              code will let a function from anywhere in the program change a local
              variable by name. I recommend against it.

              I would recommend returning an array and have the caller decide which
              variables to assign to, or returning an object, and letting the caller
              decide how to access the properties.
              [color=blue]
              > which might come a little closer to call by name than the use of
              > "with".[/color]

              Maybe a little closer, but it is not call-by-name. It is
              call-by-copy-restore, but I think that is the best we can get anyway.
              [color=blue]
              > That's in the vein of a somewhat neutral observation, as
              > opposed to a recommendation, however.[/color]

              :)
              [color=blue]
              > (By the way, I fully concur with the attempt to stamp out the evil of
              > [completely unnecessary use of] eval. Nonetheless, I have run into
              > cases where only the name of a variable is known, and in the absence
              > of an available reference, have taken the reasonable course and
              > eval'ed in order to assign/retrieve a value. If there's a good
              > alternative, the doh'lt is yet to discover it.)[/color]

              If the variable is a local variable, then I can't see another way,
              since we don't have a reference to the activation object. I highly
              recommend not to throw around the names of local variables, though.
              Make the global if someone outside of the scope needs to have access
              to them.

              /L
              --
              Lasse Reichstein Nielsen - lrn@hotpop.com
              Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
              'Faith without judgement merely degrades the spirit divine.'

              Comment

              • rh

                #8
                Re: Returning multiple results from a subroutine ?

                Lasse Reichstein Nielsen <lrn@hotpop.com > wrote in message news:<n0cu5utg. fsf@hotpop.com> ...[color=blue]
                > codeHanger@yaho o.ca (rh) writes:
                >[color=green]
                > > Or, at the risk of offending those who so far only moderately despise
                > > "with", and highly despise "eval",[/color]
                >
                > That would be me :)[/color]

                And me :)[color=blue]
                >[color=green]
                > > you could alternatively re-manifest the return object properties
                > > into caller variables by:
                > >
                > > for (var j in retObj) eval(j +"=retObj['"+j+"']");[/color]
                >
                > This differs from using "with" in two ways:
                >
                > - You only get the values of the enumerable properties of retObj.
                > ...[/color]

                While that may be a difference in the use of "with", my assumption was
                that retObj would be generated as an object literal as the return
                expression in the called function. As such, there wouldn't be any
                non-enumerable properties.
                [color=blue]
                > - It assigns the values to local variables if they exist, but those
                > properties that don't have a local variable of the same name are
                > created as global variables. With a "with", they are all made local
                > variables, but will shadow preexisting local variables of the same
                > name.
                >[/color]

                See below regarding restriction.
                [color=blue]
                > This is actually a use for eval that I can't find a way around:
                > setting the value of a local variable given its name as a string.[/color]

                Thanks for confirming.
                [color=blue]
                > It is also bad coding practice. Local variables should be free to
                > be renamed without changing the how the program works, but this
                > code will let a function from anywhere in the program change a local
                > variable by name. I recommend against it.[/color]

                The eval is in the calling function, so the only functions that could
                change local variables within are those where there is an explicit
                intention to allow it. It would be trivial to restrict the variables
                in the caller that would be open to change by the callee (by evaling
                on the restricted set, as opposed to the property names provided by
                retObj). If you don't want to trust, you don't have to.
                [color=blue]
                >
                > I would recommend returning an array and have the caller decide which
                > variables to assign to, or returning an object, and letting the caller
                > decide how to access the properties.
                >[/color]

                I tend to use an array if there are two or three return values. More
                than that, I create an object literal and access the properties.
                [color=blue][color=green]
                > > which might come a little closer to call by name than the use of
                > > "with".[/color]
                >
                > Maybe a little closer, but it is not call-by-name. It is
                > call-by-copy-restore, but I think that is the best we can get anyway.
                >[color=green]
                > > That's in the vein of a somewhat neutral observation, as
                > > opposed to a recommendation, however.[/color]
                >
                > :)[/color]

                :)
                [color=blue]
                > If the variable is a local variable, then I can't see another way,
                > since we don't have a reference to the activation object. I highly
                > recommend not to throw around the names of local variables, though.
                > Make the global if someone outside of the scope needs to have access
                > to them.[/color]

                As pointed out above, you don't really have to "throw around the
                names". I hold a similar aversion to putting things into the global
                namespace -- seems to me there's already too much potential for name
                conflict and side-effects there.

                Just trying to hold your feet to the fire a bit here. ;)

                ../rh
                [color=blue]
                >
                > /L[/color]

                Comment

                Working...