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
A JavaScript function is a block of code designed to perform a particular task.
7
+
A JavaScript function is executed when "something" invokes it (calls it).
8
+
9
+
Example
10
+
// Function to compute the product of p1 and p2
11
+
function myFunction(p1, p2) {
12
+
return p1 * p2;
13
+
}
14
+
15
+
16
+
JavaScript Function Syntax
17
+
18
+
A JavaScript function is defined with the function keyword, followed by a name, followed by parentheses ().
19
+
Function names can contain letters, digits, underscores, and dollar signs (same rules as variables).
20
+
The parentheses may include parameter names separated by commas:
21
+
(parameter1, parameter2, ...)
22
+
23
+
The code to be executed, by the function, is placed inside curly brackets: {}
24
+
25
+
function name(parameter1, parameter2, parameter3) {
26
+
// code to be executed
27
+
}
28
+
29
+
- Function parameters are listed inside the parentheses () in the function definition.
30
+
- Function arguments are the values received by the function when it is invoked.
31
+
- Inside the function, the arguments (the parameters) behave as local variables.
32
+
33
+
Function Invocation
34
+
35
+
The code inside the function will execute when "something" invokes (calls) the function:
36
+
- When an event occurs (when a user clicks a button)
37
+
- When it is invoked (called) from JavaScript code
38
+
- Automatically (self invoked)
39
+
40
+
41
+
Function Return
42
+
43
+
When JavaScript reaches a return statement, the function will stop executing.
44
+
If the function was invoked from a statement, JavaScript will "return" to execute the code after the invoking statement.
45
+
Functions often compute a return value. The return value is "returned" back to the "caller":
46
+
47
+
Example
48
+
Calculate the product of two numbers, and return the result:
49
+
50
+
// Function is called, the return value will end up in x
51
+
let x = myFunction(4, 3);
52
+
53
+
function myFunction(a, b) {
54
+
// Function returns the product of a and b
55
+
return a * b;
56
+
}
57
+
58
+
59
+
Why Functions?
60
+
61
+
- With functions you can reuse code
62
+
- You can write code that can be used many times.
63
+
- You can use the same code with different arguments, to produce different results.
64
+
*/
65
+
66
+
// Creating function
67
+
functiongetAverage(a,b){
68
+
69
+
letaverage=(a+b)/2;
70
+
document.write("Average: "+average+"<br/>");
71
+
72
+
}
73
+
74
+
// calling function - creating function doesnt do anything. we have to call the function to execute the function.
75
+
getAverage(7,12);
76
+
77
+
// If additional value add to function bracket it will ignored.
78
+
getAverage(10,12,13);// This function only need two parameter values. but we pass three. then function will work with 10 and 12 values. and ignored 13 value (ignored last added any values).
79
+
80
+
81
+
// Returning value from function
82
+
functionResult(subject1,subject2){
83
+
84
+
letfinalResultTotal=subject1+subject2;
85
+
console.log(finalResultTotal);
86
+
returnfinalResultTotal;
87
+
88
+
}
89
+
90
+
letmyResult=Result(80,75);// In here myResult now will be total result that return value from Result function.
0 commit comments