This returns a value.  

Click me

Actually, we send in Two variables (the numbers 1 and 3)
We put a comma inbetween the numbers to separate them (not a semicolon - that's to separate functions)

and return the value of them added together.

 

<SCRIPT LANGUAGE = JavaScript>

function myFunction(){
document.writeln("The number is " + getNumber(1, 3) + ".")
}


function getNumber(a, b)
{
return(a + b)
}
</SCRIPT>

 

The "return" portion gives us a work-around for global variables

This function could have been done with global variables: - the variable myNumber - when inserted in the javascript with no function enclosing it, is now seen by everything on this html page. - Anything on this page can use and change myNumber.

<SCRIPT LANGUAGE = JavaScript>

var myNumber

function myFunction(a, b){
getNumber(a,b)
document.writeln("The number is " + myNumber + ".")
}

function getNumber(a, b)
{
myNumber = a + b
}
</SCRIPT>

In the above example we use "myNumber" (a global variable) and set it equal to a + b in it's own function

In order to USE that function we have to call it in myFunction


<SCRIPT LANGUAGE = JavaScript>

var myNumber

function myFunction(a, b){
getNumber(a,b)
document.writeln("The number is " + myNumber + ".")
}

function getNumber(a, b)
{
myNumber = a + b
}
</SCRIPT>

 



Here's one more:

 

this part is in the top of the document.... in the script portion


function return_message(second_message) {
return "This message was returned from a function"
}

 

In the html portion


<BODY>
<PRE>
<SCRIPT LANGUAGE="JavaScript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS

var return_value = return_message();
document.writeln(return_value);
// STOP HIDING FROM INCOMPATIBLE BROWSERS -->
</SCRIPT>
</PRE>

</BODY>