0% found this document useful (0 votes)
40 views

CSS - Practical No. 4

The document discusses implementing functions in JavaScript. It defines a function, explains how to invoke functions, and lists advantages of using functions such as code reuse and producing different results with the same code using different arguments. The document aims to develop JavaScript code to implement functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views

CSS - Practical No. 4

The document discusses implementing functions in JavaScript. It defines a function, explains how to invoke functions, and lists advantages of using functions such as code reuse and producing different results with the same code using different arguments. The document aims to develop JavaScript code to implement functions.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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