A JavaScript function is a "recipe" of instructions (i.e., statements
or commands) whose purpose is to accomplish a well-defined task. When
the JavaScript interpreter (i.e., your browser) executes a function, it
processes these instructions, one by one, until there are no more
instructions in the function to execute.*

Some functions can be thought of as "processing units" that accept data from one end and produce results at the other. A function's input is called its

For example, a JavaScript function whose purpose is to compute the square of a number might be defined as follows:

The above

Some functions can be thought of as "processing units" that accept data from one end and produce results at the other. A function's input is called its

**parameters**; its output is called the**return value**.For example, a JavaScript function whose purpose is to compute the square of a number might be defined as follows:

In this "

`function square (number) { var result = number * number; return result; }`

`square`

" function, the parenthesized variable "`number`

" is called a parameter. It represents the input, or the value to be squared. The output of `square`

is the value of the expression following the `return`

statement -- in this case, the contents of the `result`

variable, which is equal to `number`

squared. This function fulfills its role by multiplying the input value by itself and returning the result.The above

**function definition**, by itself, accomplishes nothing. In order for it to be useful, it must be put to work. The following example shows how to use our new`square`

function:The first command assigns a value of 5 to the variable

`x = 5; y = square (x);`

`x`

. The second command uses `x`

's value, still 5, as a parameter to the `square`

function. The value returned by the function, 25, is then assigned to the variable `y`

.
## 0 comments :

## Post a Comment