Loading [Contrib]/a11y/accessibility-menu.js

Ceaser Cipher


So, in the previous section we learned how to use a loop to replace all the "a"s with a *.

What if we wanted to do this multiple times or we wanted to replace a diffrent letter? This is the perfect use for a function.

In short "A function is a code snippet that can be called by other code or by itself".

We can declare a function using the function keyword:

function foo() {
  // do something.

The code above only declares the function. It will not run unless we call it. To call it, we simply type it's name with a () behind it.

Look at the code below:

// Here we declare a function named "bark"
function bark() {
// Here we call the function 3 times.


Sometimes we want to pass some information to a function. The information we pass to a function is called a parameter.

In the code above, we defined a function called bark, and we called it 3 times in a row to make it run 3 times. However, what if we wanted to bark more times? We can pass the number of times we want to bark into the function. The parameter we pass then becomes a variable available to the function.

// We declare a function with a parameter for the amount of times we want to bark.
function bark(times) {
for (i = 0; i < times; i++) console.log("woof");
// Then we call the function and pass the number of times we want to bark as a parameter

The functions above just perforemed an action. But it didn't return any value. We can use a function to perform an action and then return a value.

In the code below, we declare a function to add two numbers together and return the result:

// Declare a function that takes 2 parameters for the numbers we want to add
function add(a, b) {
// We create a variable to hold the answer
let x = a + b;
// Then we use the return keyword to return the answer.
return x;
let answer = add(5, 10);

Now create a function named multiply which takes 2 parameters, multiplies them and returns the result.

Your Turn
// { autofold
module.exports = multiply;
// }
Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io
codingame x discord
Join the CodinGame community on Discord to chat about puzzle contributions, challenges, streams, blog articles - all that good stuff!
Online Participants