Question:
What is a function?
Answer:
A function is a block of statements grouped together to accomplish a single task, such as:
initialize images on a web page, calculate students' grades, or validate user input.
Question:
What is the benefit of using functions?
Answer:
Functions keep your code organized and make it easier to modify, should changes be needed in the future.
Each function should begin with comments describing its purpose, and any other useful explanations,
such as what the parameters are, or what its return value represents - for example:
a -1 may indicate an error, a 0 may indicate no action was taken, and a 1 may indicate success.
Functions allow several programmers to work simultaneously on a large project without getting in each others' way,
by compartmentalizing development tasks. For example, one programmer might work on images and formatting,
while another works on data validation, and a third works on navigation.
Question:
What is the syntax for defining a function?
Answer:
A function consists of a function header, and a function body.
The function header begins with the key word "function", followed by a space and the function name
(named by the programmer), followed by open and close parentheses.
The function body begins with an open curly-brace, and ends with a close curly-brace.
There is no code between the function header and function body.
Any code the function performs is placed inside the body.
The parentheses in the header may contain zero or more parameters, each separated by a comma.
If a function has parameters, do not declare them again inside the body (with the keyword var),
they are implicitly declared inside the parentheses. Example:
function MyFunction(parm1, parm2, parm3)
{
// insert function code here
}
Notice that there is no semicolon ; at the end of the parentheses,
nor after the closing curly-brace.
Question:
How do I return a value from a function?
Answer:
Use the keyword "return", followed by a space then an expression ending in a semicolon.
The expression can be a variable, object, literal, calculation... (determined by the programmer).
You can only return one item from a function - for example,
you cannot return several expressions separated by commas. Example:
function MyFunction(parm1, parm2, parm3)
{
// insert function code here
return (parm1 + parm2 + parm3);
}
Note: Such a function would be more aptly named
"GetTotal" or "Concat3Strings",
depending on the content of the parameters.
The parmaters should also be more aptly named.
Question:
How do I call a function?
Answer:
Write the name of the function, followed by parentheses, followed by a semicolon.
Do not use the keyword "function" when you call a function.
If the function has parameters, include them inside the parentheses.
The names of your parameters do not have to match the names of the parameters
in the function definition header. The value of the first parameter in your function call
will become the value of the first parameter in the function header, and so on.
The parameters in your function call can be variables, objects, literals...
If the function returns a value, assign that to a variable using the equal sign. Example:
var row = 3;
var col = 7;
var myAnswer = MyFunction(row, col, 12);
// Following along the examples on this page:
// myAnswer == 22
Question:
How many times can I call a function?
Answer:
You can call a function as many times as you need, there is no limit.
Question:
From where do I call a function?
Answer:
For the most part, functions are called from inside other functions. For example,
you can call MyFunction several times from InitFunction, as well as from GetTotalsFunction,
and from any other function.