Passing to/from function problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Reply Via Newsgroup

    Passing to/from function problems


    Folks,
    I have a simple script below... I come from a programming background
    with PHP,C++,bash unix shell scripting so I have a rough understanding
    when it comes to javascript. I have written a few javascripts of my own
    but only until now have I realised that this will be the first time that
    I have wanted to pass data *to* a function, chew it up, and spit it
    back... In my test script below, I would have expected the script to
    return def=1 and ghi=2, but it doesn't...

    Can someone tell me where I've gone wrong? Thanks, all replies, via the
    newsgroup would be much appreciated,

    randelld


    <script language="JavaS cript" type="text/javascript">
    function firstFunction(d ef, ghi)
    {
    def=1;
    ghi=2;
    return(def, ghi);
    }

    // create our variables
    var def;
    var ghi;

    // set def=10 and ghi=20
    def=10;
    ghi=20;

    // the function should now change def=1 and ghi=2
    firstFunction(d ef,ghi);

    document.write( "def=" + def + " ghi="+ghi);
    </script>
  • G Roydor

    #2
    Re: Passing to/from function problems

    1) il faut retourner un tableau
    2) appel se fait par valretour=mafon ction (.....)
    <html>
    <head>
    <title>Untitl ed Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <script language="JavaS cript" type="text/javascript">
    // create our variables
    var def;
    var ghi;

    function firstFunction(d ef, ghi)
    {
    def=1;
    ghi=2;
    return arguments;
    }


    </script>
    </head>

    <body bgcolor="#FFFFF F" text="#000000">
    <script language="JavaS cript" type="text/javascript">



    // set def=10 and ghi=20
    def=10;
    ghi=20;
    tb=new Array();
    // the function should now change def=1 and ghi=2
    tb=firstFunctio n(def,ghi);

    document.write( "def=" + tb[0] + " ghi="+ tb[1]);
    </script>

    </body>
    </html>

    Reply Via Newsgroup a écrit:[color=blue]
    >
    > Folks,
    > I have a simple script below... I come from a programming background
    > with PHP,C++,bash unix shell scripting so I have a rough understanding
    > when it comes to javascript. I have written a few javascripts of my own
    > but only until now have I realised that this will be the first time that
    > I have wanted to pass data *to* a function, chew it up, and spit it
    > back... In my test script below, I would have expected the script to
    > return def=1 and ghi=2, but it doesn't...
    >
    > Can someone tell me where I've gone wrong? Thanks, all replies, via the
    > newsgroup would be much appreciated,
    >
    > randelld
    >
    >
    > <script language="JavaS cript" type="text/javascript">
    > function firstFunction(d ef, ghi)
    > {
    > def=1;
    > ghi=2;
    > return(def, ghi);
    > }
    >
    > // create our variables
    > var def;
    > var ghi;
    >
    > // set def=10 and ghi=20
    > def=10;
    > ghi=20;
    >
    > // the function should now change def=1 and ghi=2
    > firstFunction(d ef,ghi);
    >
    > document.write( "def=" + def + " ghi="+ghi);
    > </script>[/color]

    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: Passing to/from function problems

      Reply Via Newsgroup <reply-to-newsgroup@pleas e.com> writes:
      [color=blue]
      > In my test script below, I would have expected the
      > script to return def=1 and ghi=2, but it doesn't...[/color]
      [color=blue]
      > <script language="JavaS cript" type="text/javascript">
      > function firstFunction(d ef, ghi)
      > {
      > def=1;
      > ghi=2;
      > return(def, ghi);[/color]

      You can only return one value in Javascript, not a tuple.
      You could return the two values as a composite value, e.g.,
      an array:
      return[def,ghi];
      or an object:
      return{def:def, ghi:ghi};

      [color=blue]
      > // the function should now change def=1 and ghi=2
      > firstFunction(d ef,ghi);[/color]

      Javascript is call-by-value. You declare local variables with the
      names "def" and "ghi" in the function (parameters are local
      variables), so there is no chance of changing the value of the global
      variables.

      If you return an array, you could do:

      var arr = firstFunction(d ef,ghi);
      def = arr[0];
      ghi = arr[1];

      If you return an object, you can do:

      var obj = firstFunction(d ef,ghi);
      def = obj.def;
      ghi = obj.ghi;

      You could also make the function without the parameters:

      function firstFunction() {
      def=1; // now not a local variable, so change is to global one
      ghi=2;
      }


      Good luck
      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Reply Via Newsgroup

        #4
        Re: Passing to/from function problems

        G Roydor wrote:[color=blue]
        > 1) il faut retourner un tableau
        > 2) appel se fait par valretour=mafon ction (.....)
        > <html>
        > <head>
        > <title>Untitl ed Document</title>
        > <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
        > <script language="JavaS cript" type="text/javascript">
        > // create our variables
        > var def;
        > var ghi;
        >
        > function firstFunction(d ef, ghi)
        > {
        > def=1;
        > ghi=2;
        > return arguments;
        > }
        >
        >
        > </script>
        > </head>
        >
        > <body bgcolor="#FFFFF F" text="#000000">
        > <script language="JavaS cript" type="text/javascript">
        >
        >
        >
        > // set def=10 and ghi=20
        > def=10;
        > ghi=20;
        > tb=new Array();
        > // the function should now change def=1 and ghi=2
        > tb=firstFunctio n(def,ghi);
        >
        > document.write( "def=" + tb[0] + " ghi="+ tb[1]);
        > </script>
        >
        > </body>
        > </html>
        >
        > Reply Via Newsgroup a écrit:
        >[color=green]
        >>
        >> Folks,
        >> I have a simple script below... I come from a programming background
        >> with PHP,C++,bash unix shell scripting so I have a rough understanding
        >> when it comes to javascript. I have written a few javascripts of my
        >> own but only until now have I realised that this will be the first
        >> time that I have wanted to pass data *to* a function, chew it up, and
        >> spit it back... In my test script below, I would have expected the
        >> script to return def=1 and ghi=2, but it doesn't...
        >>
        >> Can someone tell me where I've gone wrong? Thanks, all replies, via
        >> the newsgroup would be much appreciated,
        >>
        >> randelld
        >>
        >>
        >> <script language="JavaS cript" type="text/javascript">
        >> function firstFunction(d ef, ghi)
        >> {
        >> def=1;
        >> ghi=2;
        >> return(def, ghi);
        >> }
        >>
        >> // create our variables
        >> var def;
        >> var ghi;
        >>
        >> // set def=10 and ghi=20
        >> def=10;
        >> ghi=20;
        >>
        >> // the function should now change def=1 and ghi=2
        >> firstFunction(d ef,ghi);
        >>
        >> document.write( "def=" + def + " ghi="+ghi);
        >> </script>[/color]
        >
        >[/color]

        Merci beaucoup!

        Randell D.

        Comment

        • Reply Via Newsgroup

          #5
          Re: Passing to/from function problems

          Lasse Reichstein Nielsen wrote:
          [color=blue]
          > Reply Via Newsgroup <reply-to-newsgroup@pleas e.com> writes:
          >
          >[color=green]
          >>In my test script below, I would have expected the
          >>script to return def=1 and ghi=2, but it doesn't...[/color]
          >
          >[color=green]
          >><script language="JavaS cript" type="text/javascript">
          >>function firstFunction(d ef, ghi)
          >>{
          >> def=1;
          >> ghi=2;
          >> return(def, ghi);[/color]
          >
          >
          > You can only return one value in Javascript, not a tuple.
          > You could return the two values as a composite value, e.g.,
          > an array:
          > return[def,ghi];
          > or an object:
          > return{def:def, ghi:ghi};
          >
          >
          >[color=green]
          >>// the function should now change def=1 and ghi=2
          >>firstFunction (def,ghi);[/color]
          >
          >
          > Javascript is call-by-value. You declare local variables with the
          > names "def" and "ghi" in the function (parameters are local
          > variables), so there is no chance of changing the value of the global
          > variables.
          >
          > If you return an array, you could do:
          >
          > var arr = firstFunction(d ef,ghi);
          > def = arr[0];
          > ghi = arr[1];
          >
          > If you return an object, you can do:
          >
          > var obj = firstFunction(d ef,ghi);
          > def = obj.def;
          > ghi = obj.ghi;
          >
          > You could also make the function without the parameters:
          >
          > function firstFunction() {
          > def=1; // now not a local variable, so change is to global one
          > ghi=2;
          > }
          >
          >
          > Good luck
          > /L[/color]

          Thanks - I think I can follow that - or at least its given me something
          I can run with and research further.

          Cheers
          randelld

          Comment

          Working...