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: 07_Day/07_day_functions.md
+24-23Lines changed: 24 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -17,7 +17,7 @@
17
17
18
18

19
19
20
-
-[📔 Day 7](#%f0%9f%93%94-day-7)
20
+
-[📔 Day 7](#-day-7)
21
21
-[Functions](#functions)
22
22
-[Function Declaration](#function-declaration)
23
23
-[Function without a parameter and return](#function-without-a-parameter-and-return)
@@ -34,7 +34,7 @@
34
34
-[Arrow Function](#arrow-function)
35
35
-[Function with default parameters](#function-with-default-parameters)
36
36
-[Function declaration versus Arrow function](#function-declaration-versus-arrow-function)
37
-
-[💻 Exercises](#%f0%9f%92%bb-exercises)
37
+
-[💻 Exercises](#-exercises)
38
38
-[Exercises: Level 1](#exercises-level-1)
39
39
-[Exercises: Level 2](#exercises-level-2)
40
40
-[Exercises: Level 3](#exercises-level-3)
@@ -79,12 +79,13 @@ Function can be declared without a parameter.
79
79
**Example:**
80
80
81
81
```js
82
-
// function without parameter, a function which square a number
82
+
// function without parameter, a function which make a number square
83
83
functionsquare() {
84
84
let num =2
85
85
let sq = num * num
86
86
console.log(sq)
87
87
}
88
+
88
89
square() // 4
89
90
90
91
// function without parameter
@@ -95,33 +96,35 @@ function addTwoNumbers() {
95
96
96
97
console.log(sum)
97
98
}
98
-
addTwoNumbers() // function has to be called to be executed by it name
99
+
100
+
addTwoNumbers() // a function has to be called by its name to be executed
99
101
```
100
102
101
103
```js
102
-
functiongenerateFullName (){
104
+
functionprintFullName (){
103
105
let firstName ='Asabeneh'
104
106
let lastName ='Yetayeh'
105
107
let space =''
106
108
let fullName = firstName + space + lastName
107
109
console.log(fullName)
108
110
}
109
-
generateFullName() // calling a function
111
+
112
+
printFullName() // calling a function
110
113
```
111
114
112
115
### Function returning value
113
116
114
117
Function can also return values, if a function does not return values the value of the function is undefined. Let us write the above functions with return. From now on, we return value to a function instead of printing it.
115
118
116
119
```js
117
-
functiongenerateFullName (){
120
+
functionprintFullName (){
118
121
let firstName ='Asabeneh'
119
122
let lastName ='Yetayeh'
120
123
let space =''
121
124
let fullName = firstName + space + lastName
122
125
return fullName
123
126
}
124
-
console.log(generateFullName())
127
+
console.log(printFullName())
125
128
```
126
129
127
130
```js
@@ -170,19 +173,20 @@ function functionName(parm1, parm2) {
170
173
//code goes her
171
174
}
172
175
functionName(parm1, parm2) // during calling or invoking two arguments needed
173
-
// Function without parameter doesn't take input, so lets make a parameter with parameter
176
+
// Function without parameter doesn't take input, so lets make a function with parameters
174
177
functionsumTwoNumbers(numOne, numTwo) {
175
178
let sum = numOne + numTwo
176
179
console.log(sum)
177
180
}
178
181
sumTwoNumbers(10, 20) // calling functions
179
182
// If a function doesn't return it doesn't store data, so it should return
180
-
functionsumTwoNumbersAndReturn(numOne, numTwo) {
183
+
184
+
functionsumTwoNumbers(numOne, numTwo) {
181
185
let sum = numOne + numTwo
182
186
return sum
183
187
}
184
188
185
-
console.log(sumTwoNumbersAndReturn(10, 20))
189
+
console.log(sumTwoNumbers(10, 20))
186
190
functionprintFullName(firstName, lastName) {
187
191
return`${firstName}${lastName}`
188
192
}
@@ -222,7 +226,7 @@ console.log(areaOfCircle(10))
222
226
223
227
### Function with unlimited number of parameters
224
228
225
-
Sometimes we do not know how many arguments the user going to pass. Therefore, we should know how to write a function which can take unlimited number of arguments. The way we do it has a significant difference between a function declaration(regular function) and arrow function.Let us see examples both in function declaration and arrow function.
229
+
Sometimes we do not know how many arguments the user going to pass. Therefore, we should know how to write a function which can take unlimited number of arguments. The way we do it has a significant difference between a function declaration(regular function) and arrow function.Let us see examples both in function declaration and arrow function.
226
230
227
231
#### Unlimited number of parameters in regular function
Expression functions are anonymous functions. After we create a function without a name and with assign it to a variable. To return a value from the function we should call the variable. Look at the example below.
311
+
Expression functions are anonymous functions. After we create a function without a name and we assign it to a variable. To return a value from the function we should call the variable. Look at the example below.
308
312
309
313
```js
310
-
//Declaration function
311
-
functionsquare(n) {
312
-
return n * n
313
-
}
314
-
315
-
console.log(square(2)) // -> 4
316
314
317
315
// Function expression
318
316
constsquare=function(n) {
@@ -342,7 +340,7 @@ console.log(squaredNum)
342
340
343
341
Arrow function is an alternative to write a function, however function declaration and arrow function have some minor differences.
344
342
345
-
Arrow function uses arrow instead of the keyword function to declare a function. Let us see both function declaration and arrow function.
343
+
Arrow function uses arrow instead of the keyword *function* to declare a function. Let us see both function declaration and arrow function.
346
344
347
345
```js
348
346
// This is how we write normal or declaration function
@@ -357,9 +355,10 @@ const square = n => {
357
355
return n * n
358
356
}
359
357
360
-
console.log(square(2)) // -> 4
361
-
// if we have only one line, it can be written as follows, explicit return
362
-
constsquare=n=> n * n // -> 4
358
+
console.log(square(2)) // -> 4
359
+
360
+
// if we have only one line in the code block, it can be written as follows, explicit return
361
+
constsquare=n=> n * n // -> 4
363
362
```
364
363
365
364
```js
@@ -503,6 +502,8 @@ console.log('Weight of an object in Newton: ', weightOfObject(100, 1.62)) // gra
503
502
504
503
### Function declaration versus Arrow function
505
504
505
+
It ill be covered in other time
506
+
506
507
🌕 You are a rising star, now you knew function . Now, you are super charged with the power of functions. You have just completed day 7 challenges and you are 7 steps a head in to your way to greatness. Now do some exercises for your brain and for your muscle.
0 commit comments