Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (FUNCTION) MRS. S.S.

KADAM
--------------------------------------------------------------------------------------------------------------------------------------
FUNCTION
2.2 Function
A function is a subprogram designed to perform a particular task. Functions are executed
when they are called. This is known as invoking a function. Values can passed into functions
and used within in the function. Function always return a value. In javascript ,if no return
value is specified, the function will return undefined.

2.2.1 Defining a function


The function defines by using function keyword with name of function.
A list of parameters names enclosed in parentheses. return keyword is optional.

Syntax:
Function function_name(parameter)
{
Statement
}

2.2.2 Writing a function:


Program:
<html>
<body>
<script language="javascript" type="text/javascript">
function show()
{
document.write("In Show Function");
}
</script>
</body>
</html>

2.2.3 Adding an Arguments


When one or more variables that are declared within the parentheses of a function definition
is known as arguments. These arguments are used to hold data to perform some task.

Program: Write a Javascript code to pass arguments to user define function


<html>
<body>
<script language="javascript" type="text/javascript">
function show(name,rollno)
{
document.write("Student Information is :"+name+rollno);

25
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (FUNCTION) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
}
</script>
</body>
</html>

2.2.4 Scope of Variables and Argument


1.Global Scope: All the variables that you declare, is by default defiend in global scope.
When a variables is declared outside a function with a var keyword then that variable is a
global variable because it is available through all part of script.

2.Local Scope: All variables which are declared using var keyword within function then
those variables are known as local variables and are accessible within function only.

Q: Explain scope of variable declared in function with example

<html>
<body>
<script language="javascript" type="text/javascript">
var a="Hello"; //global variable
function show()
{
var b="Student"; //local variable
document.write("Global variable : "+a);
document.write("<br>Local variable : "+b);
}
show();
</script>
</body>
</html>
Output:

26
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (FUNCTION) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
2.3 Calling a Function:
2.3.1 Calling a Function with or without Argument
a) Calling a Function without Argument
In Javascript function calling is by using name of function followed by paranthsis.
<html>
<body>
<script language="javascript" type="text/javascript">
function area()
{
var l=10;
var b=20;
document.write("Area of Rectangle : "+(l*b));
}
area();
</script>
</body>
</html>

Output:

b) Calling a Function with Argument


If the function has arguments (parameters) then values for each argument in placed within
parenthesis separated by commas.
<html>
<body>
<script language="javascript" type="text/javascript">
function area(l,b)
{
document.write("Area of Rectangle : "+(l*b));
}
area(10,20);
</script>

27
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (FUNCTION) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
</body>
</html>

Output:

2.3.2 Calling Function from HTML:


In Javascript functions can be called from HTML code in response of any particular event like
page load, unload, button click etc.

Program:
<html>
<body>
<script language="javascript" type="text/javascript">
function show()
{
alert("In show function.....");
}
</script>
<button onclick="show()">Click</button>
</body>
</html>
Output:

28
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (FUNCTION) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
2.3.3 Function Calling another Function:
In Javascript we can call one function inside another function.
<html>
<body>
<script language="javascript" type="text/javascript">
function insert()
{
document.write("In insert Function......");
}
function show()
{
insert();
}
show();
</script>
</body></html>

Output:

29
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (FUNCTION) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
2.3.4 Returning a Value from Function:
The return keyword stops the execution of function and the value is return from function to
the function caller.
Syntax:
return value;

Program:Write a Javascript code to return a value from function.

<html>
<body>
<script language="javascript" type="text/javascript">
function addition()
{
var a =10;
var b=20;
return(a+b);
}
var result=addition();
document.write("Addition of two number is : "+ result);
</script>
</body>
</html>

Output:

30
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (FUNCTION) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
Intrinsic JavaScript Functions

 An intrinsic function (or built-in function) is a function (subroutine) available for use in a
given programming language whose implementation is handled specially by the compiler.

 JavaScript provides intrinsic (or “built-in”) objects.

Eg. Array, Boolean, Date, Error, Function, Global, JSON, Math, Number, Object, RegExp,
and String objects.

 If an object attribute consists of function, then it is a method of that object, or if an


object attribute consists of values, then it is a property of that object.
document.write("Hello World");

write() is a method of the document object that writes the content “Hello World” on the web
page.

 JavaScript Built-in objects such as

1. Number

2. String

3. RegExp

4. Array

5. Math

6. Date

7. Boolean

Each of the above objects hold several built-in functions to perform object related
functionality.

Number()

 Number() method takes an object as an argument and converts it to the corresponding


number value.

 Return Nan (Not a Number) if the object passed cannot be converted to a number

31
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (FUNCTION) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
 For example
var obj1=new String("123");
var obj2=new Boolean("false");
var obj3=new Boolean("true");
var obj4=new Date();
var obj5=new String("9191 9999");document.write(Number(obj1)); // 123
document.write(Number(obj2)); // 0
document.write(Number(obj3)); // 1
document.write(Number(obj4)); // 1342720050291
document.write(Number(obj5)); // NaN

String()

 String() function converts the object argument passed to it to a string value.

 For example
document.write(new Boolean(0)); // false
document.write(new Boolean(1)); // true
document.write(new Date()); // Tue Jan 05 2021 13:28:00 GMT+0530

parseInt()

 parseInt() function takes string as a parameter and converts it to integer.

 For example
document.write(parseInt("45")); // 45
document.write(parseInt("85 days")); // 85
document.write(parseInt("this is 9")); // NaN

 An optional radix parameter can also be used to specify the number system to be used
to parse the string argument.

 For example,
document.write(parseInt(“10”,16)); //16

parseFloat()

 parseFloat() function takes a string as parameter and parses it to a floating point


number.

 For example
document.write(parseFloat("15.26")); // 15.26
document.write(parseFloat("15 48 65")); // 15

32
CLIENT SIDE SCRIPTING LANGUAGE (CSS)-UNIT 2 (FUNCTION) MRS. S.S. KADAM
--------------------------------------------------------------------------------------------------------------------------------------
document.write(parseFloat("this is 29")); // NaN
document.write(pareFloat(" 54 ")); // 54

 An intrinsic function is often used to replace the Submit button and the Reset button
with your own graphical images, which are displayed on a form in place of these
buttons.
<html>
<head>
<title>Using Intrinsic JavaScript Functions</title>
</head>
<body>
<FORM name="contact" action="#" method="post">
<P>
First Name: <INPUT type="text" name="Fname"/> <BR>
Last Name: <INPUT type="text" name="Lname"/><BR>
Email: <INPUT type="text" name="Email"/><BR>
<img src="submit.jpg"
onclick="javascript:document.forms.contact.submit()"/>
<img src="reset.jpg"
onclick="javascript:document.forms.contact.reset()"/>
</P>
</FORM>
</body>
</html>

33

You might also like