Explain Different Types of Functions in JavaScript

A function is a piece of code to perform a specific task. Functions are basically of two types:

®pre defined (or) built in functions

® user defined functions

Pre defined functions:- They are also referred as global functions because they can be called and used in any part of the program.

Eg:- isNAN(), parseInt(), float(), eval().

User defined functions:-User defined functions are named and developed by the user. They are initially declored and coded, later they are called, depending on the requirement. Each of the function can have the following.

® name

® list of parameters

®list of statements

®return type.

Function code is used repeated by whenever you want. Function can be placed at head (or) body section.

Defining functions:- Function name can be’function’ keyword. The function name can be any combination of digits, letters and underscore but, cannot contain a space.

Syntax:- function functionname(parameters)
               {
                 - - - - - -
                 Code;
                 - - - - - -
                Return type;
              }
Eg:-     function   a ( )
            {
               Code;
            }

Program:

<html>
<head>
<title> function < /title>
<script language = “java script”>
function add ()
{
   var a, b;
   a = 10, b = 20;
   document.write (“sum is “ + sum);
}
< /script>
< /head>
<body>
<script>
     add ();
< /script>
< /body>
< /html>

Calling a function by an event:

<html>
<head>
<title> function < /title>
<script language = “java script”>
function add ()
{
   var a, b;
   a = 10, b = 20;
   document.write (“sum is “ + sum);
}
< /script>
< /head>
<body>
<input type = “button” value = “click here” onClick = “add( )”>
< /body>
< /html>

parameter passing:- 
We can pass data to the function as arguments and that can be available inside the function. Not every function accepts parameters not all values have to be passed as parameters. When a function receives a value as parameter that value is given in a name and can be assumed using that name by the function.

The names of parameter are taken from the function definition and are applied in the order in which parameters are passed in.

Eg:-

<html>
<head>
<title> function < /title>
<script language = “java script”>
function add (a, b)
{
   var res = a + b;
   document.write  (“ result in function “ + res);
   return (res);
}
< /script>
< /head>
<body>
<script>
     var r = add (10, 20);
     document.write(“ result is “ + r);
< /script>
< /body>
< /html>

Examine the function call:- In this parameter are passed as arrays. Every function has two parameters that can be used to find information about the parameters.

Function arguments:- This is the array of parameters that have been passed.

Function argument length:- This the number of parameters that have been passed in to the function you could easily use this to check that all parameters have been sent and to issue a warning they haven’t.

Returning values:- Variables that you declare in a function are local to that function  you cannot get their value outside the function unless you pass them around as parameter so a mechanism is need to return a value form a function. That mechanism is provided by the ‘return statement’.

Related

Web Technology 7500830257751109976

Post a Comment

emo-but-icon
:noprob:
:smile:
:shy:
:trope:
:sneered:
:happy:
:escort:
:rapt:
:love:
:heart:
:angry:
:hate:
:sad:
:sigh:
:disappointed:
:cry:
:fear:
:surprise:
:unbelieve:
:shit:
:like:
:dislike:
:clap:
:cuff:
:fist:
:ok:
:file:
:link:
:place:
:contact:

item