Call by Value

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mario Thiel

    Call by Value

    Hello,

    i am new to JavaScript and right now i am learning Call by Value. i
    tryed to write a small script but somehow it doesnt work right. i cant
    put <br> into it, and i can only put the document.write( xyz,x) in the
    function.

    <html>
    <head>
    <script language="JavaS cript">
    function Wert(x) {
    x += 5;
    document.write( xyz)
    document.write( x)//Why can i add an <br>?
    }
    </script>
    </head>
    <body>
    <script language="JavaS cript">
    var xyz = 10;
    Wert(xyz);
    Wert(x);
    //Why cant i put the document.write in here?
    </script>
    </body>
    </html>

    Greets from Germany

  • Brian

    #2
    Re: Call by Value


    "Mario Thiel" <sepppel@freene t.de> wrote in message
    news:br4lf0$293 k27$1@ID-217726.news.uni-berlin.de...[color=blue]
    > Hello,
    >
    > i am new to JavaScript and right now i am learning Call by Value. i
    > tryed to write a small script but somehow it doesnt work right. i cant
    > put <br> into it, and i can only put the document.write( xyz,x) in the
    > function.
    >
    > <html>
    > <head>
    > <script language="JavaS cript">
    > function Wert(x) {
    > x += 5;
    > document.write( xyz)
    > document.write( x)//Why can i add an <br>?
    > }
    > </script>
    > </head>
    > <body>
    > <script language="JavaS cript">
    > var xyz = 10;
    > Wert(xyz);
    > Wert(x);
    > //Why cant i put the document.write in here?
    > </script>
    > </body>
    > </html>
    >
    > Greets from Germany
    >[/color]

    You are having a huge problem with variable scope. For instance, when you
    call Wert(x), X has never been declared. Also, this code never tries to
    write a <BR>. Basically, when you call Wert(x), you fail. Whatever is
    happening, you are likely failing with something, which is likely due to
    variable scope, and you are not finishing.

    Both xyz and x need to be in global scope. I have no idea what this code is
    supposed to due, but the following modifications will make everything work:

    <html>
    <head>
    <script language="JavaS cript">

    // Declare the variables as global, before they are used.
    var xyz = 10;
    var x = 0;

    function Wert(x) {
    x += 5;
    document.write( xyz)
    document.write( x)//Why can i add an <br>?
    document.write( "<BR>");
    }

    </script>
    </head>
    <body>
    <script language="JavaS cript">
    Wert(xyz);
    Wert(x);
    //Why cant i put the document.write in here?
    // Wert(x) will not fail any more, allowing you to proceed
    document.write( "WRITE IS WORKING, now");
    </script>
    </body>
    </html>


    Comment

    • @SM

      #3
      Re: Call by Value

      Mario Thiel a ecrit :
      [color=blue]
      > Hello,
      >
      > i am new to JavaScript and right now i am learning Call by Value. i
      > tryed to write a small script but somehow it doesnt work right. i cant
      > put <br> into it, and i can only put the document.write( xyz,x) in the
      > function.[/color]

      try that to understand a little
      [color=blue]
      > <html>
      > <head>
      > <script language="JavaS cript">
      > function Wert(x) {[/color]

      x=x*1; // to be sure x is a number
      [color=blue]
      > x += 5;[/color]

      /*
      [color=blue]
      > document.write( xyz)[/color]

      that is not correct ==> xyz is unknown
      you could do : */
      xy = 'hello world'
      document.write( x+'<hr><h2>'+xy +'</h2');
      [color=blue]
      > document.write( 'test with : '+x)//Why can i add an <br>?[/color]

      // because you have to tell it in JS ==> write('<br>');

      document.write( '<hr><h3>Addin g 5 times 1 to number : '+x+'</h3>');
      for(var i=1;i<6;i++)
      document.write( (x*1+i*1)+'<br> ');

      document.write( '<hr><h3>Geomet rical suite by 20 of number : '+x+'</h3>');
      for(var i=0;i<150;i+=20 )
      if(i>0)
      document.write( (x*i)+' = '+x+' * '+i+'<br>');

      document.write( '<p align=center>--- End of this test -----<p>');
      [color=blue]
      > }
      > </script>
      > </head>
      > <body>
      > <script language="JavaS cript">
      > var xyz = 10;
      > Wert(xyz);[/color]

      /*
      [color=blue]
      > Wert(x);[/color]

      */
      // you have to give a value to 'x'
      Werf(4); // Play with number 4
      document.write( '<h1>Other Number</h1>');
      Werf('15'); // Play with number 15
      [color=blue]
      > //Why cant i put the document.write in here?
      > </script>[/color]
      [color=blue]
      > </body>
      > </html>[/color]

      --
      *************** *************** *************** *************** **
      Stéphane MORIAUX : mailto:stephane OTER-MOImoriaux@wana doo.fr
      Aide aux Pages Perso (images & couleurs, formulaire, CHP, JS)

      *************** *************** *************** *************** **


      Comment

      • Mario Thiel

        #4
        Re: Call by Value

        Thanks you too, i see the mistake now. This script was just for
        understanding, not for having any purpose.

        Comment

        • Thomas 'PointedEars' Lahn

          #5
          Re: Call by Value

          Mario Thiel wrote:
          [color=blue]
          > i am new to JavaScript and right now i am learning Call by Value.[/color]

          JavaScript does not support call-by-value. It supports a special
          kind of call-by-reference which differs from that you may be used
          to:

          function foobar(o, o2, p, i)
          {
          // Does not redefine the referenced object
          // but uses `o' as local variable and assign
          // it a primitive boolean value:
          // "call-by-value"
          o = false;
          alert("foobar:: o = " + o) // false

          // Changes the value of a property of the referenced object
          // using the value of `i'
          // "call-by-reference" for one, "call-by-value" for the other
          o2.haha = i;

          // Adds a property to the referenced object:
          // "call-by-reference"
          o2.bla = "blurb";

          // Does not change the value of the global variable but uses
          // `p' as local variable:
          // "call-by-value"
          p = Math.PI;
          }

          var x = 42;
          var obj1 = {};
          var obj2 = {};
          obj2.haha = "333";

          foobar(obj1, obj2, x, 999);

          // "42 [object Object] 999 blurb undefined" (Mozilla/5.0)
          alert(
          x
          + " " + obj1
          + " " + obj2.haha
          + " " + obj2.bla
          + " " + typeof o);


          PointedEars

          Comment

          • Lasse Reichstein Nielsen

            #6
            Re: Call by Value

            Thomas 'PointedEars' Lahn <PointedEars@we b.de> writes:
            [color=blue]
            > Mario Thiel wrote:
            >[color=green]
            >> i am new to JavaScript and right now i am learning Call by Value.[/color]
            >
            > JavaScript does not support call-by-value. It supports a special
            > kind of call-by-reference which differs from that you may be used
            > to:[/color]

            I guess that's a matter of philosophy. Javascript definitly passes
            simple values (strings, numbers, booleans, null and undefined) by
            value. The "problem" is objects, which are passed ... as objects.

            I would still call it "call-by-value", it's just that the value of an
            object is the object itself, not a copy of it (because objects have an
            identity, an copy of an object would be a different object). It is
            probably *implemente* by passing a reference, but it's not like you
            can change what the reference points to (as with call-by-reference).

            To me, Javascript is call-by-value (and so is, e.g., Java, but not
            C# or C++)

            /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

            Working...