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