-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path5-functions.js
executable file
·337 lines (227 loc) · 7.89 KB
/
5-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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*FUNCTIONS
Functions can be created using function declaration and function expression
In function declaration, the function is created using function name.
NB: These functions can be called before their declaration.
In function expression, the function is created by assigning it's imprementation to a variable.
The name of the fuction is optional and the function is called using the variable with parentesis added.
NB: They cannot be called before their declaration
NB: Functions can be redefined and also can be called before their declaration appears in the program.
*/
//function declaration with two parameters and return statement
function add(a, b)
{
return a + b;
}
//function declaration with one parameter no return statement
function print(func)
{
console.log(func);
}
//function call
print(add(5,7));
mul(4, 8);
function mul(r, t)
{
return r * t;
}
// function expression
const myfunction = function()
{
console.log("anonymous function called");
return 5+25;
}
console.log(myfunction()); // output message and return value.
// function expression with a parameter
let func2 = function (m)
{
return m ** 2;
}
console.log(func2(20));
function add(a, b)
{
return a * a * a;
}
/** Using strict mode in functions
* put the string 'use strict' on the first line of the function body.
*/
function strictFunc()
{
'use strict';
const message = "hello";
console.log(message);
}
strictFunc(); // outputs hello world because the function has been modified below
function strictFunc()
{
console.log("hello world");
}
strictFunc(); //outputs hellow world
/**
* CALLING FUNCTIONS WITH FEWER PARAMETERS THAN EXPECTED
Funcions can be called with fewer arguments than parameter defined. In this case, parameters without arguments behave like an uninitialised local variable.It value is undefined.
*/
function listItems(a,b,c,d)
{
console.log(a,b,c,d);
}
listItems(4,5); //output 4 5 undefined undefined
/*Due to this feature of javascript, you need to always check if arguments are passed to the function before using the parameters with the function.
This can be done by checking to see if the value of the parameter is not equal to undefined
*/
function listItemsSafe(a, b, c)
{
if(a !== undefined)
console.log(a);
if (b !== undefined)
console.log(b);
if (c !== undefined)
console.log(c);
}
listItemsSafe(5,3) //output 5 \n 3
/*
**CALLING FUNCTIONS WITH MORE ARGUMENTS THAN EXPECTED
It is possible to call functions with more arguments than expected.
In this case, the excess arguments is not available in the function.
There is still a way to access the excess arguments. The @arguments object is an array-like object that contains all passed arguments and so every argument can be accessed by using array index notation. We can use argements.length method to find the number of args passed.
NB: The arguments object does not have all the method available in an array
*/
/*output formatting : writing without \n character to the console */
process.stdout.write("343");
process.stdout.write("now");// output 343now
function listExcess(a, b, c)
{
console.log(a,b,c);
if (arguments.length > 3)
{
for (i = 3; i < arguments.length; i++)
console.log(arguments[i]);
}
}
console.log("listExcess:");
listExcess(4,5,1,0,7); //output 4 5 1 \n 0 \n 7
/**
* USING REST PARAMETERS
* Since ES2015, rest parameters provide a better way to use excess arguments within a function. Rest parameters contains all excess arguments passed to the function.
*
* Rest parameters is an array of remaining arguments without parameters and they can be accessed via an index.
*
*
* syntax: function(args1, args2, ...restArgs)
* {
* //function body
* }
*
*NB: the rest parameter container is an array which contains ONLY the arguments to which there are no corresponding parameters.
The length property can be used to check whether the length of the array to indicate whether excess arguments were passed or not.
rest parameters can be accessed through index notation since it's a proper array.
*
*
*/
function showCourses(course1, course2, ...remainingCourses)
{
console.log(course1);
console.log(course2);
if (remainingCourses.length > 0)
{
let i = 0;
while (i < remainingCourses.length)
{
console.log(remainingCourses[i]);
i++;
}
}
}
showCourses("DCIT 201", "DCIT 205", "DCIT 209", "DCIT 207");
/*output
DCIT 201
DCIT 205
DCIT 209 = restArgs[0]
DCIT 207 = restArgs[1]
*/
/* VARIADIC FUNCTIONS
These are functions with varied number of parameters.
rest parameters can be used to easily implement variadic functions.
An array can also be used to implement variadic fuctions
*/
function displayMenu(...foodItems)
{
//making sure at least one arg was passed
if (foodItems.length === 0)
{
return;
}
let i = 0;
while (i < foodItems.length)
{
console.log(foodItems[i]);
i++;
}
}
displayMenu("Banku","Tuo-zaafi"); // prints Banku \n Tuo-zaafi
displayMenu(); //prints nothing
/*DEFINING FUNCTIONS WITHIN FUNCTIONS
*
*Unlike other languages like Java or C++, JavaScript allow functions to be defined inside other functions.
These inner functions can ONLY be called inside the function it was defined since that is their scope.
NB: The inner functions are recreated anytime we call the other function.
*/
function differenceOfTWoSquares(x,y)
{
function add()
{
return x + y;
}
function sub()
{
return x - y;
}
console.log(add() * sub());
}
differenceOfTWoSquares(3,2); //output 5
/*USING ELEMENTS FROM AN ARRAY AS A PARAMETER
This can be achieved by passing in each element by using index notation.
i.e fuc(array[i]);
*Using Spread operator
The spread operator helps you to use array elements directly as arguments. The array elements are spread over the parameters
syntax:
declare:
func(item,item,item)
{
console.log(item,item,item);
}
call:
func(...array);
It is achieved by preceding the array name with 3 dots as an argument.
*/
let evenNumbers = [2, 4, 6, 8, 10];
function printArray(element1, element2, element3, ...elements)
{
//this prints elements excluding rest Parameters
console.log(element1,element2,element3);
//this print rest parameters
if (elements.length > 0)
{
let i = 0;
while (i < elements.length)
{
console.log(elements[i]);
i++;
}
}
}
printArray(...evenNumbers);
//output 2 4 6 \n 8 \n 10
/*DEFINING FUNCTIONS USING SHORT NOTATIONS
Function expressions can be defined in a short way using arrow functions.
syntax:
variable = (parameters) => {function body}
If the function has only one parameter, the parenthesis around the parameters can be omitted.
Also if the body of the function is made up of one statement, the curly braces can be omited. typeScript prevents this tho
syntax:
variable = parameter => x + y
*/
const arrowFunc1 = (item1, item2) =>
{
return item1 / item2
}
console.log( arrowFunc1(100, 10)); //output 10