Pilcrow Records

Scripter Javascript Tutorial

javascriptmusiclogicscripterbookexcerpttutorial

36 Functions

MDN: Reference: Functions

Functions are another core building block of JavaScript (and for most popular programming languages). Essentially, a function is code which is developed to perform a specific task, such as performing a calculation, and that task can be called as needed. Any time a bit of code, be it a line or a block, is repeated, then it should go into a function. The syntax for a function is very simple:

function name( argument1, argument2 ) {
	// do something
}

In the above example:

A very simple example is adding two numbers together:

function add( operand1, operand2 ) {
	var result = operand1 + operand2;
	return result;
}

var op1 = 2;
var op2 = 3;
var sum = add(op1, op2); // sum = 5

In the above example: