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

JavaScript Functions

The document provides an overview of JavaScript functions, explaining their purpose and structure, including user-defined and built-in functions. It includes examples of functions that calculate the area of a circle and the volume of a cylinder, demonstrating how to use parameters and return values. Additionally, it presents a task to write a program to calculate the volume of a cylinder with specific dimensions using a JavaScript function.

Uploaded by

Stephen Mwai
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 views4 pages

JavaScript Functions

The document provides an overview of JavaScript functions, explaining their purpose and structure, including user-defined and built-in functions. It includes examples of functions that calculate the area of a circle and the volume of a cylinder, demonstrating how to use parameters and return values. Additionally, it presents a task to write a program to calculate the volume of a cylinder with specific dimensions using a JavaScript function.

Uploaded by

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

JavaScript Functions

JavaScript Functions, is a set of statements that are used to perform a specific task, like adding
two numbers, finding the square of a number, or any user-defined task.

User Defined Function Definition

The following image shows the structure of a JavaScript Function

Functions can be parameterized and non-parameterized which means they may or may not
take any input. A function that does not take any parameters/inputs is known as non-
parametrized function. To return output from a function, we use the return statement Functions
can be categorized into two categories:

• User defined Function

• Built-in Function
EXAMPLE 1:
<!DOCTYPE html>
<html>
<head>
<title>Circle Area Calculator</title>
</head>
<body>
<p>Enter the radius of the circle to calculate its area:</p>

<script>
function calculateCircleArea() {
// Prompt the user to enter the radius
const r = prompt("Enter the radius of the circle:");
const radius = parseFloat(r);

// Calculate the area


const area = [Link] *r*r;
alert(`The area of the circle with a radius of ${radius} is
${[Link](2)}`);
}
// Call the function when the page loads
calculateCircleArea();
</script>
</body>
</html>
EXAMPLE 2:
<html>
<head>
<title> Calculating area of circle</title>
</head>
<body>
<p> PROGRAM TO CALCULATE AREA OF CIRCLE</p>
<script>
function Areaofcircle() {
const r = 10;
// It converts the user's input to a number
const radius = parseFloat (r);
const area = [Link] * r*r;
alert(`The area of circle whose radius is ${radius} is ${area}`);
}
// Call the function when the page loads
Areaofcircle();
</script>
</body>
</html>
EXAMPLE 3:
<html>
<head>
<title>Program on Volume of cylinder</title>
</head>
<body>
<p>A PROGRAM TO CALCULATE THE VOLUME OF A CYLINDER</p>
<script>
function volumeofcylinder() {
const r =prompt("Enter the radius of the cylinder");
const h =prompt("Enter the height of the cylinder");
const radius=parseFloat(r);
const height=parseFloat(h);
const volume=[Link]*r*r*h;
alert(`The volume of cylinder whose radius is ${radius} and
height is ${height} is ${volume}`);
}
volumeofcylinder();
</script>
</body>
</html>

TASK:
Write a program to calculate the volume of a cylinder using a JavaScript function where the
radius is 17 and the height is 14.

You might also like