...

View Full Version : idiot proof explanation? / easy question



doyouknow
07-16-2011, 10:55 PM
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:



<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 :)

devnull69
07-16-2011, 11:31 PM
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.

thesam101
07-16-2011, 11:32 PM
Hi doyouknow

the value returns to whatever called the function.

In your case:


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:


rectArea = RETURNED VALUE;

:thumbsup:

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>) ;)

doyouknow
07-16-2011, 11:50 PM
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.

jmrker
07-17-2011, 02:10 AM
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-training-tutorials/244-0.html?AWKeyWord=web&_kk=javascript%20tutorial&_kt=e1885f2d-0485-4221-b81f-1b4bacd3766a&gclid=CPaGnM-Xh6oCFY0H2god41Epzw
and: http://www.javascriptkit.com/javatutors/
and:http://www.echoecho.com/javascript.htm

Do a google search for others: 'javascript tutorial'

thesam101
07-17-2011, 09:29 AM
My favourite:

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

:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum