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

Ceaser Cipher

Adriaan
4,283 views

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() {
console.log("woof!");
}
// Here we call the function 3 times.
bark();
bark();
bark();
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Parameters

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
bark(5);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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);
console.log(answer);
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

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

Your Turn
// { autofold
module.exports = multiply;
// }
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
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!
JOIN US ON DISCORD
Online Participants