mozilla

Revision 118069 of Defining Functions

  • Revision slug: JavaScript/Guide/Obsolete_Pages/Defining_Functions
  • Revision title: Defining Functions
  • Revision id: 118069
  • Created:
  • Creator: Mgjbot
  • Is current revision? No
  • Comment robot Modifying: [[pl:Przewodnik po języku JavaScript 1.5:Definiowanie funkcji]]

Revision Content

Defining Functions

A function definition consists of the function keyword, followed by

  • The name of the function.
  • A list of arguments to the function, enclosed in parentheses and separated by commas.
  • The JavaScript statements that define the function, enclosed in curly braces, { }. The statements in a function can include calls to other functions defined in the current application.

For example, the following code defines a simple function named square:

function square(number) {
   return number * number;
}

The function square takes one argument, called number. The function consists of one statement that indicates to return the argument of the function multiplied by itself. The return statement specifies the value returned by the function.

return number * number

All parameters are passed to functions by value; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function. However, if you pass an object as a parameter to a function and the function changes the object's properties, that change is visible outside the function, as shown in the following example:

function myFunc(theObject) {
   theObject.make="Toyota"
}

mycar = {make:"Honda", model:"Accord", year:1998};
x=mycar.make;     // returns Honda
myFunc(mycar);    // pass object mycar to the function
y=mycar.make;     // returns Toyota (prop was changed by the function)

A function can be defined based on a condition. For example, given the following function definition:

if (num == 0)
{
   function myFunc(theObject) {
   theObject.make="Toyota"
   }
}

the myFunc function is only defined if the variable num equals 0. If num does not equal 0, the function is not defined, and any attempt to execute it will fail.

In addition to defining functions as described here, you can also define Function objects.

A method is a function associated with an object. You'll learn more about objects and methods in Chapter 8, "Working with Objects."

A function can also be defined inside an expression. This is called a function expression. Typically such a function is anonymous; it does not have to have a name. For example, the function square could have been defined as:

const square = function(number) {return number * number};

This is convenient when passing a function as an argument to another function. The following example shows the map function being defined and then called with an anonymous function as its first parameter:

function map(f,a) {
   var result=new Array;
   for (var i = 0; i != a.length; i++)
      result[i] = f(a[i]);
   return result;
}

The call

map(function(x) {return x * x * x}, [0, 1, 2, 5, 10]);

returns {{mediawiki.external('0, 1, 8, 125, 1000')}}.

{{template.PreviousNext("Core_JavaScript_1.5_Guide:Exception_Handling_Statements:try...catch_Statement", "Core_JavaScript_1.5_Guide:Calling_Functions")}}

{{ wiki.languages( { "fr": "fr/Guide_JavaScript_1.5/D\u00e9finition_de_fonctions", "ja": "ja/Core_JavaScript_1.5_Guide/Defining_Functions", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Definiowanie_funkcji" } ) }}

Revision Source

<p>
</p>
<h3 name="Defining_Functions"> Defining Functions </h3>
<p>A function definition consists of the function keyword, followed by
</p>
<ul><li> The name of the function.
</li><li> A list of arguments to the function, enclosed in parentheses and separated by commas.
</li><li> The JavaScript statements that define the function, enclosed in curly braces, { }. The statements in a function can include calls to other functions defined in the current application.
</li></ul>
<p>For example, the following code defines a simple function named square:
</p>
<pre>function square(number) {
   return number * number;
}
</pre>
<p>The function <code>square</code> takes one argument, called number. The function consists of one statement that indicates to return the argument of the function multiplied by itself. The <code>return</code> statement specifies the value returned by the function.
</p>
<pre>return number * number
</pre>
<p>All parameters are passed to functions <i>by value</i>; the value is passed to the function, but if the function changes the value of the parameter, this change is not reflected globally or in the calling function. However, if you pass an object as a parameter to a function and the function changes the object's properties, that change is visible outside the function, as shown in the following example:
</p>
<pre>function myFunc(theObject) {
   theObject.make="Toyota"
}

mycar = {make:"Honda", model:"Accord", year:1998};
x=mycar.make;     // returns Honda
myFunc(mycar);    // pass object mycar to the function
y=mycar.make;     // returns Toyota (prop was changed by the function)
</pre>
<p>A function can be defined based on a condition. For example, given the following function definition:
</p>
<pre>if (num == 0)
{
   function myFunc(theObject) {
   theObject.make="Toyota"
   }
}
</pre>
<p>the <code>myFunc</code> function is only defined if the variable <code>num</code> equals 0. If <code>num</code> does not equal 0, the function is not defined, and any attempt to execute it will fail.
</p><p>In addition to defining functions as described here, you can also define <a href="en/Core_JavaScript_1.5_Guide/Predefined_Core_Objects/Function_Object">Function objects</a>.
</p><p>A method is a function associated with an object. You'll learn more about objects and methods in <a href="en/Core_JavaScript_1.5_Guide#Working_with_Objects">Chapter 8, "Working with Objects."</a>
</p><p>A function can also be defined inside an expression. This is called a function expression. Typically such a function is anonymous; it does not have to have a name. For example, the function <code>square</code> could have been defined as:
</p>
<pre>const square = function(number) {return number * number};
</pre>
<p>This is convenient when passing a function as an argument to another function. The following example shows the map function being defined and then called with an anonymous function as its first parameter:
</p>
<pre>function map(f,a) {
   var result=new Array;
   for (var i = 0; i != a.length; i++)
      result[i] = f(a[i]);
   return result;
}
</pre>
<p>The call
</p>
<pre>map(function(x) {return x * x * x}, [0, 1, 2, 5, 10]);
</pre>
<p>returns {{mediawiki.external('0, 1, 8, 125, 1000')}}.
</p><p>{{template.PreviousNext("Core_JavaScript_1.5_Guide:Exception_Handling_Statements:try...catch_Statement", "Core_JavaScript_1.5_Guide:Calling_Functions")}}
</p>{{ wiki.languages( { "fr": "fr/Guide_JavaScript_1.5/D\u00e9finition_de_fonctions", "ja": "ja/Core_JavaScript_1.5_Guide/Defining_Functions", "pl": "pl/Przewodnik_po_j\u0119zyku_JavaScript_1.5/Definiowanie_funkcji" } ) }}
Revert to this revision