Class 9-Term 2-Advanced Javascript-Functions
Class 9-Term 2-Advanced Javascript-Functions
TERM 2
Advanced JavaScript
FUNCTIONS
OBJECTIVE
A large program is broken into small, simple manageable code called functions or
modules.
ADVANTAGES
•By doing so it becomes easy to debug and manage the function and integrate the
modules and run as a whole unit. This is called divide and conquer.
DISADVANTAGES
As the code is divided into small modules, it increases the complexity of understanding.
TYPES OF FUNCTIONS
<html>
<head>
<script language="JavaScript">
function sayHello() //function definition
{
alert("Hello from the world of Javascript");
}
</script>
</head>
<body></body>
</html>
A FUNCTION CAN BE CALLED MANY TIMES
FROM WITHIN A SCRIPT.
<html>
<head>
<script language="JavaScript">
function sayHello() //function definition
{
alert("Hello from the world of javascript");
}
for(i=1;i<11;i++)
sayHello(); //function call 10 times
</script>
</head>
<body></body>
</html>
JUST A MINUTE…
EXERCISE
<<script language="JavaScript">
var g=200; //global variable
function a()
{ Output
var l=10 // local variable
document.write("Local to fn a " +l+"<BR>"); Global data before function calls 200
g+=10 Local to fn a 10
document.write("Global data inside fn a " +g+"<BR>");
} Global data inside fn a 210
Local to fn b 5
function b()
{ Global data inside fn b 230
var l=5 Global data after function calls 230
g+=20
document.write("Local to fn b " +l+"<BR>");
document.write("Global data inside fn b " +g+"<BR>");
}
document.write("Global data before function calls " + g+"<BR>");
a();
b();
document.write("Global data after function calls " +g+"<BR>");
</script>
<script language="javascript">
var x=30
function calculate()
{
var y=90 // LOCAL VARIABLE ONLY AVAILABLE IN THIS FUNCTION
abc()
}
function abc(y)
{
alert(x) /valid
alert(y) // INVALID , CANNOT ACCESS VARIABLE y USED IN calculate()
y=y+80 // INVALID
}
calculate()
</script>
Passing parameters to functions
Most functions have a list of formal parameters or
arguments that provide the means for communicating
information between function via function calls.
Syntax
return(value);
<script language=“JavaScript">
function area(a)
{
return (a * a)
}
document.write(area(4))
</script>
SCRIPT USING ARGUMENTS TO CALCULATE
AVERAGE OF STUDENT MARKS
<script language="JavaScript">
function average(x,y,z)
//function definition with arguments x,y,z
{
var sum= x+y+z; // statement 1
var avg= sum/3; //2
return avg; //3
}
var result=average(1,2,3); //4 passing parameters 1,2,3
document.write("Average is " + result); //5
</script>
JUST A MINUTE…
EXERCISE
<script language="javascript">
function area(a)
{
return (a * a)
}
x=area(6)
alert(x)
</script>
Predict the output
<script language=”javascript”>
function sumfn( y)
{
if(y%2= =0)
document.write(y+6*5);
}
Z=sumfn(4)
alert(Z)
</script>
JUST A MINUTE…
EXERCISE
<script language="javascript">
function square(i,j)
{
i=i* i
j=j*j
x=i+j
return(x)
}
var i=4
var j=10
var result=square(i,j)
document.write(result);
</script>
Write a script that defines a function square() to calculate
the square of the integers 1 to 10 (using for loop).
<script language=”JavaScript”>
function square(x) //function definition
{
return x*x;
}
var m=90;
execute(m);
</script>
JUST A MINUTE…
EXERCISE