Revision 53103 of function

  • Revision slug: JavaScript/Reference/Statements/function
  • Revision title: function
  • Revision id: 53103
  • Created:
  • Creator: evilpie
  • Is current revision? No
  • Comment 3 words added, 20 words removed

Revision Content

Summary

Declares a function with the specified parameters.

You can also define functions using the Function constructor and the function operator (function expression).

Statement
Implemented in JavaScript 1.0
ECMAScript Edition ECMA-262

Syntax

function name([param] [, param] [..., param]) {
   statements
}

Parameters

name 
The function name.
param 
The name of an argument to be passed to the function. A function can have up to 255 arguments.
statements 
The statements which comprise the body of the function.

Description

To return a value, the function must have a return statement that specifies the value to return.

A function created with the function statement is a Function object and has all the properties, methods, and behavior of Function objects. See Function for detailed information on functions.

A function can also be declared inside an expression. In this case the function is usually anonymous. See function operator for more information about the function (function expression).

Functions can be conditionally declared. That is, a function definition can be nested within an if statement. Technically, such declarations are not actually function declarations; they are function expressions.

Examples

Example: Using function

The following code declares a function that returns the total dollar amount of sales, when given the number of units sold of products a, b, and c.

function calc_sales(units_a, units_b, units_c) {
   return units_a*79 + units_b * 129 + units_c * 699;
}

See also

Functions, Function, function operator


{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Sentencias/function", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/function", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/function", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/function" } ) }}

Revision Source

<p>
</p>
<h3 name="Summary">Summary</h3>
<p>Declares a function with the specified parameters.</p>
<p>You can also define functions using the <code><a href="/en/JavaScript/Reference/Global_Objects/Function" title="en/JavaScript/Reference/Global_Objects/Function">Function</a></code> constructor and the <code><a href="/en/JavaScript/Reference/Operators/Special/function" title="en/JavaScript/Reference/Operators/Special/function">function operator</a></code> (function expression).</p>
<table class="standard-table"> <tbody> <tr> <td class="header" colspan="2">Statement</td> </tr> <tr> <td>Implemented in</td> <td>JavaScript 1.0</td> </tr> <tr> <td>ECMAScript Edition</td> <td>ECMA-262</td> </tr> </tbody>
</table><h3 name="Syntax"> Syntax </h3>
<pre class="eval">function <i>name</i>([<i>param</i>] [, <i>param</i>] [..., <i>param</i>]) {
   <i>statements</i>
}
</pre>
<h3 name="Parameters"> Parameters </h3>
<dl><dt> <code>name</code> </dt><dd> The function name.
</dd></dl>
<dl><dt> <code>param</code> </dt><dd> The name of an argument to be passed to the function. A function can have up to 255 arguments.
</dd></dl>
<dl><dt> <code>statements</code> </dt><dd> The statements which comprise the body of the function.
</dd></dl>
<h3 name="Description"> Description </h3>
<p>To return a value, the function must have a <code><a href="en/Core_JavaScript_1.5_Reference/Statements/return">return</a></code> statement that specifies the value to return.
</p><p>A function created with the <code>function</code> statement is a <code>Function</code> object and has all the properties, methods, and behavior of <code>Function</code> objects. See <code><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Function">Function</a></code> for detailed information on functions.
</p><p>A function can also be declared inside an expression. In this case the function is usually anonymous. See <code><a href="en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/function_Operator">function operator</a></code> for more information about the <code>function</code> (function expression).
</p><p>Functions can be conditionally declared. That is, a function definition can be nested within an <code>if</code> statement. Technically, such declarations are not actually function declarations; they are function expressions.
</p>
<h3 name="Examples"> Examples </h3>
<h4 name="Example:_Using_function"> Example: Using <code>function</code> </h4>
<p>The following code declares a function that returns the total dollar amount of sales, when given the number of units sold of products <code>a</code>, <code>b</code>, and <code>c</code>.
</p>
<pre class="eval">function calc_sales(units_a, units_b, units_c) {
   return units_a*79 + units_b * 129 + units_c * 699;
}
</pre>
<h3 name="See_also"> See also </h3>
<p><code><a href="en/Core_JavaScript_1.5_Reference/Functions">Functions</a></code>,
<code><a href="en/Core_JavaScript_1.5_Reference/Global_Objects/Function">Function</a></code>,
<code><a href="en/Core_JavaScript_1.5_Reference/Operators/Special_Operators/function_Operator">function operator</a></code>
</p><p><br>
</p>
<div class="noinclude">
</div>
{{ languages( { "es": "es/Referencia_de_JavaScript_1.5/Sentencias/function", "fr": "fr/R\u00e9f\u00e9rence_de_JavaScript_1.5_Core/Instructions/function", "ja": "ja/Core_JavaScript_1.5_Reference/Statements/function", "pl": "pl/Dokumentacja_j\u0119zyka_JavaScript_1.5/Polecenia/function" } ) }}
Revert to this revision