7 - Functions
When you are designing code, there will be many instances where you will design a set of code which performs some functionality in your program. You may want to re-use that same functionality somewhere else in your code, or include that code in another program you write. One way you could do this is by rewriting all of the code you want to use every place that you need it. This works, but what happens when you reuse that code 100 or 1000 times. It gets to be quite messy. There is another way though -- functions. Functions allow you to reuse blocks of code by calling one line instead of the entire code block. This makes reading and writing code much easier. In this tutorial we will go through how to define functions, calling them once you define them, what to put in your function, and how to return values from functions.
Now that we know that functions exist, how do we use them. The first step is to define them.
Once you create your function, you will need a way to use it.
You may want to pass variables into your functions from other parts of your program. Function arguments allow you to do this. There are three main types of arguments that you can use in a function: Default, Required, Keyword, and Variable.
Return statements can be used to pass a value back to the calling line. For example, instead of calling an add function and setting a variable equal to the result inside of the function, you could return the value to a variable. This is probably hard to think of from that description so let's look at an example.