Quick Index
Math Definition of Function


f(x) = x2 = x * x
Inputs:
   x = 7
Outputs:
   y = 49


Coding Definition of Function


The coding definition of function is a callable unit of code.

Some other terms sometimes used for function include method, procedure and routine.

Coding (Input and output)
In coding, a function (or method) can follow the math pattern (above). It may also have two variations -- see following steps.
function squareNumber(x) {
	return x * x;
}
/*
Inputs:
	x = 7
Outputs:
	y = 49
*/
Coding (No Output)
A coding function (or method) may have no output (e.g., in Java this is called void).
function printSquareOfNumber(x) {
	let square = x * x;
	println("Square: " + square);
}
/*
Inputs:
	x = 7
Outputs:
	None
Task Description:
	Prints square of input x
*/
Coding (No Input)
A coding function (or method) may have no input.

Note that the function can have any combination of inputs and output (e.g., none and none, many and one output, many and none, etc).
function getHelloWorld() {
	return "Hello World";
}
/*
Inputs:
	None
Outputs:
	Returns string "Hello World"
*/
Function Input/Output Options
As shown, a programming function if flexible in that it may have a variety of input and output types.


Examples


For these examples we'll use the simple equation that computes the area of a rectangle.

The inputs are "width" and "height".

The function output is the calculated area.
function computeArea(width, height)

	return width * height
Recall from variables that variables are placeholders.

So now we have a real example where the function has received input values 10 and 2. The function returns 20 (10 times 2).
zzzzzzzzzzzzzzzzzzzzzz10yyyyyy2
function computeArea(width, height)

wwwwwwwwwwwwwwww10vvvvvvv2
        return width * height
This time the function receives the input values of 5 and 3. The function returns 15 (5 times 3).

This is a general function. It can be used for an unlimited number of different variables.
zzzzzzzzzzzzzzzzzzzzzzz5yyyyyy3
function computeArea(width, height)

wwwwwwwwwwwwwwwww5vvvvvvv3
        return width * height