div></div></figure><blockquote id="3c23"><p><i>Everything that starts with two slashes <code>//</code> is a comment, it is ignored and will not be executed.</i></p></blockquote><p id="55ec">So what is happening here? The function is getting called four times in a row, once for each beverage. The minimum age for the beverage is passed as a parameter: 1 for soda, 18 for beer, 18 for wine, and 21 for wodka. So we have 4 output messages, that should be something like this:</p><div id="3518"><pre><span class="hljs-built_in">Here</span> <span class="hljs-variable">you</span> <span class="hljs-variable">go</span><span class="hljs-operator">!</span>
<span class="hljs-built_in">Here</span> <span class="hljs-variable">you</span> <span class="hljs-variable">go</span><span class="hljs-operator">!</span>
<span class="hljs-built_in">Here</span> <span class="hljs-variable">you</span> <span class="hljs-variable">go</span><span class="hljs-operator">!</span>
<span class="hljs-built_in">Not</span> <span class="hljs-variable">old</span> <span class="hljs-variable">enough</span><span class="hljs-operator">,</span> <span class="hljs-variable">sorry</span><span class="hljs-operator">.</span></pre></div><p id="80ba">It would be nice to see in the output what beverage it is, right? We can do that by passing the name of the beverage as parameter as well. We then use it in the <code>console.log("...")</code> text, like this:</p>
<figure id="c3a1">
<div>
<div>
<iframe class="gist-iframe" src="/gist/teresaholfeld/37f12fc4637cecf9ae0ba3675c916d4b.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="5e86">What happens here is: we define an additional parameter called <code>beverage</code>.
This parameter is then treated as a variable in the block of the function.</p><blockquote id="6ef3"><p><i>If you don’t remember variables, check out the <a href="https://readmedium.com/fundamentals-of-programming-297479a50a3"><b>previous blog post</b></a> where we covered them.</i></p></blockquote><p id="378e">In the <code>console.log</code> statements, we use the variable <code>beverage</code> and put it into the output text.</p><p id="8b4a">As we learned above, this is just the function declaration. We also need to call it. Let’s call the function once for each beverage:</p>
<figure id="a8f9">
<div>
<div>
<iframe class="gist-iframe" src="/gist/teresaholfeld/ffaf1661e526c6cc896ca81f92d4f7cf.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="0514">This should give an output like this:</p><div id="a53b"><pre><span class="hljs-built_in">Here</span> <span class="hljs-variable">you</span> <span class="hljs-variable">go</span><span class="hljs-operator">!</span> <span class="hljs-variable">Have</span> <span class="hljs-variable">a</span> <span class="hljs-variable">Soda</span>
<span class="hljs-built_in">Here</span> <span class="hljs-variable">you</span> <span class="hljs-variable">go</span><span class="hljs-operator">!</span> <span class="hljs-variable">Have</span> <span class="hljs-variable">a</span> <span class="hljs-variable">Beer</span>
<span class="hljs-built_in">Here</span> <span class="hljs-variable">you</span> <span class="hljs-variable">go</span><span class="hljs-operator">!</span> <span class="hljs-variable">Have</span> <span class="hljs-variable">a</span> <span class="hljs-variable">Wine</span>
<span class="hljs-built_in">Not</span> <span class="hljs-variable">old</span> <span class="hljs-variable">enough</span> <span class="hljs-variable">for</span> <span class="hljs-variable">Wodka</span><span class="hljs-operator">,</span> <span class="hljs-variable">sorry</span><span class="hljs-operator">.</span></pre></div><p id="1825">We now have one console log for each output with the beverage name.</p><h1 id="bb4f">Parameter Data Types</h1><p id="3f09">As you remember from our <a href="https://readmedium.com/fundamentals-of-programming-297479a50a3"><b>previous blog post</b></a>, variables have data types, e.g. integer (number) or string (text). The same is true for parameters. Parameters are just like variables that are put into functions.</p><p id="66ce">In the function call</p>
<figure id="1941">
<div>
<div>
<iframe class="gist-iframe" src="/gist/teresaholfeld/298332fe99da3913639d168f275471ff.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="e282">you can see that we pass parameters of different data types: First, the beverage name — as a string. Second, the minimum age for the beverage — as an integer.</p><p id="53c3">In the function declaration you need to be a bit conscious about the data type, because you don’t see it. It just shows the parameter name:</p>
<figure id="ea25">
<div>
<div>
<iframe class="gist-iframe" src="/gist/teresaholfeld/fe1cdeac4509073adfb2a56314761946.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="17a9">Therefore you need to keep in mind how you use the parameter in the code of the function.</p><p id="b6a0">In our case, <code>beverage</code> is used later in the code as a string:</p>
<figure id="7b69">
<div>
<div>
<iframe class="gist-iframe" src="/gist/teresaholfeld/9ccd461467842ba22a459d6d4648dc37.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="d282">And <code>minAge</code> is used as an integer:</p>
<figure id="4c45">
<div>
<div>
<iframe class="gist-iframe" src="/gist/teresaholfeld/29d76771aa08fd1e0bbd068cc9ab4c24.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="a357">So we need to make sure that when we call the function, we pass the right data type at the right place. In this case: first a string, then an integer.</p>
<figure id="ce86">
<div>
<div>
<iframe class="gist-iframe" src="/gist/teresaholfeld/298332fe99da3913639d168f275471ff.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="7087">This is nothing to worry about, you just need to be careful that you pass the right data types into the function at the right place.</p><h1 id="d8c3">Return Values</h1><p id="7243">Functions can have a <b><i>return value</i></b>. What does that mean?
It means we can make a function return something. In other words, we can get an output from the function.</p><p id="48e5">Look at this example:</p>
<figure id="a06c">
<div>
<div>
<iframe class="gist-iframe" src="/gist/teresaholfeld/0f6ade308b2dada4501075fb57382d83.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="b413">This function returns <code>true</code> or <code>false</code>, because it has a return value:</p>
<figure id="c55f">
<div>
<div>
<iframe class="gist-iframe" src="/gist/teresaholfeld/78fcdd21ca05ddf9b686909b69863fcf.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
Options
</div>
</figure></iframe></div></div></figure><p id="9b09">Here, <code>oldEnough</code> is the variable that we created one line before. With <code>return</code>, we give it back: we say that the output of the function should be whatever <code>oldEnough</code> is (<code>true</code> or <code>false</code>).</p><p id="15ef">So if we call the function, we get a value back, and can, for example, save this value in a variable:</p>
<figure id="700f">
<div>
<div>
<iframe class="gist-iframe" src="/gist/teresaholfeld/e504bd6610a1018ab3cf661ba86937d6.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="05c7"><code>isOldEnough(19, 18)</code> is the function call – we call our new function here with the parameters <code>age</code> (19) and <code>minAge</code> (18).</p><p id="672d">Then we save it in a variable: <code>var canDrink</code>.</p><p id="7287">You can print it on the console to see the value:</p>
<figure id="9c24">
<div>
<div>
<iframe class="gist-iframe" src="/gist/teresaholfeld/e3fafffac022b9b2cb69160c53b7fc40.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="6126">This should print <code>true</code> in the console.</p><p id="f263">You can play around with it, change the numbers that you pass into the function, and see how the result changes.</p><h1 id="32c0">Putting it all together</h1><p id="9f63">Let’s wrap up what we learned so far and apply everything to the first example of this article.</p><p id="13da">First, we are going to write down our function declarations. We will need 2 functions:</p><ul><li><code><b>isOldEnough(age, minAge)</b></code> that returns <code>true</code> or <code>false</code></li><li><code><b>canWeDrinkThis(beverage, minAge)</b></code> that makes our console output</li></ul><p id="376c">So here they go:</p>
<figure id="6eb7">
<div>
<div>
<iframe class="gist-iframe" src="/gist/teresaholfeld/a19867d74a103388d7e23bf0b69ed64f.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="64b4">You see here that I am already calling the function <code>isOldEnough()</code> in the function <code>canWeDrinkThis()</code>. And yes, of course you can make a function call inside a function declaration. This is common practice and it is how you use functions, basically.</p><p id="d4e5">Now there are two lines of code that we could make even shorter:</p>
<figure id="fe60">
<div>
<div>
<iframe class="gist-iframe" src="/gist/teresaholfeld/0f3babc6ef67b9d3ce43a5f05d14ce69.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="c449">This would be the same as writing:</p>
<figure id="3fb8">
<div>
<div>
<iframe class="gist-iframe" src="/gist/teresaholfeld/cdcbf773cbef98cdc796cf6ef1888379.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="048e">You see what I did here? I skipped creating a new variable <code>oldEnough</code>, and used the function directly in its place in the <code>if (...)</code> brackets. <b>We replaced the variable with the function call.</b></p><p id="d859">This is possible, because the function has a return value. We know the return type of our function is <code>true</code> or <code>false</code>, so if we put this in the <code>if (...)</code> clause, this works.</p><blockquote id="bac0"><p><b><i>Note:</i></b><i> If the function would return something different, like a string or an integer, we couldn’t use it in the <code>if (...)</code> clause.</i></p></blockquote><p id="bbda">This means: <b>If the function has a return type, I can treat a function call just the same way as a variable</b>. This is suitable if I call it only once and don’t need to save the value for re-usage. In our case, it makes the function one line shorter. And shorter is always better. 😊</p><p id="f821">So we call the function <code>isOldEnough()</code> now in our other function <code>canWeDrinkThis()</code>, but one important thing is missing: we also need to call the function <code>canWeDrinkThis()</code>.
We want to do this once for each beverage:</p>
<figure id="ed1b">
<div>
<div>
<iframe class="gist-iframe" src="/gist/teresaholfeld/87a21e27d907992aba7e6a020ce895d1.js" allowfullscreen="" frameborder="0" height="undefined" width="undefined">
</div>
</div>
</figure></iframe></div></div></figure><p id="e0c5">This is giving you the output you want:</p><div id="6d45"><pre><span class="hljs-built_in">Here</span> <span class="hljs-variable">you</span> <span class="hljs-variable">go</span><span class="hljs-operator">!</span> <span class="hljs-variable">Have</span> <span class="hljs-variable">a</span> <span class="hljs-variable">Soda</span>
<span class="hljs-built_in">Here</span> <span class="hljs-variable">you</span> <span class="hljs-variable">go</span><span class="hljs-operator">!</span> <span class="hljs-variable">Have</span> <span class="hljs-variable">a</span> <span class="hljs-variable">Beer</span>
<span class="hljs-built_in">Here</span> <span class="hljs-variable">you</span> <span class="hljs-variable">go</span><span class="hljs-operator">!</span> <span class="hljs-variable">Have</span> <span class="hljs-variable">a</span> <span class="hljs-variable">Wine</span>
<span class="hljs-built_in">Not</span> <span class="hljs-variable">old</span> <span class="hljs-variable">enough</span> <span class="hljs-variable">for</span> <span class="hljs-variable">Wodka</span><span class="hljs-operator">,</span> <span class="hljs-variable">sorry</span><span class="hljs-operator">.</span></pre></div><p id="bbb6">As a summary, here is what the whole program should look like now:</p>
<figure id="899e">
<div>
<div>
Much shorter and no unnecessary code duplication anymore! ✨</p><blockquote id="250d"><p><i>You can look up the solution code <a href="https://repl.it/@TeresaHolfeld/ShimmeringMurkyFact"><b>here</b></a>. Feel free to run it and play around with it.</i></p></blockquote><p id="ee15"><b>Well done!</b> If you followed the tutorial, you should be able to write functions, tell a function declaration from a function call, pass parameters to functions, and let a function return a value.</p><p id="ee1e">This is not easy. If you have the feeling that you didn’t quite get 100% of it, stay patient. Play around with the functions you have, and get some practice. Everything will get more clear and easy with practice.</p><blockquote id="5e52"><p><i>As an exercise, you can make the function <code>canWeDrinkThis(...)</code> return true or false and print it on the console. You can look up the solution for this exercise in our Repl <a href="https://repl.it/@TeresaHolfeld/AntiqueAlertDebuggers"><b>here</b></a>.</i></p></blockquote><p id="2bf0">We hope this tutorial helped you understand functions. Were you able to follow? If yes, give this article some claps. 👏 And if not, write a comment and ask your question. Thanks for reading, and happy coding! 👩💻</p></article></body>
In this blog post, we will have a look at functions. You will learn how you can build functions in JavaScript, how you can pass data to them and get data back.
Let’s dive right in! 🏊♀️
Let’s have a look at this example:
You can always look up and run the examples of this blog post here. We would suggest that you create your own Repl at repl.it and do all the examples yourself. Learning by doing is the best method for learning to code.
You can see that we repeat a code snippet multiple times. These lines are repeated twice:
and two other blocks are very similar.
We can actually put this repeated code snippet into a block. This block then gets a name, and we can use it wherever we need. This is called: a function.
Functions
If we take above example and put the duplicated code snippet into a function, it will look like this:
The function keyword tells us: Here comes a function!
Then there’s the name of the function (canWeDrinkThis). You can name it however you like.
The name of the function is followed by an opening and closing bracket ().
And then the opening curly bracket { signals: here comes the code block! It ends with the closing curly bracket }, which signals that this is the end of the code block.
Inside the curly brackets goes the code snippet that the function is supposed to do.
Now this is only the “definition” of the function. We call it the function declaration.
What does this mean? It means that we just defined what the function is going to do.
In order to actually run the function, so that our code snippet is executed and does something, we need to call it:
You call the function by using the function name and brackets (). This is the function call.
This is important to remember: You first declare the function, then you call it. It is important that you know the difference between function declaration and function call. If this is a bit confusing, hang on. It will get more clear with practice.
The function gets only executed when you call it. You can, in fact, call it multiple times, and it will get executed multiple times. Try it:
What happens if you run this? It gets called three times, so you will have three console outputs.
This is a bit like creating a recipe: When we declare the function, it is like we write the recipe down on a sheet of paper. When we call the function, it is like we are actually cooking the dish of the recipe.
Now with recipes, there’s usually a list of ingredients at the beginning: the things that go into the meal that you are about to cook. Functions have a similar concept: parameters.
Function Parameters
A function parameter is data that goes into a function: the input of a function.
We also call function parameters params.
If you look at our first example you see: we have four blocks that are very similar. The only thing that changes is the age threshold for each beverage. We could make our function more flexible to take this into account, by using parameters. We can pass the minimum age for a beverage into the function as a parameter.
This would look like this:
Here, minAge is the minimum age that is allowed for a beverage. It is passed as a parameter.
For beer, for example, the minAge would be 18. We put that number inside the brackets () when we call the function:
And similarly, we could call that function for the other beverages:
Everything that starts with two slashes // is a comment, it is ignored and will not be executed.
So what is happening here? The function is getting called four times in a row, once for each beverage. The minimum age for the beverage is passed as a parameter: 1 for soda, 18 for beer, 18 for wine, and 21 for wodka. So we have 4 output messages, that should be something like this:
Hereyougo!Hereyougo!Hereyougo!Notoldenough,sorry.
It would be nice to see in the output what beverage it is, right? We can do that by passing the name of the beverage as parameter as well. We then use it in the console.log("...") text, like this:
What happens here is: we define an additional parameter called beverage.
This parameter is then treated as a variable in the block of the function.
If you don’t remember variables, check out the previous blog post where we covered them.
In the console.log statements, we use the variable beverage and put it into the output text.
As we learned above, this is just the function declaration. We also need to call it. Let’s call the function once for each beverage:
We now have one console log for each output with the beverage name.
Parameter Data Types
As you remember from our previous blog post, variables have data types, e.g. integer (number) or string (text). The same is true for parameters. Parameters are just like variables that are put into functions.
In the function call
you can see that we pass parameters of different data types: First, the beverage name — as a string. Second, the minimum age for the beverage — as an integer.
In the function declaration you need to be a bit conscious about the data type, because you don’t see it. It just shows the parameter name:
Therefore you need to keep in mind how you use the parameter in the code of the function.
In our case, beverage is used later in the code as a string:
And minAge is used as an integer:
So we need to make sure that when we call the function, we pass the right data type at the right place. In this case: first a string, then an integer.
This is nothing to worry about, you just need to be careful that you pass the right data types into the function at the right place.
Return Values
Functions can have a return value. What does that mean?
It means we can make a function return something. In other words, we can get an output from the function.
Look at this example:
This function returns true or false, because it has a return value:
Here, oldEnough is the variable that we created one line before. With return, we give it back: we say that the output of the function should be whatever oldEnough is (true or false).
So if we call the function, we get a value back, and can, for example, save this value in a variable:
isOldEnough(19, 18) is the function call – we call our new function here with the parameters age (19) and minAge (18).
Then we save it in a variable: var canDrink.
You can print it on the console to see the value:
This should print true in the console.
You can play around with it, change the numbers that you pass into the function, and see how the result changes.
Putting it all together
Let’s wrap up what we learned so far and apply everything to the first example of this article.
First, we are going to write down our function declarations. We will need 2 functions:
isOldEnough(age, minAge) that returns true or false
canWeDrinkThis(beverage, minAge) that makes our console output
So here they go:
You see here that I am already calling the function isOldEnough() in the function canWeDrinkThis(). And yes, of course you can make a function call inside a function declaration. This is common practice and it is how you use functions, basically.
Now there are two lines of code that we could make even shorter:
This would be the same as writing:
You see what I did here? I skipped creating a new variable oldEnough, and used the function directly in its place in the if (...) brackets. We replaced the variable with the function call.
This is possible, because the function has a return value. We know the return type of our function is true or false, so if we put this in the if (...) clause, this works.
Note: If the function would return something different, like a string or an integer, we couldn’t use it in the if (...) clause.
This means: If the function has a return type, I can treat a function call just the same way as a variable. This is suitable if I call it only once and don’t need to save the value for re-usage. In our case, it makes the function one line shorter. And shorter is always better. 😊
So we call the function isOldEnough() now in our other function canWeDrinkThis(), but one important thing is missing: we also need to call the function canWeDrinkThis().
We want to do this once for each beverage:
As a summary, here is what the whole program should look like now:
Nice, isn’t it? 😊
Much shorter and no unnecessary code duplication anymore! ✨
You can look up the solution code here. Feel free to run it and play around with it.
Well done! If you followed the tutorial, you should be able to write functions, tell a function declaration from a function call, pass parameters to functions, and let a function return a value.
This is not easy. If you have the feeling that you didn’t quite get 100% of it, stay patient. Play around with the functions you have, and get some practice. Everything will get more clear and easy with practice.
As an exercise, you can make the function canWeDrinkThis(...) return true or false and print it on the console. You can look up the solution for this exercise in our Repl here.
We hope this tutorial helped you understand functions. Were you able to follow? If yes, give this article some claps. 👏 And if not, write a comment and ask your question. Thanks for reading, and happy coding! 👩💻