0% found this document useful (0 votes)
2 views

javascript_fns

Uploaded by

grojamani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

javascript_fns

Uploaded by

grojamani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

What is a 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 be passed into functions and used within
the function.
 Functions always return a value. In JavaScript, if
no return value is specified, the function will
return undefined.
 Functions are objects.
Define a Function.
There are a few different ways to define a function in
JavaScript:
A Function Declaration defines a named function. To
create a function declaration you use
the function keyword followed by the name of the
function. When using function declarations, the
function definition is hoisted, thus allowing the
function to be used before it is defined.
function name(parameters){
statements
}

Example
Calculate the product of two numbers, and return the result:

var x = myFunction(4, 3); // Function is called, return value


will end up in x

function myFunction(a, b) {
return a * b; // Function returns the product of a
and b
}

The result in x will be:

12

<html>
<head>

<script type="text/javascript">

var count = 0;

function countVowels(name)

for (var i=0;i<name.length;i++)

if(name[i] == "a" || name[i] == "e" || name[i] == "i" || name[i] == "o" || name[i] == "u")

count = count + 1;

document.write("Hello " + name + "!!! Your name has " + count + " vowels.");

var myName = prompt("Please enter your name");

countVowels(myName);

</script>

</head>

<body>

</body> </body>

</html>

o/p Hello rojamani!!! Your name has 4 vowels.


RETURN VALUE

<html>

<head>

<script type="text/javascript">

function returnSum(first, second)

var sum = first + second;

return sum;

}
var firstNo = 78;

var secondNo = 22;

document.write(firstNo + " + " + secondNo + " = " + returnSum(firstNo,secondNo));

</script>

</head>

<body>

</body>

</html> O/P 78 + 22 = 100

You might also like