0% found this document useful (0 votes)
2 views3 pages

CSS Practical No.4 Writeup & Program

Uploaded by

Rupa Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views3 pages

CSS Practical No.4 Writeup & Program

Uploaded by

Rupa Shinde
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Practical No.4: Develop JavaScript to implement functions.

Introduction:

Functions are very important and useful in any programming language as they make the
code reusable. A function is a block of code which will be executed only if it is called. If you
have a few lines of code that needs to be used several times, you can create a function
including the repeating lines of code and then call the function wherever you want.

What is Function?

 A function is a group of reusable code which can be called anywhere in your


program.

 This eliminates the need of writing the same code repeatedly.

 It helps programmers in writing modular codes.

 Functions allow a programmer to divide a big program into a number of small and
manageable functions.

Declaring A Function:

 Before we use a function, we need to define it. The most common way to define a
function in JavaScript is by using the function keyword, followed by a unique
function name, a list of parameters (that might be empty), and a statement block
surrounded by curly braces.

 Syntax:

function functionname(parameter-list)

statements

Adding An Arguments:

 We can pass some arguments to the function.

 Syntax:

function functionname(argument1, argument2,….. argumentn)

statements
}

Returning A Value From A Function:

 A JavaScript function can have an optional return statement.

 This is required if you want to return a value from a function.

 This statement should be the last statement in a function.

 The return value is either stored in variable or directly display in browser.

Conclusion: We understand that how implement functions JavaScript.


JavaScript to check vowels from given string-using function.

<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>
</html>

Output: Hello Polytechnic...!!! Your name has 3 vowels.


-------------------------------------------------------------------------------------------------------------------
JavaScript Return Value using function.

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

Output: 78 + 22 = 100

You might also like