-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathfunctions.js
84 lines (57 loc) · 3.45 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
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
/*
------------------------------------------------------------------------------------
Tutorial: Functions in JavaScript
------------------------------------------------------------------------------------
*/
/*
------------------------------------------------------------------------------------
FUNCTIONS: A function is a repeating piece of "Processing" while the input and output changes.
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
EXAMPLE: washing machine which takes dirty clothes as an input, do some processing i.e washing the clothes and returns the washed clothes as an output. Functions are same as washing machine, they take some input, do some processing on that input and then returns that processed value as an output.
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
SYNTAX:-
function functionName(parameterOne, parameterTwo){
some processing code...
}
------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
FUNCTION EXAMPLE - the below 👇 example takes a name as an input and prints hello "passedName" in the console;
------------------------------------------------------------------------------------
*/
function showName(name){
console.log("hello " + name);
}
showName("sourabh"); // will print "hello sourabh" in the console
/*
------------------------------------------------------------------------------------
PARAMETERS & ARGUMENTS:-
1. PARAMETERS: Parameters are the variable names listed inside of the function definition's parenthesis. (for the above showName function the parameter is "name" listed inside the parenthesis.)
2. ARGUMENTS: Arguments are the actual values passed to a function when it is called. (for the above function showName the argument is "sourabh" which is passed to the function "showName" when called.)
------------------------------------------------------------------------------------
*/
/*
------------------------------------------------------------------------------------
RETURN VALUE & UNDEFINED:-
A funtion which do not returns something returns a special value "undefined".
Also we can assign the returned value of a function as a value in a variable identifier.
------------------------------------------------------------------------------------
*/
console.log(showName("Tanay pratap")); // will print "undefined" in the console because showName is not returning any value
// Returning value from the function
function add(num1, num2){
return num1 + num2;
}
console.log(add(5, 6)); // will print 11 in the console
/*
------------------------------------------------------------------------------------
Challenge: create a function "welcomeUser" which will take username as an input and returns "Welcome " + passed username as an output.
-> your function name should be "welcomeUser".
-> parameter name should be "userName".
-> return Welcome + passed username value as an return value.
-> pass your name as an argument to the "welcomeUser" function.
-> store that return value in a new variable named as "greeting".
-> use console.log() to show the greeting value.
------------------------------------------------------------------------------------
*/