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
Copy file name to clipboardExpand all lines: docs/13-functions.md
+46-3Lines changed: 46 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -12,9 +12,52 @@ In this Session, you will learn to:
12
12
- Explain Private class methods
13
13
- Explain JSON
14
14
15
-
---
16
-
17
15
## 12.1 Introduction
18
16
Consider a scenario where a Web page has been designed to greet the user with his/her name on the click of a button. A code can be used here to accomplish this task, but may result in the same output on repetitive executions. However, writing these statements each time for the same action is tedious, time consuming, and error prone.
19
17
20
-
To make the code more task-oriented and manageable, JavaScript allows to group statements before they are actually invoked. This can be achieved by using the concept of functions. A function is a reusable block of code that is executed on the occurrence of an event. The event can be a user action on the page or a call within the script.
18
+
To make the code more task-oriented and manageable, JavaScript allows to group statements before they are actually invoked. This can be achieved by using the concept of functions. A function is a reusable block of code that is executed on the occurrence of an event. The event can be a user action on the page or a call within the script.
19
+
20
+
## 12.2 Functions
21
+
A function is an independent reusable block of code that performs certain operations on variables and expressions to fulfill a task. A function might take parameters, which are variables or values on which it performs operations. After performing operations, a function might return the resultant value to display it in the browser. For example, a function names `Add()` might take two numbers on which the addition operation will be performed and will return the result of addition.
22
+
23
+
A JavaScript function is always created under the `script` element. JavaScript supports both user-defined and build-in functions.
24
+
25
+
## 12.2.1 Declaring and Defining Functions
26
+
JavaScript allows declaring a function using the `function` keyword. The keyword is followed by the name of the function and parameters enclosed within the parenthesis. If the function does not take any parameters, then it must be specified with the empty parenthesis.
27
+
28
+
Once the function is declared, you need to define the function by specifying the operations or instructions within the curly braces { and }. These curly braces indicate the start and end of the function block, which is collectively referred to as the body of the function.
29
+
30
+
## 12.2.2 Invoking Functions
31
+
A function requires to be invoked or called to execute it in the browser. To invoke a function, specify the function name followed by parenthesis outside the function block.
32
+
33
+
A function can be defined and invoked even in an external JavaScript file. Also, a function can be called from another function in JavaScript. The function that invokes another function is called the calling function; whereas the function that is called is referred to as the called function.
34
+
35
+
## 12.2.3 Parameterized Functions
36
+
Parameterized functions refer to JavaScript functions that take parameters. These parameters hold values on which the function requires to perform operations. Parametrized functions can be created to accept values for performing operations.
37
+
```
38
+
var val1 = parseInt(prompt("Enter the first for addition"));
39
+
var val2 = parseInt(prompt("Enter the second for addition"));
40
+
41
+
add(val1, val2);
42
+
43
+
function add(num1, num2) {
44
+
var result = num1 + num2;
45
+
alert("Addition Result: " + result);
46
+
}
47
+
```
48
+
The parameters of a function are variables that are declared in the function declaration, Here, `num1` and `num2` are the parameters of the function. Similarly, arguments are the values passed to the function. Here, `val1` and `val2` are the arguments whose values are passed to the parameters, `num1` and `num2` while invoking the function.
49
+
50
+
Alternatively, one can use same variable names for arguments and parameters while creating and invoking functions. In either of the case, the variables will occupy different memory space.
51
+
52
+
## 12.2.4 Ways of Passing Arguments
53
+
There are two ways of passing arguments to a function namely, pass by value and pass by reference. The description about these is as follows:
54
+
55
+
-**Passing by value** - Refers to passing variables as arguments to a function. In the pass by value method, the called function do not change the values of the parameters passed to it from calling function.
56
+
-**Passing by reference** - Refers to passing objects as arguments to a function. In the pass by reference method, the called function modifies the value of parameters passed to it from the calling function. This change is reflected when the control passes back to the calling function.
57
+
58
+
## 12.2.5 return Statement
59
+
A function operates on its parameters that might lead to some output values. The output needs to be displayed to the user or it needs to be send back to the calling function. JavaScript allows sending the result back to the calling function by using the `return` statement.
60
+
61
+
The return statement begins with `return` keyword followed by the variable or value, which must be returned to the calling function. The return statement can also be used to halt the execution of the function and to return the control to the calling function. This is required when a particular condition is false or when there are chances of unexpected results during the code execution.
0 commit comments