1
Atal Bihari Vajpayee Vishwavidyalaya
Session 2020-21
Subject :-Internet Technologies
Submitted By
Ujjwal Matoliya
Topic :-functions
2
ADD A FOOTER
Function
Function Definition
Calling a Function
Function Parameters
Function Return
Function Expressions
1
Function
3
A function is a group of reusable code which can be called
anywhere in your program. This eliminates the need of writing
the same code again and again. It helps programmers in writing
modular codes. Functions allow a programmer to divide a big
program into a number of small and manageable functions.
2
4
3
Function Declaretion
The most common way to
define a function in
JavaScript is by using
the function keyword,
followed by a unique function
name, a list of parameters
(that might be empty), and a
statement block surrounded
by curly braces.
Syntax
<script type = "text/javascript">
<!-- function
functionname(parameter-list)
{ statements }
//-->
</script>
<script type = "text/javascript">
<!-- function sayHello()
{ alert("Hello there"); }
//-->
</script>
Calling a Function
5
4
6
ADD A FOOTER
Output
5
7
ADD A FOOTER
Function Parameters
A function can also be declared with parameters. A parameter is a value
that is passed when declaring a function.
6
8
ADD A FOOTER
program to add two numbers using a function Parameters
7
9
ADD A FOOTER
Function Return
A JavaScript function can have an optional return statement. This is required
if you want to return a value from a function. This statement should be the
last statement in a function.
8
10
ADD A FOOTER
9
11
ADD A FOOTER
10
12
ADD A FOOTER
Function Expressions
In Javascript, functions can also be defined as expressions.
In the above program, variable x is used to store the function. Here the
function is treated as an expression. And the function is called using the
variable name.
The function above is called an anonymous function.
11
13
ADD A FOOTER
12
14
ADD A FOOTER
Benefits of Using a Function
Function makes the code reusable. You can
declare it once and use it multiple times.
Function makes the program easier as each
small task is divided into a function.
Function increases readability.
13
15
ADD A FOOTER