New from BookNet Canada for 2024: Loan Stars - Tech Forum 2024
Functions in javascript
1.
2. Disclaimer: This presentation is prepared by trainees of
baabtra as a part of mentoring program. This is not official
document of baabtra –Mentoring Partner
Baabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .
Ltd
5. Functions in Javascript
Muhammed Noufal V T
muhammednoufalvt@gmail.c
om
www.facebook.com/vtnoufa
lvt
twitter.com/noufalurnappy
in.linkedin.com/pub/muham
med-noufal
9744003056
6. •
What is Functions defined)
A group of statements that is put together (or
once and then can be used (by reference) repeatedly on
a Web page
• Also known as subprogram, procedure, subroutine
• A function is written as a code block (inside curly { }
braces), preceded by the function keyword
• Syntax js function
– function functionname()
{
some code to be executed
}
– alert("message")
– confirm("message")
– prompt("message")
7. Advantages of Functions
• Number of lines of code is reduced
• Code becomes easier to read &
understand
• Code becomes easier to maintain as
changes need to be made only at a
single location instead multiple
9. Function Defnitions
•
•
•
Format of a function definition
function function-name( parameter-list )
{
declarations and statements
}
– Function name any valid identifier
– Parameter list names of variables that will receive arguments
• Must have same number as function call
• May be empty
– Declarations and statements
• Function body (“block” of code)
Return statement
– Optional , can return either nothing, or a value
– This is required if you want to return a value from a function
10. Function Parameters
• Facility to pass different parameters
while calling a function
• Passed parameters can be captured
inside the function and any
manipulation can be done over those
parameters
• A function can take multiple
parameters separated by comma.
• Parameters define the interface
between the function and the rest of
11. • <html>
Example
<head>
<script type="text/javascript">
function say(name, age)
{
alert( "I am" + name + age + " years old.");
}
</script>
</head>
<body>
<p>Click the following button to Know about my self</p>
<form>
<input type="button" onclick="say('Baabtra', 2)" value="Myself">
</form>
</body>
</html>
12. Local and Global variable
• A variable declared (using var) within a
JavaScript function becomes LOCAL and can
only be accessed from within that function
13. Contact Us
Emarald Mall (Big Bazar Building)
Mavoor Road, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550
Start up Village
Eranakulam,
Kerala, India.
Email: info@baabtra.com
NC Complex, Near Bus Stand
Mukkam, Kozhikode,
Kerala, India.
Ph: + 91 – 495 40 25 550