A function is a set of instructions for the computer to run.
Functions can take in data, work with it, and return a result. The practice editor contains a function named returnFive
that returns the value 5
.
Try running the practice editor and see what happens.
Calling a Function
Functions are like recipes. Writing a recipe and following a recipe are two different things. Just because we write a recipe down on paper doesn't mean we automatically get food. We have to actually follow the recipe.
We just declared a function, but that's all we did. We just wrote our recipe down on paper.
In order to run the function (follow our recipe and get a result), we have to call it. To call a function, use the function's name with parentheses behind it.
Add returnFive()
as a new line in the editor. See what happens.
Summary
Once we call the function, we actually get the return value of the function back. We see 5
in the results.
Functions are important because they allow us to reuse code. After we write a function once, we can use that function over and over again in other places in our code.