Skip to content

Commit d240971

Browse files
committed
update functions notes
1 parent 4ece458 commit d240971

2 files changed

Lines changed: 44 additions & 32 deletions

File tree

common-content/en/module/js1/functions/index.md

Lines changed: 44 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -3,58 +3,60 @@ title = 'Functions'
33

44
time = 20
55
[objectives]
6-
1='Define a function in programming'
6+
1='Explain the purpose of a function in programming'
77
2='Evaluate a function expression'
8-
3='Call a function with an input in REPL'
98
[build]
109
render = 'never'
1110
list = 'local'
1211
publishResources = false
1312

1413
+++
1514

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!
1716

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.
1918

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.
2120

22-
> 🤔 "What does the number `10.3` **round** to?”
21+
### Reusing instructions
2322

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 {{<tooltip title="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.
2724

2825
`Math.round` is a function. Because a function is a _reusable_ set of instructions, `Math.round` rounds any number.
2926

3027
Functions usually take **inputs** and then **apply their set of instructions to the inputs** to produce an **output**.
3128

32-
{{<tabs name="Functions in REPL">}}
33-
===[[Try it yourself]]===
29+
{{<note type="exercise" title="Exercise: Using a function">}}
30+
Create a new file to work in and add the following line:
3431

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+
```
3735

38-
The REPL output `[Function: round]` is telling us `Math.round` is a function.
36+
Take a look at the output in the console:
3937

40-
===[[Watch and follow along]]===
41-
![rounding](round.gif "[Function: round] indicates that Math.round is a function")
38+
```console
39+
[Function: round]
40+
```
41+
42+
This is telling us that `Math.round` is a function.
43+
44+
{{</note>}}
4245

43-
{{</tabs>}}
4446

45-
## 📲 Calling a function
47+
### Calling a function
4648

47-
For our function to work, we need Node to read the instructions and {{<tooltip title="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.
4850

4951
```js
50-
Math.round(10.3);
52+
console.log(Math.round(10.3));
5153
```
5254

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.
5456

55-
{{<note title="Calling a function" type="note">}}
57+
{{<note title="Definition: Calling a function" type="definition">}}
5658

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.
5860

5961
{{</note>}}
6062

@@ -66,16 +68,26 @@ If we type `Math.round(10.3)` then we get the result `10`. So we say that `Math.
6668

6769
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`.
6870

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.
7072

71-
```js
72-
const roundedValue = Math.round(10.3);
73-
```
73+
{{<note type="exercise" title="Exercise: Calling functions">}}
74+
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.
7476

75-
or we can write:
77+
<details>
78+
<summary>Example solution:</summary>
7679

77-
```js
78-
const roundedValueInAString = `10.3 rounds to ${Math.round(10.3)}`;
79-
```
80+
```js {{title="Part 1"}}
81+
roundedNumber = Math.round(10.3);
8082

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"`.
83+
console.log(roundedNumber);
84+
```
85+
86+
```js {{title="Part 2"}}
87+
firstRoundedNumber = Math.round(10.3);
88+
secondRoundedNumber = Math.round(4.2);
89+
90+
console.log(firstRoundedNumber + secondRoundedNumber);
91+
```
92+
</details>
93+
{{</note>}}
-586 KB
Binary file not shown.

0 commit comments

Comments
 (0)