How to write a function in JavaScript, by multiplying two numbers

Chris De Leon
3 min readJul 14, 2021

--

In the software engineer world, it’s standard to know the basics of your preferred programming language, like functions. As a growing software engineer, I’m reminded to write my progress in some form. Today, I will be reviewing how to write a function by multiplying two numbers together, in JavaScript!

What in the world is a function, you might ask. Its basic definition, a set of instructions that typically run on a single line of code to complete a task. In this example, we are using JavaScript as the programming language. Keep in mind, functions apply to all languages.

Here’s why we write them. They give us the ability to visualize code in small bytes, or a group of smaller steps. It gives us the ability to reuse the code instead of revising every time. Lastly, they give us the ability to maintain a clean look for variables and their naming convention.

Now we know the why, let’s write one, or the skeleton of one.

You want to name your function something that make sense from the context. Typically, the function name starts as a verb. So we can call ours multiplyTwoNums. Nothing random like, space bunny hero or little demon kicks. The parenthesis holds the parameters, or also called arguments, for the function.

Inside the curly braces, is where the action happens. Here, you want to enter the input to calculate the what, multiplying two numbers together.

What’s happening now is, we pass in our parameters num0 and num1. And in the curly braces, we can say, return num0 * num1. The word return will tell the computer to perform the multiplication and give us the result too.

To show this, we type console.log first, this will display the output from our code, usually only visible to us engineers. Lastly, you call the function by its name, and enter the values we want to pass in. Let’s use 10 * 10.

Checking for the answer below in our terminal… it returns us 100 as calculated!

This was a quick and simple way to learn how to write a function, by multiplying two numbers together. And there are several ways to use them, but currently the scope is for the junior or just starting out person!

--

--

No responses yet