Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    May 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy newbie question: variable and function

    see the piece of code:
    -----------------------------------
    <html>
    <head>
    <script language="JavaScript">
    function makeText(var txt,var pic)
    {
    var s=txt+", hello, "+pic;
    return s;
    }
    </script>
    </head>
    <body>
    *********
    <br>
    <script language="JavaScript">
    var str=makeText("More Text","down.bmp");
    document.write(str);
    </script>
    <br>
    </body>
    </html>
    -------------------------

    my questions:

    1)why function above can not return that string?
    can java-script return a value?

    2)are variables in java-script similar to c++ which contain following concepts:
    global, constant,reference,pointer variable etc?

    3)are there any differences if codes are in one <script ..></script> pair or in 2 <script ..></script> pairs?

    BTW: I am a c++/java program, please compare with c++/java if possible.
    Last edited by hello20109876; 05-06-2006 at 09:19 AM.

  • #2
    Regular Coder
    Join Date
    Sep 2005
    Location
    Chicago, IL
    Posts
    160
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It won't return a string because your not asking it to return a string, or at least asking it to write it out. This is your code with some corrections and explanations. Granted this may not be the most efficient way, it'll be easier for you to understand because you wrote it.

    PHP Code:
    <script type="text/javascript">
    <!-- Use 
    script type='text/javascript' because its w3c compliant and the 'correct' way to write it -->
    function 
    makeText(txt,pic)
    <!-- 
    You don't need the word 'var' in front of 'txt' and 'pic because its already understood that they are variables -->
    {
    var 
    s=txt+", hello, "+pic;
    document.write(s);
    <!-- 
    Instead of using return swhich you can use to pass avraible from function to function, its easier to use the document.write right there so it will go ahead and write it wherever you place the code to call it -->
    }
    </script> 
    Now for your 'calling code' (because your calling the function above up):
    PHP Code:
    <script type="text/javascript">
    <!-- Use 
    script type='text/javascript' because its w3c compliant and the 'correct' way to write it -->
    makeText("More Text","down.bmp");
    <!-- 
    Since your calling the function up to be placed here you dont use variable to call itjust the name of the function and the variables inside the brackets -->
    </script> 
    And you could just place that last code anywhere on the page that you want that text to be displayed. But there are exceptions to the rules, mroe efficient ways of doing things, etc... But its a learning process and you gotta start somewhere. (Just trying to cover my butt because someone usually says that you could do this better by doing this.......whatever......) I'm just trying to give you an idea of how you can do it. I would put it in C++/Java terms btu I have no idea about either of those, so if you have any questions just ask.
    I'm only telling you how I would do it, not how its supposed to be done. ;)

  • #3
    New Coder
    Join Date
    May 2006
    Posts
    17
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thumbs up

    many thanks for your long reply.

    acturally, after several funny tests, I have solved the problem myself:
    result is:
    if i add var before parameters of txt and pic, the function doesn't work.

    very strange, i don't understand why java-script is designed in this way - I have to just follow it.





  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •