0% found this document useful (0 votes)
5 views14 pages

Function Declaration and Expression

This document provides an overview of JavaScript functions, including their definition, advantages, properties, and invocation methods. It includes example programs demonstrating function creation, invocation, and function expressions. The session concludes with a summary of the key points discussed regarding the declaration and usage of functions in JavaScript.

Uploaded by

Dhanush kuna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views14 pages

Function Declaration and Expression

This document provides an overview of JavaScript functions, including their definition, advantages, properties, and invocation methods. It includes example programs demonstrating function creation, invocation, and function expressions. The session concludes with a summary of the key points discussed regarding the declaration and usage of functions in JavaScript.

Uploaded by

Dhanush kuna
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 14

WEB PROGRAMMING

JAVASCRIPT FUNCTIONS

By
G.Sowmya
Assistant Professor
CSE-AIML Dept
MLR Institute of Technology
Overview of the Presentation

 What is a Function?
 Advantages of Functions
 Function properties and Invocation
 Example JavaScript Function Program
JAVASCRIPT POP BOXES

What is a Function

 A group of statements that is put together (or defined) once and then can be
used (by reference) repeatedly on a Web page

 Also known as subprogram, procedure, subroutine.


JAVASCRIPT FUNCTIONS

Advantages of Functions:

 Number of lines of code is reduced

 Code becomes easier to read & understand

 Code becomes easier to maintain as changes need to be made only at a single


location instead multiple locations
JAVASCRIPT FUNCTIONS

Syntax:
Function Properties and Invocation:
function name(parameter1, parameter2,
 A JavaScript function is defined with parameter3) {
code to be executed
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: {}
JAVASCRIPT FUNCTIONS

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)
JAVASCRIPT FUNCTIONS

Example JavaScript Function Program1

<html>
<script type="text/javascript">
function my_fun()
{
document.write("Within the function");
}
</script>
JAVASCRIPT FUNCTIONS

<body>
<script type="text/javascript">
document.write("Before function call");
document.write("<br>");
my_fun();
</script>
</body>
</html>
JAVASCRIPT FUNCTIONS

Example JavaScript Function Program2

Returning value from Function:


<html>
<script type="text/javascript">
function add( a, b )
{
c=a+b;
return c ;
}
</script>
JAVASCRIPT FUNCTIONS

<body>
<script type="text/javascript">
document.write(add(2,4));
document.write("<br>");
sum=add(4,6);
document.write(sum);
</script>
</body>
</html>
.
JAVASCRIPT FUNCTIONS

Function expression:
A Function Expression defines a function as part of an assignment to a variable.
Syntax:
const greet = function() { console.log("Hello, World!"); }; greet(); // Output: Hello, World!
Key Features:
 Not hoisted: You cannot call the function before its definition.
 Can be anonymous (without a name) or named.
 Often used in callbacks or assigned to variables
.
JAVASCRIPT FUNCTIONS

Ex:
greet(); // ❌ Error: Cannot access 'greet' before initialization
const greet = function() {
console.log("Hello!");
};
SUMMARY

In this session we discussed about

 What is a function and how to declare and use functions in JavaScript


with example programs.
WEB PROGRAMMING

Thank You

You might also like