Thursday, 3 January 2013

Previous Interview Question Next Interview Question
Home

What is a function in javascript

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 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:
function square (number)
{
 var result = number * number;
 return result;
}
In this "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:
x = 5;
y = square (x);
The first command assigns a value of 5 to the variable 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.

Previous Interview Question Next Interview Question
Home

0 comments :

Post a Comment