Functions

In programming, functions are reusable snippets of code that you can 'call' when needed. Functions are made up of statements and can also accept arguments or parameters.

Functions

What is a function?

In programming, a function is one of the fundamental building blocks. Think of a function as a recipe. A recipe allows you to take a batch of ingredients with a set of instructions, and it allows you to bake that perfect cake every single time.

Well, a function works in the same way. You define a set of instructions that are bundled up into a block of code, and it allows the computer to repeat those steps every single time that you call upon that function.

How do you write a function in JavaScript?

Let's say we want to create a function that will take any two numbers and multiply them by each other, then return the result.

Step 1: Declare your function

We'll start by declaring a function using the function keyword. Then we will give our function a relevant name so that we can reference it later on. Let's call our function multiplyNums since we will be multiplying two numbers together. The function name is followed by a set of parentheses, a set of curly brackets, then a semi-colon. Now our function is looking something like this:

function multiplyNums(){

};

TIP: multiplyNums is written in camel case. This is a standard when naming functions and variables in JavaScript.

Step 2: Provide the function arguments

Within the parentheses, we will pass two arguments or parameters to our function. These two arguments will represent the two numbers, whatever they may be, that we want to multiply.

function multiplyNums(a, b){

}; 

TIP: You will often hear the terms arguments and parameters used interchangeably.

Step 3: Define what your function will do

We want to define what we want our function to do everytime that it's called upon by placing our code within the curly brackets. In this case, we want to take our two function arguments, and multiply them by each other, then return the result:

function multiplyNums(a, b){
    return a * b; 
}; //return the result of 'a' multiplied by 'b'

TIP: In JavaScript, you can use standard arithmetic operators when working with integers.

Step 4: Call your function

Last but not least, we need to call our function, otherwise the computer won't know that we're trying to use it. To do this we would write out our function name, and pass in any two numbers, a and b, and the computer will return our result.

Let's say we want to multiply the numbers 17 and 91. We would call our function using multiplyNums(17, 91);

function multiplyNums(a, b){
    return a * b; 
};
multiplyNums(17, 91); //call the function

Paste this code snippet in your browser console to try it out

TIP: In JavaScript, you can write comments using two slashes: //. Comments are not rendered by the computer, and are useful when trying to remember what a piece of code is used for.

And that's all there is to it! Now we have a reusable code snippet that we can reference and call upon anytime we need to multiply two numbers together.

Help us improve our content