Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 0269a6c

Browse files
Functions in JS 😁🍾
1 parent 0e3a089 commit 0269a6c

File tree

2 files changed

+104
-0
lines changed

2 files changed

+104
-0
lines changed

‎17_JavaScript - Functions/index.html‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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>Hello World!</title>
7+
</head>
8+
<body>
9+
10+
<script src="./script.js"></script>
11+
12+
</body>
13+
</html>

‎17_JavaScript - Functions/script.js‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
alert("Function in JavaScript!");
2+
3+
/*
4+
JavaScript Functions
5+
6+
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+
function getAverage (a, b){
68+
69+
let average = (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+
function Result(subject1, subject2){
83+
84+
let finalResultTotal = subject1 + subject2;
85+
console.log(finalResultTotal);
86+
return finalResultTotal;
87+
88+
}
89+
90+
let myResult = Result(80, 75); // In here myResult now will be total result that return value from Result function.
91+
document.write("Final Result: " + myResult + "<br />");

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /