aboutsummaryrefslogtreecommitdiff
path: root/files/ca/web/javascript/reference/statements/function/index.html
blob: 207c3b0af897760015cd89a8cb2ccd05f6142a06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
---
title: function
slug: Web/JavaScript/Reference/Statements/function
translation_of: Web/JavaScript/Reference/Statements/function
original_slug: Web/JavaScript/Referencia/Sentencies/function
---
<div>
<div>{{jsSidebar("Statements")}}</div>
</div>

<h2 id="Resum">Resum</h2>

<p>La <strong>declaració d'una funció</strong> defineix una funció amb uns paràmetres especificats.</p>

<div class="noinclude">
<p>També podeu definir funcions fent servir el constructor {{jsxref("Function")}} i un {{jsxref("Operators/function", "function expression")}}.</p>
</div>

<h2 id="Syntax" name="Syntax">Sintaxi</h2>

<pre class="syntaxbox">function <em>nom</em>([<em>paràm</em>,[, <em>paràm</em>,[..., <em>paràm</em>]]]) {
   [<em>sentències</em>]
}
</pre>

<dl>
 <dt><code>nom</code></dt>
 <dd>El nom de la funció.</dd>
</dl>

<dl>
 <dt><code>paràm</code></dt>
 <dd>El nom d'un argument que se li passarà a la funció. Una funció pot arribar a tenir fins a 255 arguments.</dd>
</dl>

<dl>
 <dt><code>sentències</code></dt>
 <dd>Les sentències que comprenen el cos de la funció.</dd>
</dl>

<h2 id="Description" name="Description">Descripció</h2>

<p>Una funció creada amb una declaració d'una funció és un objecte <code>Function</code> i té totes les propietats, mètodes i comportament dels objectes <code>Function</code>. Vegeu {{jsxref("Function")}} per informació detallada sobre funcions.</p>

<p>Una funció també es pot crear fent servir una expressió (vegeu {{jsxref("Operators/function", "function expression")}}).</p>

<p>Per defecte, les funcions retornen <code>undefined</code>. Per tal de retornar qualsevol altre valor, la funció ha de tenir una sentència {{jsxref("Statements/return", "return")}} que especifiqui el valor que retorna.</p>

<h3 id="Funcions_creades_de_forma_condicional">Funcions creades de forma condicional</h3>

<p>Les funcions poden ser declarades de forma condicional, és a dir, una sentència d'una funció pot estar aniuada dins d'una sentència <code>if</code>. La majoria de navegadors que no siguin Mozilla tractaran aquestes declaracions condicionals com a declaracions incondicionals i crearàn la funció tant si la condició és vertadera o falsa, vegeu <a href="http://kangax.github.io/nfe/#function-statements">aquest article</a> per una visió general. Per tant, no s'haurien de fer servir, per creacions condicionals feu servir expressions de funcions.</p>

<h3 id="Declarar_les_funcions_abans_de_definir-les_(hosting)">Declarar les funcions abans de definir-les <em>(hosting)</em></h3>

<p><u>Function declarations in JavaScript are hoisting the function definition</u>. En Javascript es pot ser cridar una funció abans de declarar-la. En anglès existeix un ver:</p>

<pre class="brush: js">hoisted(); // logs "foo"

function hoisted() {
  console.log("foo");
}
</pre>

<p>Vegeu que {{jsxref("Operators/function", "function expressions")}} no estan hoisted:</p>

<pre class="brush: js">notHoisted(); // TypeError: notHoisted is not a function

var notHoisted = function() {
   console.log("bar");
};
</pre>

<h2 id="Exemples">Exemples</h2>

<h3 id="Exemple_Fer_servir_function">Exemple: Fer servir <code>function</code></h3>

<p>El codi següent declara una funció que retorna la quantitat total de vendes, quan se li dóna el nombre d'unitat venudes d' <code>a</code>, <code>b</code>, i <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="Especificacions">Especificacions</h2>

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Especificació</th>
   <th scope="col">Estat</th>
   <th scope="col">Comentaris</th>
  </tr>
  <tr>
   <td>1a edició de ECMAScript.</td>
   <td>Estàndard</td>
   <td>Definició iniciañ. Implementat en JavaScript 1.0</td>
  </tr>
  <tr>
   <td>{{SpecName('ES5.1', '#sec-13', 'Function definition')}}</td>
   <td>{{Spec2('ES5.1')}}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName('ES6', '#sec-function-definitions', 'Function definitions')}}</td>
   <td>{{Spec2('ES6')}}</td>
   <td> </td>
  </tr>
 </tbody>
</table>

<h2 id="Compatibilitat_amb_navegadors">Compatibilitat amb navegadors</h2>

<p>{{ CompatibilityTable() }}</p>

<div id="compat-desktop">
<table class="compat-table">
 <tbody>
  <tr>
   <th>Característica</th>
   <th>Chrome</th>
   <th>Firefox (Gecko)</th>
   <th>Internet Explorer</th>
   <th>Opera</th>
   <th>Safari</th>
  </tr>
  <tr>
   <td>Suport bàsic</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>Característica</th>
   <th>Android</th>
   <th>Chrome per Android</th>
   <th>Firefox Mobile (Gecko)</th>
   <th>IE Mobile</th>
   <th>Opera Mobile</th>
   <th>Safari Mobile</th>
  </tr>
  <tr>
   <td>Suport bàsic</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">Vegeu també</h2>

<ul>
 <li>{{jsxref("Functions_and_function_scope", "Functions and function scope")}}</li>
 <li>{{jsxref("Function")}}</li>
 <li>{{jsxref("Operators/function", "function expression")}}</li>
 <li>{{jsxref("Statements/function*", "function* statement")}}</li>
 <li>{{jsxref("Operators/function*", "function* expression")}}</li>
 <li>{{jsxref("GeneratorFunction")}}</li>
</ul>