Functions are sets of instructions. These are reusable building blocks which are used to execute a specific task whenever we need it.
Uses of function:
1) Function keyword — function
What it means:
This word tells the computer:
👉 "I am going to create a function."
2) Function name — sum
What it means:
This is the name of the function.
You use this name later to run the function.
3) Parameters — (x, y)
What it means:
These are inputs the function expects to receive.
Simple example:
Like a calculator asking:
👉 "Give me two numbers."
Here:
x = first number
y = second number
4) Function body — { }
What it means:
This is the working area of the function.
All instructions are written inside these curly brackets.
Simple example:
Like a kitchen where cooking happens.
5) Return keyword — return
What it means:
This sends the result back after the work is done.
Simple example:
Like giving the answer after solving a problem.
👉 "Here is your result."
6) Statement — return x + y;
What it means:
This is the actual task the function performs.
Here it means:
👉 Add x and y
Simple example:
If:
x = 4
y = 5
Result:
9
7) Arguments — (4, 5)
What it means:
These are the real values sent to the function.
Simple example:
Parameters are placeholders:
(x, y)
Arguments are actual numbers:
(4, 5)
8) Function call — sum(4, 5)
What it means:
This tells the function to run.
Simple example:
Like calling someone on the phone:
👉 "Sum, please do this calculation."
9) Variable — let result
What it means:
This stores the answer returned by the function.
Simple example:
Like saving the result in a box.
result = 9
Simple Real-Life Example
function like a juice machine:
Programming Term Real-Life Meaning
Function Juice machine
Parameters Fruits
Function body: Machine process
Return Juice output
Arguments Actual fruits given
Function call Pressing the button
Types of function:
Other lists of functions which were noticed while referring to other sites:
- Generator Function
- Higher-Order Function
- Nested Functions
- Pure Functions
- Default Parameter Function
- Rest Parameter Function
Scenario 1:
*Function to find even or odd number *
<!DOCTYPE html>
<html>
<head>
<title>Odd or Even</title>
</head>
<body>
<script>
// Declare a number
let number = 9;
// Function to check odd or even
function addorEven() {
if (number % 2 == 0) {
return 100; // Even number
}
else {
return 200; // Odd number
}
}
// Call the function
let findoutvalue = addorEven();
// Print the result
console.log(findoutvalue + 20);
</script>
</body>
</html>
Output
220
Scenario 2:
Function to find the biggest number
<!DOCTYPE html>
<html>
<head>
<title>Function Demo</title>
</head>
<body>
<script>
// Function to find the biggest number
function bigNumber(a, b, c) {
if (a > b && a > c) {
return "A is biggest";
}
else if (b > a && b > c) {
return "B is biggest";
}
else {
return "C is biggest";
}
}
// Call the function
let numberInput = bigNumber(10, 11, 9);
// Print the result
console.log("The final result of biggest number: " + numberInput);
</script>
</body>
</html>
Output
The final result of biggest numberB is bigger
SOCIAL SHARE CARD GENERATOR