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 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Jul 2011
    Location
    london / uk
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts

    idiot proof explanation? / easy question

    hi guys, l am currently studying web technologies on a part time basis, and learning javascript.

    l got a very useful website, but still l dont understand certain things like this one:


    When a function has performed an assignment, it may provide a value that other functions would need, for any reason you judge necessary. When a function produces a value an makes it available to other functions, such a function is said to return a value.

    To define a function that returns a value, just before the closing curly bracket, type the return keyword followed by the value that the function returns. The value should be a valid and expected type. Because JavaScript is not a type-checking language, meaning it hardly checks anything you are doing, it is your responsibility to make sure that your function returns the right value.

    Here is a function that returns a value:

    function rectangleArea(length, height)
    {
    var area;
    area = length * height * 3.14159;
    return area;
    }

    Even if a function returns a value, it can still be called like any other function. If a function returns a value, its name can be assigned to a variable to give a value to, or change the value of, the variable. Here is an example:
    Code:
    <Script Language="JavaScript">
    function rectangleArea(length, height)
    {
      var area;
      area = length * height * 3.14159;
      return area;
    }
    function displayArea()
    {
      var l, h, rectArea;
      l = 52.05;
      h = 46.55;
      rectArea = rectangleArea(l, h);
      document.write("The area of the is ", rectArea);
    }
    </Script>
    
    <Script Language="JavaScript">
      displayArea();
    </Script>
    what l understand from the code is, l have 2 functions, first function is a return function and has got a value, but where is this value going? l mean what's happing with it? l mean l cant see this return value in the second function, why? l ma also not understand why we need to have 2 functions, why not just 1 ?/?? can someone explain me one by one the lines maybe?

    thanks

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    Mainly you use functions to modularize your code. Certain tasks can and should be encapsulated into a function.

    In your example the second function displayArea() is being called in the first place. In the fourth line of this function the first function rectangleArea() is being called with two parameters. Its return value (you can call it the value of the function) will then be assigned to the variable rectArea using the assignment operator =

    Inside the first function rectangleArea() there is a variable area which has been declared using the "var" keyword. Therefore this variable is local to the function. If you leave the function the variable will be lost. But if you return its value (!) this value will be available to the calling function outside of the current function. So it's not the variable itself that is available but its value.

  • Users who have thanked devnull69 for this post:

    doyouknow (07-17-2011)

  • #3
    New Coder
    Join Date
    Apr 2010
    Location
    Norfolk, England
    Posts
    63
    Thanks
    1
    Thanked 14 Times in 14 Posts
    Hi doyouknow

    the value returns to whatever called the function.

    In your case:

    Code:
    rectArea = rectangleArea(l, h);
    you are defining the value of rectArea by calling the function, so when that function you called returns a value, it will return to that definition, basically giving:

    Code:
    rectArea = RETURNED VALUE;


    Also, just to mention. In modern coding/doctypes, Language="Javascript"should be type="text/javascript"

    and in web coding tags, you should either choose upper case or lower case (normally lower) but don't mix (<Script should be <script>)
    //Improvement in coding is iterative, each 'failure' is just the next step on your learning curve, some knowledge and logic can get you a long way.//

  • Users who have thanked thesam101 for this post:

    doyouknow (07-17-2011)

  • #4
    New to the CF scene
    Join Date
    Jul 2011
    Location
    london / uk
    Posts
    5
    Thanks
    3
    Thanked 0 Times in 0 Posts
    thank you for the answers, do you guys know a website apart from w3schools, which is helpful to learn javascript, as l need to write a javascript code as a housework, but l have got no clue.

  • #5
    Senior Coder jmrker's Avatar
    Join Date
    Aug 2006
    Location
    FL
    Posts
    3,092
    Thanks
    38
    Thanked 498 Times in 492 Posts

    Lightbulb

    Quote Originally Posted by doyouknow View Post
    thank you for the answers, do you guys know a website apart from w3schools, which is helpful to learn javascript, as l need to write a javascript code as a housework, but l have got no clue.
    I have not personally worked through these but you might find them interesting.
    See: http://www.mredkj.com/tutorials/index.html
    and: http://www.lynda.com/JavaScript-trai...FY0H2god41Epzw
    and: http://www.javascriptkit.com/javatutors/
    and:http://www.echoecho.com/javascript.htm

    Do a google search for others: 'javascript tutorial'

  • Users who have thanked jmrker for this post:

    doyouknow (07-17-2011)

  • #6
    New Coder
    Join Date
    Apr 2010
    Location
    Norfolk, England
    Posts
    63
    Thanks
    1
    Thanked 14 Times in 14 Posts
    //Improvement in coding is iterative, each 'failure' is just the next step on your learning curve, some knowledge and logic can get you a long way.//


  •  

    Posting Permissions

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