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

Function L3

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

Function L3

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

OUTLINE

Function –
• Defining a function
• Writing a function
• Adding an argument
• Scope of variables & arguments
• Calling a function
• Returning a value from a function
DEFINING A FUNCTION
• The process of creating a function is called defining a function
• The process of using the function is referred as calling a
function
• A function must be defined before it can be called in a java
script statement
• The best place to define a function is at the beginning of a java
script that is inserted in the <head> tag
• A function definition consists of four parts: the name,
parenthesis, a code block, and an optional return keyword
WRITING A FUNCTION
• Syntax:

function function_name( )
{
// code block
}
ADDING AN ARGUMENT
SCOPE OF VARIABLE AND ARGUMENTS
• A variable declared within a function is a local variable
• A variable declared outside the function is a global variable
• A variable is considered in scope if the statement can access
the variable
• A variable is out of scope if a statement cannot access the
variable
CALLING A FUNCTION
• Function can be called by using function name followed by the
parentheses
• If function has arguments, values for each argument are placed
within the parentheses
Calling a function from HTML
• A function can be called from HTML code on your web page
• Typically, function will be called in response to an event, such as
when web page is loaded or unloaded by the browser
• E.g.
<body onload=“welcomeMsg()”>
CALLING A FUNCTION
Function Calling another function
• Function can be called from any java script or from HTML code
on a web page itself
• Function can also be called from another function

Returning a value from a function


• A function can report back to the statement that calls the
function
• Function returns value to that statement using return keyword
followed by a return value

You might also like