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.