You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: 1-js/2-first-steps/18-function-basics/article.md
+10-8Lines changed: 10 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
1
-
# Function basics
1
+
# Functions
2
2
3
3
Quite often we need to perform a similar action in many places of the script.
4
4
@@ -10,21 +10,23 @@ Functions are the main "building blocks" of the program. They allow the code to
10
10
11
11
We've already seen examples of built-in functions, like `alert(message)`, `prompt(message, default)` and `confirm(question)`. But we can create functions of our own as well.
12
12
13
-
## Definition
13
+
## Function Declaration
14
14
15
-
An example of a function definition:
15
+
To create a function we can use a *function declaration*.
16
+
17
+
It looks like this:
16
18
17
19
```js
18
20
functionshowMessage() {
19
21
alert( 'Hello everyone!' );
20
22
}
21
23
```
22
24
23
-
The `function` keyword goes first, then follows the *name of the function*, then a list of *parameters* in the brackets (empty in the example above) and finally the code of the function, also named "the function body".
25
+
The `function` keyword goes first, then goes the *name of the function*, then a list of *parameters* in the brackets (empty in the example above) and finally the code of the function, also named "the function body".
24
26
25
27
<imgsrc="function_basics.png">
26
28
27
-
Once defined, the function can be called by it's name.
29
+
Our new function can be called by it's name.
28
30
29
31
For instance:
30
32
@@ -40,9 +42,9 @@ showMessage();
40
42
*/!*
41
43
```
42
44
43
-
The call executes the code of the function. Here we will see the message shown two times.
45
+
The call `showMessage()`executes the code of the function. Here we will see the message two times.
44
46
45
-
In this example we can see one of the main purposes of the functions: to evade code duplication.
47
+
This example clearly demonstrates one of the main purposes of the functions: to evade code duplication.
46
48
47
49
If we ever need to change the message or the way it is shown -- it's enough to modify the code in one place: the function which outputs it.
48
50
@@ -184,7 +186,7 @@ showMessage(from, "Hello");
184
186
alert( from ); // Ann
185
187
```
186
188
187
-
## Default parameter values
189
+
### Default values
188
190
189
191
A function can be called with any number of arguments. If a parameter is not provided, but listed in the declaration, then its value becomes `undefined`.
0 commit comments