-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
54 lines (38 loc) · 1.25 KB
/
functions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
// const num1 = 10;
// const num2 = 30;
// const sum1 = num1 + num2;
// console.log(sum1);
// const num3 = 70;
// const num4 = 100;
// const sum2 = num3 + num4;
// console.log(sum2);
// ---------------------------
// functions give us the ability to avoid writing code like above ☝
// we can reuse the same behavior yet allow the values being calculated
// to be different
// function definition example
function displayWordsToScreen(word1, word2) {
// word1 and word2 are parameters in this function
console.log(word1, word2);
}
// function invocation / execute examples
// "something" and "else" are arguments being passed into the function
displayWordsToScreen("something", "else");
displayWordsToScreen("number 1");
displayWordsToScreen("hello");
displayWordsToScreen("😁");
// function return values
function add(parameterA, parameterB) {
// const sum = parameterA + parameterB;
// return sum;
// the line below and the two lines above are equivalent
return parameterA + parameterB;
}
// here we add 1 and 2 together and store the result in newSum
const newSum = add(1, 2);
// the following two lines are equivalent
console.log(newSum);
console.log(add(1, 2));
const newSum2 = add(-90, 10);
console.log(newSum2);
console.log(add(-90, 10));