Back
Close

Ceaser Cipher

Adriaan
9,309 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:

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.

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:

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

Your Turn
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