- Javascript - Page Printing
- Javascript - Void Keyword
- Javascript - Dialog Boxes
- Javascript - Page Redirect
- Javascript - Cookies
- Javascript - Events
- Javascript - Functions
- Javascript - Loop Control
- Javascript - For...in
- Javascript - For Loop
- Javascript - While Loop
- Javascript - Switch Case
- Javascript - If...Else
- Javascript - Operators
- Javascript - Variables
- Javascript - Placement
- Javascript - Enabling
- Javascript - Syntax
- Javascript - Overview
- Javascript - Home
JavaScript Objects
- Javascript - HTML DOM
- Javascript - RegExp
- Javascript - Math
- Javascript - Date
- Javascript - Arrays
- Javascript - Strings
- Javascript - Boolean
- Javascript - Number
- Javascript - Objects
JavaScript Advanced
- Javascript - Browsers
- Javascript - Image Map
- Javascript - Debugging
- Javascript - Multimedia
- Javascript - Animation
- Javascript - Validations
- Javascript - Error Handling
JavaScript Useful Resources
- Javascript - Resources
- Javascript - Functions
- Javascript - Quick Guide
- Javascript - Questions And Answers
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
JavaScript - Functions
A function is a group of reusable code which can be called anywhere in your program. This epminates the need of writing the same code again and again. It helps programmers in writing modular codes. Functions allow a programmer to spanide a big program into a number of small and manageable functions.
Like any other advanced programming language, JavaScript also supports all the features necessary to write modular code using functions. You must have seen functions pke alert() and write() in the earper chapters. We were using these functions again and again, but they had been written in core JavaScript only once.
JavaScript allows us to write our own functions as well. This section explains how to write your own functions in JavaScript.
Function Definition
Before we use a function, we need to define it. The most common way to define a function in JavaScript is by using the function keyword, followed by a unique function name, a pst of parameters (that might be empty), and a statement block surrounded by curly braces.
Syntax
The basic syntax is shown here.
<script type = "text/javascript"> <!-- function functionname(parameter-pst) { statements } //--> </script>
Example
Try the following example. It defines a function called sayHello that takes no parameters −
<script type = "text/javascript"> <!-- function sayHello() { alert("Hello there"); } //--> </script>
Calpng a Function
To invoke a function somewhere later in the script, you would simply need to write the name of that function as shown in the following code.
<html> <head> <script type = "text/javascript"> function sayHello() { document.write ("Hello there!"); } </script> </head> <body> <p>Cpck the following button to call the function</p> <form> <input type = "button" oncpck = "sayHello()" value = "Say Hello"> </form> <p>Use different text in write method and then try...</p> </body> </html>
Output
Function Parameters
Till now, we have seen functions without parameters. But there is a facipty to pass different parameters while calpng a function. These 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.
Example
Try the following example. We have modified our sayHello function here. Now it takes two parameters.
<html> <head> <script type = "text/javascript"> function sayHello(name, age) { document.write (name + " is " + age + " years old."); } </script> </head> <body> <p>Cpck the following button to call the function</p> <form> <input type = "button" oncpck = "sayHello( Zara , 7)" value = "Say Hello"> </form> <p>Use different parameters inside the function and then try...</p> </body> </html>
Output
The return Statement
A JavaScript function can have an optional return statement. This is required if you want to return a value from a function. This statement should be the last statement in a function.
For example, you can pass two numbers in a function and then you can expect the function to return their multippcation in your calpng program.
Example
Try the following example. It defines a function that takes two parameters and concatenates them before returning the resultant in the calpng program.
<html> <head> <script type = "text/javascript"> function concatenate(first, last) { var full; full = first + last; return full; } function secondFunction() { var result; result = concatenate( Zara , Ap ); document.write (result ); } </script> </head> <body> <p>Cpck the following button to call the function</p> <form> <input type = "button" oncpck = "secondFunction()" value = "Call Function"> </form> <p>Use different parameters inside the function and then try...</p> </body> </html>
Output
There is a lot to learn about JavaScript functions, however we have covered the most important concepts in this tutorial.