You are on page 1of 2

Practical No. 4:Develop JavaScript to implement Functions.

I.Aim: Develop Javascript code to implement Functions.

II. Minimum Theoretical Background

a. Function: A JavaScript function is a block of code designed to perform a


particular task. A JavaScript function is executed when “something” invokes it.
Function in JavaScript has following properties
- A JavaScript function is defined with the function keyword, followed by a name,
followed by parentheses ().
- Function names can contain letters, digits, underscores, and dollar signs (same
rules as variables).
- The parentheses may include parameter names separated by commas:(parameter1,
parameter2, ...)
- The code to be executed, by the function, is placed inside curly brackets: {}
Example:
function myFunction(p1, p2) {
return p1 * p2;
}
b. Function Invocation
The code inside the function will execute when "something" invokes (calls) the
function:
• When an event occurs (when a user clicks a button)
• When it is invoked (called) from JavaScript code
• Automatically (self invoked)
c. Advantages of using Functions
• You can reuse code i.e define the code once, and use it many times.
• You can use the same code many times with different arguments, to produce
different results

III.Result :
………………………………………………………………………………………………………………
………………………………………………………………………………………………………………
………………………………………………………………………………………………………………

IV.Conclusion:
………………………………………………………………………………………………………………
………………………………………………………………………………………………………………
………………………………………………………………………………………………………………..

V.Questions:-
1. Explain function with parameters in JavaScript.
Function: A JavaScript function is a block of code designed to perform a
particular task. A JavaScript function is executed when “something” invokes it.
-A JavaScript function is defined with the function keyword, followed by a name,
followed by parentheses ().
The parentheses may include parameter names separated by commas:(parameter1,
parameter2, ...)
Syntax:
Function name(parameter1, parameter2,......parameterN)
{
//code to be executed
}

2. Explain local and global variables.


- Local variables can only be accessed within a function.
- The variables declared outside of the function that variable is a global variable, it can be
accessed to all parts of script.

VI. Exercise:-
1. Write a program to perform additional operations by passing parameters using a function.

2. Write a program to display a message in the alert box using a function.

You might also like