Python functions

Got a chunk of code that you are going to reuse a bunch of times? Consider turning it into a function, it will save you time!

Functions allow you to reuse multiple lines of code by just typing the name you give it and any information it needs to run.

To make a function, first create a definition starting with the word def. To use it, call it by saying the name of the function where you want to run it.

Here's an example of a help menu. Instead of typing this in every room of a text based game, the programmer can just type it once, and call its name any time the player requests to see the menu:



Here's another example. Let's say you're doing a particular math problem over and over again, like collision, or in this case, finding the average of three numbers. Before turning your code into a function, it might look like this:



Here's what the code looks like after being turned into a function: