Programming basics
Functions
Functions are separate fragments of code, which perform certain task. Example of function name may be "calculateInterest" or "paintPicture".
Syntax of the function is simple. It has a name with couple parentheses with possible arguments or without them. The code of the function is included in the pair of curly braces.
function calculate(b,c) {
a = b+c;
if (a==3) {
message = "You won!";
}
else
{
message = "You lost!";
}
}
If such function is defined it may be called from some other place of the program like that
calculate (1, 2);
Function call will reach the function, supply it with parameters 1 and 2 for b and c and assign "You won!" to the variable message.
If function has certain arguments you need to provide parameters for them. If function argument list is empty – you cannot pass an information to the function.
Anonymous functions are allowed. They are often used to return the value to the caller.
varResult = function() {
varA*varB + varC;
}
An example above is simple and it's easier to incorporate without function, but later on you will find many spots where it's very useful.