Functions : Function Definition « Function « JavaScript Tutorial






All function declaration must begin with the keyword function followed by the name of the function.

Parentheses are placed after the function name to hold arguments.

If more than one argument are used, use commas to separate the arguments.

If no arguments, leave the space between the parentheses empty.

Curly brackets are used to contain the code related to the function.

Curly brackets are not optional.

JavaScript uses call by value.

<html>
<SCRIPT LANGUAGE='JavaScript'>
<!--
    var aString = "A"
    var aNumber = 1;
    function test(aString, aNumber) {
      aString = "B";
      aNumber = 2;
      document.write("<BR><BR><BR><BR><BR>During function call:<BR>");
      document.write("aStringCopy=",aString,"<BR>");
      document.write("aNumberCopy=",aNumber,"<BR>");
    }

    document.write("<BR><BR><BR><BR><BR>Before function call:<BR>");
    document.write("aString=",aString,"<BR>");
    document.write("aNumber=",aNumber,"<BR>");


    test(aString,aNumber);


    document.write("<BR><BR><BR><BR><BR><BR><BR>After function call:<BR>");
    document.write("aString=",aString,"<BR>");
    document.write("aNumber=",aNumber,"<BR>");
    //-->
</SCRIPT>
</html>








7.1.Function Definition
7.1.1.Functions
7.1.2.Define the simplest function
7.1.3.Define a function
7.1.4.Calculation in function
7.1.5.Recursive function