You are on page 1of 10

MORE ON FUNCTIONS

PROF. DAVID ROSSITER

1/10

AFTER THIS PRESENTATION


You'll know how to assign a function to a variable
You'll be able to pass a function to a function
You'll be able to return a function from a function

2/10

WE WILL LOOK AT
Events

onload

Functions

function
return

3/10

TWO WAYS TO DECLARE A FUNCTION


functionfunctionOne(){

. . . code here . . .
}

This function is defined when the web page is loaded.

4/10

varfunctionTwo=function(){

. . . code here . . .
}
varfunctionTwo=functionthisFunc(){

. . . code here . . .
}

5/10

Here we give a function to a variable


The function is defined when the browser
reaches that point in the code

6/10

PASSING A FUNCTION TO A FUNCTION


You can pass a function to a function

7/10

<!doctypehtml>
<html>
<head>
<script>
functioncheck(a,b){

if(b!=0)returntrue

elsereturnfalse
}
functionmyDivide(fn,num,div){

if(fn(num,div)){

alert("It'sOK!")

returnnum/div

}else{

alert("NotOK!")

}
}
result=myDivide(check,44,1)
</script>
</head>
</html>

8/10

RETURNING A FUNCTION FROM A FUNCTION


You can return a function from a function

9/10

<!doctypehtml>
<html>
<head>
<script>
functioncounter(){
varcount=0
returnfunction(){
count++
alert(count)
}
}
varcount=counter()
count()
count()
count()
</script>
</head>
</html>

10/10

You might also like