Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 07-16-2011, 10:55 PM   PM User | #1
doyouknow
New to the CF scene

 
Join Date: Jul 2011
Location: london / uk
Posts: 5
Thanks: 3
Thanked 0 Times in 0 Posts
doyouknow is an unknown quantity at this point
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:

Quote:

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
doyouknow is offline   Reply With Quote
Old 07-16-2011, 11:31 PM   PM User | #2
devnull69
Senior Coder

 
Join Date: Dec 2010
Posts: 2,245
Thanks: 10
Thanked 531 Times in 525 Posts
devnull69 will become famous soon enough
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.
devnull69 is online now   Reply With Quote
Users who have thanked devnull69 for this post:
doyouknow (07-17-2011)
Old 07-16-2011, 11:32 PM   PM User | #3
thesam101
New Coder

 
Join Date: Apr 2010
Location: Norfolk, England
Posts: 63
Thanks: 1
Thanked 14 Times in 14 Posts
thesam101 is an unknown quantity at this point
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.//
thesam101 is offline   Reply With Quote
Users who have thanked thesam101 for this post:
doyouknow (07-17-2011)
Old 07-16-2011, 11:50 PM   PM User | #4
doyouknow
New to the CF scene

 
Join Date: Jul 2011
Location: london / uk
Posts: 5
Thanks: 3
Thanked 0 Times in 0 Posts
doyouknow is an unknown quantity at this point
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.
doyouknow is offline   Reply With Quote
Old 07-17-2011, 02:10 AM   PM User | #5
jmrker
Senior Coder

 
jmrker's Avatar
 
Join Date: Aug 2006
Location: FL
Posts: 2,764
Thanks: 29
Thanked 453 Times in 447 Posts
jmrker will become famous soon enough
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'
jmrker is offline   Reply With Quote
Users who have thanked jmrker for this post:
doyouknow (07-17-2011)
Old 07-17-2011, 09:29 AM   PM User | #6
thesam101
New Coder

 
Join Date: Apr 2010
Location: Norfolk, England
Posts: 63
Thanks: 1
Thanked 14 Times in 14 Posts
thesam101 is an unknown quantity at this point
My favourite:

http://www.quirksmode.org/js/contents.html

__________________
//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.//
thesam101 is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 10:11 PM.


Advertisement
Log in to turn off these ads.