User Defined Functions in Javascript
User Defined Functions in Javascript
Functions offer the ability to group together the JavaScript code that performs a specific task
into a single unit that can be used repeatedly.
Function can accept in the form of arguments and can return results.
Appropriate syntax needs to be followed for declaring functions, invoking them, passing them
values and accepting their return values.
Declaring Functions
Functions are declared and created using the function keyword.
Syntax of a function
function function_name(param1,parameter2,…)
{
//block of javascript code
}
• A function is a case sensitive and can include underscore(_) and has to start with a letter.
• The list of parameters passed to the function appears in parentheses and commas separate the
members of the list.
Place of declaration
Functions can be declared anywhere within an HTML file. Preferably functions are created within the
<HEAD> and </HEAD> tags. This ensures that all the functions are parsed before they are invoked and
called. If the function is called before it is declared and parsed ,it will lead to an error condition.
Passing Parameters
Values can be passed to function parameters when a parameterized function is called. Values are passed
to the function by listing them in the parentheses following the function name.
Multiple values can be passed, separated by commas provided that the function has been coded to
accept multiple parameters.
Val user=“Bob”;
printName(user); will cause the variable Bob is assigned to the user.
Variable Scopes
The parameters of the function are local to the function. They come into existence when the function is
called and cease to exist when the function ends. For example, the variable user exists only in the
function printName() and not outside it.
Any variable declared using the var variable name within the function would have a scope limited to the
function and it is said to be local.
If a variable declared outside the function and it is available to all statements and it is said to be global.
Return Values
An user defined function can return values. Such values can be returned using the return statement.
The return statement is used to return any valid expression that evaluates to a single value.
Example:
function cube(number)
{
var cube = number*number*number;
return cube;
}
Object_name.Method_name
The write() method accepts a string value passed to it within its parantheses. The String value then can
be written to the browser. The write() method accepts this string and places it in the current browser
window.
Dialog boxes
JavaScript supports three important types of dialog boxes.
These dialog boxes can be used to raise an alert, or to get confirmation on any input or to have a kind of
input from the users.
The following are the three types of dialog boxes. They are
For example, if one input field requires to enter some text but the user does not provide any input, then
as a part of validation, you can use an alert box to give a warning message.
Nonetheless, an alert box can still be used for friendlier messages. Alert box gives only one button "OK"
to select and proceed.
<html>
<head>
<script type="text/javascript">
<!--
function Warn() {
alert ("This is a warning message!");
document. write ("This is a warning message!");
}
//-->
</script>
</head>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type="button" value="Click Me" onclick="Warn();" />
</form>
</body>
</html>
Confirm Dialog Box: A confirmation dialog box is mostly used to take user's consent on any option. It
displays a dialog box with two buttons: OK and Cancel.
If the user clicks on the OK button, the window method confirm will return true. If the user clicks on the
Cancel button, then confirm returns false
<head>
<script type="text/javascript">
<!--
function getConfirmation(){
var retVal = confirm("Do you want to continue ?");
if( retVal == true ){
document.write ("User wants to continue!"); return true;
}
else{
document.write ("User does not want to continue!"); return false;
}
}
//-->
</script>
</head>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type="button" value="Click Me" onclick="getConfirmation();" />
</form>
</body>
</html>
Prompt Dialog Box: The prompt dialog box is very useful when an user wants to pop-up a text box to get
user input. Thus, it enables to interact with the user. The user needs to fill in the field and then click OK.
This dialog box is displayed using a method called prompt which takes two parameters: i) a label which
you want to display in the text box and ii) a default string to display in the text box.
This dialog box has two buttons: OK and Cancel. If the user clicks the OK button, the window method
prompt will return the entered value from the text box. If the user clicks the Cancel button, the window
method prompt returns null.
<html>
<head>
<script type="text/javascript">
<!--
function getValue(){
var retVal = prompt("Enter your name : ", "your name here");
document.write("You have entered : " + retVal);
}
//-->
</script>
</head>
<body>
<p>Click the following button to see the result: </p>
<form>
<input type="button" value="Click Me" onclick="getValue();" />
</form>
</body>
</html>