# Functions

Functions are a way of organizing code for re-use and avoid duplication. A function is just a block of code with an identifier so that you can tell the computer what to execute. Imagine a function is a car and the license plate is the identifier. The same way you could tell someone to bring you a specific car by telling them the car's license plate, you could tell the computer to run a block of code based on the identifier of the function (usually a string/name). So let's look at an example of a function:

Copy code
      
    

So depending on the language, there's a special word to tell the computer that you want to define a function. For Javascript, you specify function and then you specify the identifier. In our example, the identifier is doSomething. And then depending on the language, you need to specify the begin and end of a function. For Javascript, you use the { and } brackets. Whatever is specified inside the function is what the computer will execute each time you call the function. To call the function, you use the identifier and in our Javascript example, it looks like doSomething();.

# Function Parameters

Being able to tell the computer to run a block of code is useful, but it's rare that you want it to do the exact same things over and over. That would be like hiring a painter to paint your house but you only get one color for every wall. To solve this, we can use parameters. Parameters are a way of passing a value into a function. The function can then use that value however it likes. Let's look at a basic example of passing a parameter in:

Copy code
      
    

So in our new example, we have a function paintWall that takes in a parameter color. And then we call the function, we pass in a string value of "white". And so the idea here is that we could pass in other values like "blue", "black", "green", etc. depending on what color we want. So imagine we ask the user what color they want and then we pass the color they pick to our function paintWall. This is just an example of how to use parameters.

# Function Return Value

Now that we can define a function to run a block of code and we can pass data into our function through parameters, we need to figure out how to get back data from our function. For example, imagine we have a function translate(phrase) that takes a phrase in one language and translates it for you. It would probably be helpful if you could get the translated phrase back otherwise, what was the point of translating text. So we accomplish this by returning something from our function. Let's look at how this is done:

Copy code
      
    

So each language has a way to specify what should be passed back from a function. In Javascript, we use the keyword return. In our example, we return translatedText which holds the string "hola". This isn't a real example since we're only translating a single word, but hopefully the structure makes sense. You'll also see that when we called the function, we saved the value that was returned to a new string translation. And we did this using = which is the same way we would set the value of any variable.

Next: Arrays

Join our community

Your sign-up could not be saved. Please try again.
Your sign-up was successful.

Sign-up and get tips to becoming a software engineer.