So, let’s begin.
The concept of functions is the same in almost all programming languages. By creating a function, we can reuse code many times in our program. It saves time and effort.
After creating that function, you don’t have to write that code again. Whenever you will call the function in your program the statements will automatically execute.
In the parenthesis (), we use parameters separated by commas. Like this (parameter1, parameter2, parameter, …).
Parameters (also known as function arguments) are the values that are passed during the function calls.
The image below is clearly showing an example of the syntax.
- Parameters: parameters are values that we pass to a function when we call it.
- Code inside curly brackets will execute when we make a function call.
Real Life Example
In real life, for example, you work in a school. And your only job is to make the results of the mathematics papers. And every time, the principal of the school sends a peon to take the student’s marks sheet of a math paper.
Now imagine, in this whole process, you’re a function that is doing the job by making marks sheet. The peon is an argument or parameter, that is passing the information to the (function) about the math paper. The principal is a user that is making a function call mean sending the arguments. The marks sheet of a math paper is a function’s return value.
Hope you have understood.
Let’s start with a simple one. I take a real-life example to demonstrate it.
Let’s suppose, we have a circle and we want to find the area of a circle. You know the formula to find the area of a circle. You can see this in the image below.
To find the area of a circle, we find the radius of a circle first. And then take its square and multiply it by pi. It’s a simple example.
With this function, whenever we want to know the area of a circle in our program, we can do it by simply calling a function at any time. It will save much time and you don’t have to write formulas again.
areaofcircle()followed by the function keyword.
- Then, I defined a parameter inside the parenthesis which is r. It will pass the value of the radius of a circle to function.
- After the return, I used the formula to find the area of a circle.
It’s time to call a function to check that is it working correctly or not.
To call a function, write a <p> tag inside the body of the HTML and give it an id with any name. This will display the answer on the HTML webpage.
Then, see line no. 18 in the code below.
To call a function to do the job, I used a document.getElementById method to write the answer inside HTML <p> tags.
After that inside the function area of a circle (), I used the value of the radius that will go to the function and return the answer.
When I called a function, I gave it the value of the circle’s radius which is 16. You can give any value. The function will return the answer in HTML which will output this below after executing.
If the radius of a circle is 16 then the area of a Circle will be: 804.2496
If you want to know more about the functions then see the guide on Wikipedia.
If you have any questions then feel free to ask in the comment section below.