0% found this document useful (0 votes)
575 views6 pages

User Defined Functions in Javascript

User defined functions allow JavaScript code to be grouped into reusable units that can accept parameters and return values; functions are declared with the function keyword followed by a name, parameters, and code block, and can be invoked by passing arguments to call the block of code and optionally return a value. Functions provide modularity and code reuse through abstraction and encapsulation of logical tasks.

Uploaded by

riaz ahamed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
0% found this document useful (0 votes)
575 views6 pages

User Defined Functions in Javascript

User defined functions allow JavaScript code to be grouped into reusable units that can accept parameters and return values; functions are declared with the function keyword followed by a name, parameters, and code block, and can be invoked by passing arguments to call the block of code and optionally return a value. Functions provide modularity and code reuse through abstraction and encapsulation of logical tasks.

Uploaded by

riaz ahamed
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

User defined Functions

 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.

A function can comprise of the following

 a name for the function


 A list of parameters that will accept values passed to the function

A block of JavaScript code that defines what the function does

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.

Example: Function declaration


function printName(user)
{
document.write(“<HR/>Your Name is <b>”);
document.write(user);
document.write(“</b></hr>”);
}
Where printName is the function name which has a parameter user.
A static value printName( “Bob”); will cause the string Bob is assigned to the user.

A variable is passed by the following:

Val user=“Bob”;
printName(user); will cause the variable Bob is assigned to the user.

Example Program for User Defined Functions

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;
}

Recursive Functions: Recursion refers to a situation wherein function calls themselves.


Example:
function factorial(number)
{
if(number>1)
{
return number* factorial(number-1);
}
else
{
return number;
}
}

Placing Text in a browser


In Java Script, a String can be written to the browser from within an HTML file.
The document object in JavaScript has a method for placing text in a browser. This method is called
write().
The method names are called by combining the object name with the method name.

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.

Example: document. write(“Test”);

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

a] alert dialog box


b] confirm dialog box
c] prompt dialog box

Alert Dialog Box


An alert dialog box is mostly used to give a warning message to the users.

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>

You might also like