Function L3
Function L3
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