1+ <!DOCTYPE html>
2+ < html lang ="en ">
3+ < head >
4+ < meta charset ="UTF-8 ">
5+ < meta name ="viewport " content ="width=device-width, initial-scale=1.0 ">
6+ < title > Functions</ title >
7+ </ head >
8+ < body >
9+ < script >
10+ /*
11+ # Functions:
12+ - A set/block of code which is intended to peform a particular task
13+ - Function can be executed as many number of times as needed
14+ - Advantags of functions:
15+ - Avoid repeated code (redundancy)
16+ - DRY (Don't Repeat Yourself) principle
17+ - Modular code (Organized code)
18+
19+ - For any function to be executed in JS, it needs two steps:
20+ - Declaration
21+ - Invokation (Calling the function)
22+
23+ - Type of function:
24+ - Standard function
25+ - Anonymous function
26+ - Does not have a name
27+ - Used as a function expression
28+
29+ - Syntax:
30+ - Declaration
31+ - STANDARD FUNCTION
32+ function name() {
33+ // code
34+ }
35+ - ANONYMOUS FUNCTION
36+ const name = function() {
37+ // code
38+ }
39+ - Invokation
40+ name()
41+
42+ - Parameters and arguments:
43+ - Can make a function dynamic (For different I/P, you get different O/p)
44+ - Parameters: Function variables (Values expected by the function)
45+ - Arguments: Actual values passed for function paramaters during invokation
46+ - Syntax:
47+ - Declaration:
48+ function fun1(a, b) { // Parameters
49+ // code
50+ }
51+ - Invokation:
52+ func1(10, 20) // Arguments
53+ - 'return' keyword:
54+ - Return a value at the point where the function was invoked
55+ - Terminates the function execution at the point where return keyword was used
56+ */
57+
58+ /* --------------------------- */
59+ // function sayHi() {
60+ // console.log('START')
61+ // let num1 = 10
62+ // let num2 = 20
63+ // let sum = num1 + num2
64+ // console.log('Sum: ', sum)
65+ // console.log('END')
66+ // }
67+
68+ // sayHi()
69+ // sayHi()
70+ // sayHi()
71+
72+ /* --------------------------- */
73+ // function abc() {
74+ // console.log('ABC')
75+ // }
76+
77+ // const abc = function () {
78+ // console.log('ABC')
79+ // }
80+
81+ // abc()
82+ // abc()
83+ // abc()
84+ /* --------------------------- */
85+ /* # Example 1: Create a function to list the recipe steps for making a pizza
86+ 1. Prepare the pizza base
87+ 2. Add pizza sauce
88+ 3. Add toppings
89+ 4. Add cheeze
90+ 5. Bake the pizza
91+ */
92+
93+ // function pizzaRecipe() {
94+ // console.log('1. Prepare the pizza base')
95+ // console.log('2. Add pizza sauce')
96+ // console.log('3. Add toppings')
97+ // console.log('4. Add cheeze')
98+ // console.log('5. Bake the pizza')
99+ // }
100+
101+ // pizzaRecipe()
102+ // pizzaRecipe()
103+ // pizzaRecipe()
104+
105+ /* --------------------------- */
106+ /* # Example 2: Create a function to find the sum of two numbers */
107+ // function sum(num1, num2) { // Parameters
108+ // console.log(num1 + num2)
109+ // }
110+
111+ // sum(10, 20) // Arguments
112+ // sum(3784783984, 34709359)
113+ // sum(-15, 12)
114+
115+ /* --------------------------- */
116+ /* # Example 3: Create a function to display the full name of the user if firstName and lastName are provided */
117+ // function printFullName(firstName, lastName) {
118+ // console.log(firstName + ' ' + lastName)
119+ // }
120+
121+ // printFullName('Hayden', 'Thompson')
122+ // printFullName('Kelly', 'Cook')
123+ // printFullName('Happy', 'Singh')
124+
125+ /* --------------------------- */
126+ /* # Example 4: Create a function which takes a number as parameter and logs whether it is an even number or odd number */
127+
128+ // Way 1
129+ // function checkEvenOrOdd(num) {
130+ // if(num%2 == 0) {
131+ // console.log('Provided number is even')
132+ // } else {
133+ // console.log('Provdied number is odd')
134+ // }
135+ // }
136+
137+ // Way 2
138+ // function checkEvenOrOdd(num) {
139+ // console.log(num % 2 ? 'Odd' : 'Even')
140+ // }
141+
142+ // checkEvenOrOdd()
143+
144+ /* --------------------------- */
145+ // function abc() {
146+ // console.log('START')
147+ // console.log('FUNCTIONS')
148+ // return
149+ // console.log('END')
150+ // }
151+ // abc()
152+
153+ // function add(num1, num2) {
154+ // const sum = num1 + num2
155+ // return sum
156+ // }
157+
158+ // const result = add(30, 50)
159+ // console.log(result)
160+
161+ /* --------------------------- */
162+ /* # Example 5: Create a function which takes a number as parameter and checks whether it is an prime number or not */
163+
164+ // function checkPrime(num1) {
165+ // for(let i = 2; i < num1; i++) {
166+ // if(num1 % i == 0) {
167+ // console.log('Provided number is not prime number')
168+ // return
169+ // }
170+ // }
171+ // console.log('Provided number is a prime number')
172+ // }
173+
174+ // checkPrime(5)
175+
176+ /* --------------------------- */
177+ /* # Example 6: Create a function which takes a number as parameter and returns the factorial of that number */
178+
179+ // Way 1
180+ // function factorial(num) {
181+ // let fact = 1
182+ // for(let i = num; i >= 2; i--) { // 1
183+ // fact = fact * i // 120
184+ // }
185+ // return fact
186+ // }
187+
188+ // Way 2
189+ // function factorial(num) {
190+ // let fact = 1
191+ // for(let i = 2; i <= num ; i++) { // 1
192+ // fact = fact * i // 120
193+ // }
194+ // return fact
195+ // }
196+
197+ // console.log(factorial(6))
198+
199+ /* --------------------------- */
200+ /* # Example 7: Create a function which takes a 2 numbers as parameters and returns the average of two numbers */
201+
202+ // function getAverage(num1, num2) {
203+ // let avg = (num1 + num2) / 2
204+ // return avg
205+ // }
206+ // console.log(getAverage(11, 14))
207+
208+ /* --------------------------- */
209+ /* # Example 8: Create a function which takes a 3 numbers as parameters and returns the greatest number (Assume that all 3 numbers are unequal) */
210+
211+ // Way 1
212+ // function findMax(num1, num2, num3) {
213+ // if(num1 > num2 && num1 > num3) {
214+ // return num1
215+ // } else if (num2 > num1 && num2 > num3) {
216+ // return num2
217+ // } else {
218+ // return num3
219+ // }
220+ // }
221+
222+ // Way 2
223+ // function findMax(num1, num2, num3) {
224+ // return Math.max(num1, num2, num3)
225+ // }
226+
227+ // let result = findMax(25, 50, 75)
228+ // console.log(result + " is the greatest number")
229+ </ script >
230+ </ body >
231+ </ html >
0 commit comments