mozilla

Revision 651697 of function

  • リビジョンの URL スラグ: Web/JavaScript/Reference/Statements/function
  • リビジョンのタイトル: function
  • リビジョンの ID: 651697
  • 作成日:
  • 作成者: fscholz
  • 現行リビジョン いいえ
  • コメント

このリビジョンの内容

{{JsStatementsQLAlpha()}}

Summary

The function statement declares function with the specified parameters.

You can also define functions using the {{jsxref("Function")}} constructor and a {{jsxref("Operators/function", "function expression")}}.

Syntax

function name([param,[, param,[..., param]]]) {
   [statements]
}
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

A function created with a function declaration is a Function object and has all the properties, methods and behavior of Function objects. See {{jsxref("Function")}} for detailed information on functions.

A function can also be created using an expression (see {{jsxref("Operators/function", "function expression")}})

Functions can be conditionally declared, that is, a function declaration can be nested within an if statement. Some browsers will treat such conditional declarations as an unconditional declaration and create the function whether the condition is true or not. Therefore they should not be used.

By default, functions return undefined. To return any other value, the function must have a {{jsxref("Statements/return", "return")}} statement that specifies the value to return.

Examples

Example: Using function

The following code declares a function that returns the total 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;
}

Specifications

Specification Status Comment
ECMAScript 1st Edition. Standard Initial definition. Implemented in JavaScript 1.0
{{SpecName('ES5.1', '#sec-13', 'Function definition')}} {{Spec2('ES5.1')}}  
{{SpecName('ES6', '#sec-function-definitions', 'Function definitions')}} {{Spec2('ES6')}}  

Browser compatibility

{{ CompatibilityTable() }}

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}
Feature Android Chrome for Android Firefox Mobile (Gecko) IE Mobile Opera Mobile Safari Mobile
Basic support {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }} {{ CompatVersionUnknown() }}

See also

このリビジョンのソースコード

<div>
 {{JsStatementsQLAlpha()}}</div>
<h2 id="Summary">Summary</h2>
<p>The <strong>function statement</strong> declares function with the specified parameters.</p>
<div class="noinclude">
 <p>You can also define functions using the {{jsxref("Function")}} constructor and a {{jsxref("Operators/function", "function expression")}}.</p>
</div>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
function <em>name</em>([<em>param</em>,[, <em>param</em>,[..., <em>param</em>]]]) {
   [<em>statements</em>]
}
</pre>
<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>
<h2 id="Description" name="Description">Description</h2>
<p>A function created with a function declaration is a <code>Function</code> object and has all the properties, methods and behavior of <code>Function</code> objects. See {{jsxref("Function")}} for detailed information on functions.</p>
<p>A function can also be created using an expression (see {{jsxref("Operators/function", "function expression")}})</p>
<p>Functions can be conditionally declared, that is, a function declaration can be nested within an <code>if</code> statement. Some browsers will treat such conditional declarations as an unconditional declaration and create the function whether the condition is true or not. Therefore they should not be used.</p>
<p>By default, functions return <code>undefined</code>. To return any other value, the function must have a {{jsxref("Statements/return", "return")}} statement that specifies the value to return.</p>
<h2 id="Examples">Examples</h2>
<h3 id="Example.3A_Using_function">Example: Using <code>function</code></h3>
<p>The following code declares a function that returns the total 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="brush: js">
function calc_sales(units_a, units_b, units_c) {
   return units_a*79 + units_b * 129 + units_c * 699;
}
</pre>
<h2 id="Specifications">Specifications</h2>
<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>ECMAScript 1st Edition.</td>
   <td>Standard</td>
   <td>Initial definition. Implemented in JavaScript 1.0</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-13', 'Function definition')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td>&nbsp;</td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-function-definitions', 'Function definitions')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td>&nbsp;</td>
  </tr>
 </tbody>
</table>
<h2 id="Browser_compatibility">Browser compatibility</h2>
<p>{{ CompatibilityTable() }}</p>
<div id="compat-desktop">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Chrome</th>
    <th>Firefox (Gecko)</th>
    <th>Internet Explorer</th>
    <th>Opera</th>
    <th>Safari</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<div id="compat-mobile">
 <table class="compat-table">
  <tbody>
   <tr>
    <th>Feature</th>
    <th>Android</th>
    <th>Chrome for Android</th>
    <th>Firefox Mobile (Gecko)</th>
    <th>IE Mobile</th>
    <th>Opera Mobile</th>
    <th>Safari Mobile</th>
   </tr>
   <tr>
    <td>Basic support</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
    <td>{{ CompatVersionUnknown() }}</td>
   </tr>
  </tbody>
 </table>
</div>
<h2 id="See_also" name="See_also">See also</h2>
<ul>
 <li><a href="/en-US/docs/Web/JavaScript/Reference/Functions_and_function_scope" title="JavaScript/Reference/Functions_and_function_scope">Functions and function scope</a></li>
 <li>{{jsxref("Function")}}</li>
 <li>{{jsxref("Operators/function", "function expression")}}</li>
</ul>
このリビジョンへ戻す