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: common-content/en/module/js1/functions/index.md
+44-32Lines changed: 44 additions & 32 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -3,58 +3,60 @@ title = 'Functions'
3
3
4
4
time = 20
5
5
[objectives]
6
-
1='Define a function in programming'
6
+
1='Explain the purpose of a function in programming'
7
7
2='Evaluate a function expression'
8
-
3='Call a function with an input in REPL'
9
8
[build]
10
9
render = 'never'
11
10
list = 'local'
12
11
publishResources = false
13
12
14
13
+++
15
14
16
-
Now, instead of adding or multiplying numbers, we’ll consider `10.3`.
15
+
When we are writing programs we often find ourselves needing to do the same thing over and over again. Think back to our password checker from the previous sprint: there are lots of places where you need to enter a password!
17
16
18
-
> 🤔 "What is the nearest whole number to `10.3`?"
17
+
We could re-write the code to check a password every time we needed to use it but that wouldn't be very efficient. It would take a long time to write and there's a chance we could make a mistake and introduce a bug. It would be much easier if we could write the code once and reuse it anywhere it was needed.
19
18
20
-
The process of finding the nearest whole number to a decimal number is called **rounding**. So we could rephrase our question as:
19
+
This applies to any repeated process. Let's look at how we can round a decimal to the nearest whole number.
21
20
22
-
> 🤔 "What does the number `10.3`**round** to?”
21
+
### Reusing instructions
23
22
24
-
## ♻️ Reusing instructions
25
-
26
-
There is no operator for rounding the number `10.3` in JavaScript. But we will want to round numbers **again and again**. We should use a {{<tooltiptitle="function">}}A **function** is a reusable set of instructions.{{</tooltip>}}.
23
+
There is no operator for rounding a number in JavaScript, but we will want to round numbers **again and again**. We can use a **function** to do this. A function is a reusable set of instructions.
27
24
28
25
`Math.round` is a function. Because a function is a _reusable_ set of instructions, `Math.round` rounds any number.
29
26
30
27
Functions usually take **inputs** and then **apply their set of instructions to the inputs** to produce an **output**.
31
28
32
-
{{<tabsname="Functions in REPL">}}
33
-
===[[Try it yourself]]===
29
+
{{<notetype="exercise"title="Exercise: Using a function">}}
30
+
Create a new file to work in and add the following line:
34
31
35
-
1. Write `Math.round` in the Node REPL
36
-
1. Hit enter to evaluate our expression
32
+
```js
33
+
console.log(Math.round);
34
+
```
37
35
38
-
The REPL output `[Function: round]` is telling us `Math.round` is a function.
36
+
Take a look at the output in the console:
39
37
40
-
===[[Watch and follow along]]===
41
-

38
+
```console
39
+
[Function: round]
40
+
```
41
+
42
+
This is telling us that `Math.round` is a function.
43
+
44
+
{{</note>}}
42
45
43
-
{{</tabs>}}
44
46
45
-
##📲 Calling a function
47
+
###Calling a function
46
48
47
-
For our function to work, we need Node to read the instructions and {{<tooltiptitle="execute">}}Execution means the computer reads and follows instructions.{{</tooltip>}} them. Write the following in the REPL:
49
+
For our function to work, we need Node to read the instructions and **execute** them. Execution simply means the computer will run the code with the instructions in it. Update your code to add some extra information.
48
50
49
51
```js
50
-
Math.round(10.3);
52
+
console.log(Math.round(10.3));
51
53
```
52
54
53
-
Notice the `(` and `)` brackets after the name of the function and a number inside the brackets. These brackets mean we are calling the function. The number inside the brackets is the **input** we're passing to the function.
55
+
Notice the `(` and `)` brackets after the name of the function and a number inside the brackets. These brackets mean we are **calling** the function. The number inside the brackets is the **input** we're passing to the function.
54
56
55
-
{{<notetitle="Calling a function"type="note">}}
57
+
{{<notetitle="Definition: Calling a function"type="definition">}}
56
58
57
-
**Calling a function** means telling the computer to read the function's instructions and carry out its instructions. When calling a function we can also pass inputs to the function.
59
+
**Calling a function** means telling the computer to read the function's instructions and carry them out. When calling a function we can also pass inputs to the function.
58
60
59
61
{{</note>}}
60
62
@@ -66,16 +68,26 @@ If we type `Math.round(10.3)` then we get the result `10`. So we say that `Math.
66
68
67
69
A **call expression** is an **expression** which **evaluates** to the value returned by the function when it is called. So the expression `Math.round(10.3)` evaluates to the value `10`.
68
70
69
-
If we assign that expression to a variable, or use it in a string, we'll get the value `10`. So we can write:
71
+
If we assign that expression to a variable, or use it in a string, we'll get the value `10`. We can use this value just like any other that we store in a variable.
1. Update your code from earlier to store the result of calling `Math.round(10.3)` in a variable and print the variable using `console.log`
75
+
2. Create a second variable to store the result of `Math.round(4.2)` and print the sum of the two values.
74
76
75
-
or we can write:
77
+
<details>
78
+
<summary>Example solution:</summary>
76
79
77
-
```js
78
-
constroundedValueInAString=`10.3 rounds to ${Math.round(10.3)}`;
79
-
```
80
+
```js {{title="Part 1"}}
81
+
roundedNumber =Math.round(10.3);
80
82
81
-
Both of these instructions **evaluate** the call expression `Math.round(10.3)` to the returned value `10` as soon as the call expression appears. The variable `roundedValue` will have a numeric value `10` (just like if we'd written `const roundedValue = 10;`), and the variable `roundedValueInAString` will have a string value `"10.3 rounds to 10"`.
0 commit comments