Functions

Introduction

A function is a block of statements. It is a unit of reusable code. Function is defined by using the function keyword and followed by function name.

Function definition

function functionName(parameters) 
{
  // statements
}

The code inside a function is executed when the function is invoked.

function product(a, b) 
{
  return a * b;
}
product(10, 2);           // Will return 20

Built-in Functions

abs(x)

Returns the absolute value of x.

ceil(x)

Returns the value of x rounded up to its nearest integer.

floor(x)

Returns the value of x rounded down to its nearest integer.

round(x)

Returns the value of x rounded to its nearest integer.

max(x, y, z, ..., n)

Returns the number with the highest value.

min(x, y, z, ..., n)

Returns the number with the lowest value.

pow(x, y)

Returns the value of x to the power of y.

sqrt(x)

Returns the square root of x.