| Function Definition |
function FuncName(param1, param2,...)
{
variable-declarations
function-body
return return-value;
}
- FuncName:
- The name of the function.
- param1:
- The name of the first parameter passed to the function.
- variable-declarations:
- list of local variables (if any) that appear in the function, preceded by var.
- function-body:
- JavaScript statements to be executed when the function is called.
- return-value:
- Value that is returned from this function. The return statement is optional.
|
function FooFunc()
// Returns: the string "foo"
{
return "foo";
}
function BarFunc(N)
// Assumes: N >= 0
// Returns: string of N "bar"s
{
var i, theStr;
theStr = "";
for (i = 1; i <= N; i = i + 1) {
theStr = theStr+"bar";
}
return theStr;
}
function Greet(name)
// Assumes: name is a string
// Results: displays greeting
{
document.write("Hi "+name);
}
|
| Function Call |
myVar = FuncName(arg1, arg2,...);
- myVar:
- Variable to hold the value (if any) returned by the function.
- FuncName:
- Name of the function to call.
- arg1:
- Argument 1, the value for the first parameter of the function.
|
Str1 = FooFunc();
// Result: Str1 == "foo"
Str2 = BarFunc(3);
// Result: Str2 == "barbarbar"
Greet("Oliver");
// Result: displays "Hi Oliver"
|