Skip to content

Commit 12cef39

Browse files
author
Aaron O'Mullan
committed
Add chapter on functions
1 parent ac6fe15 commit 12cef39

File tree

4 files changed

+131
-1
lines changed

4 files changed

+131
-1
lines changed

SUMMARY.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,7 @@
1919
* [Concatenate](conditional/concatenate.md)
2020
* [Loops](loops/README.md)
2121
* [For](loops/for.md)
22-
* [While](loops/while.md)
22+
* [While](loops/while.md)
23+
* [Functions](functions/README.md)
24+
* [Declare](functions/declare.md)
25+
* [Higher order](functions/higher_order.md)

functions/README.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Functions
2+
3+
Functions, are one of the most powerful and essential notions in programming.
4+
5+
Functions like mathematical functions perform transformations, they take input values called **arguments** and **return** an output value.
6+

functions/declare.md

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Declaring Functions
2+
3+
Functions like variables must be declared. Lets declare a function `double` which accepts an **argument** called `x` and **returns** the double of x :
4+
5+
```javascript
6+
function double(x) {
7+
return 2 * x;
8+
}
9+
```
10+
11+
Functions are also values in JavaScript, they can be stored in variables (just like numbers, strings, etc ...) and given to other functions as arguments :
12+
13+
```javascript
14+
var double = function(x) {
15+
return 2 * x;
16+
};
17+
```
18+
19+
---
20+
21+
Declare a function named `triple` that takes and argument and returns it's triple.
22+
23+
```js
24+
25+
```
26+
27+
```js
28+
var triple = function(x) {
29+
return x * 3;
30+
}
31+
```
32+
33+
```js
34+
assert(triple);
35+
assert(triple(4) === 12);
36+
assert(triple(10) === 30);
37+
```
38+
39+
---
40+

functions/higher_order.md

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
# Higher Order Functions
2+
3+
Higher order functions, are functions that accept other functions as arguments or return a function it generates.
4+
5+
This is a *fancy* and extremely powerful construct available in JavaScript and other high-level languages (python, lisp, ...)
6+
7+
Now lets pass our `double` function as an argument to another function called `transform` :
8+
9+
```javascript
10+
// Our double function
11+
var double = function(x) {
12+
return 2 * x;
13+
};
14+
15+
// A function that accepts two arguments
16+
// first the function to call
17+
// second the value to call it on
18+
var transform = function(func, value) {
19+
return func(value);
20+
}
21+
22+
// This does the same as double(3)
23+
transform(double, 3);
24+
```
25+
26+
The above example was intentionally simple, having functions as values grants enormous flexibility and allows for many powerful constructs.
27+
28+
Now lets create a function called `buildMultiplier` that takes a number `x` and returns a function that multiplies it's argument by `x` :
29+
30+
```javascript
31+
var buildMultiplier = function(x) {
32+
return function(y) {
33+
return x * y;
34+
}
35+
}
36+
37+
var double = buildMultilplier(2);
38+
var triple = buildMultilplier(3);
39+
40+
double(3); // => 6
41+
triple(3); // => 9
42+
```
43+
44+
---
45+
46+
Define a function named `negate` that takes `add1` as argument and returns a function, that returns the opposite of the value returned by `add`. (Things get a bit more complicated ;) )
47+
48+
```js
49+
var add1 = function (x) {
50+
return x + 1;
51+
};
52+
53+
var negate = function(func) {
54+
// TODO
55+
};
56+
57+
// Should return -6
58+
// Because (5+1) * -1 = -6
59+
negate(add1)(5);
60+
61+
```
62+
63+
```js
64+
var add1 = function (x) {
65+
return x + 1;
66+
}
67+
68+
var negate = function(func) {
69+
return function(x) {
70+
return -1 * func(x);
71+
}
72+
}
73+
74+
negate(add1)(5);
75+
```
76+
77+
```js
78+
assert(negate(add1)(5) === -6);
79+
```
80+
81+
---

0 commit comments

Comments
 (0)