Functions are a programming concept that allow us to organize our code into sections. This organization makes code easier to read, but also allows the programmer to reuse code throughout a program. As an example, suppose you are making a program that completes the following task.
- The user is prompted to input numbers until they input “quit”. Each input is run through the formula (input * 2 + 2), and appended to a list
- Once the user types quit, they are prompted for a final input as a value to search for. This value is also run through the formula (input * 2 + 2), and the list is searched for the value
- If the value is in the list, output that the value is found. Otherwise, output that the value isn’t found
For this example, the following code would implement the logic required.
Now, suppose that suddenly the logic changed, and we no longer wanted to take input * 2 + 2, but rather wanted to do input * 3 + 2. Notice that in the current code, you would need to change the logic in two places.
This is a great example of a situation where functions and code reuse become useful. We can apply functions to this problem to make it so that if the logic ever changes, we only need to fix it in one place. For this example, we will create a function that takes an input, multiplies it by 2, then adds 2, and returns the new value. The code to do this is shown below.
Def is a keyword to state that we are defining a function, and applyFormula is the name of the function. Whenever we want to use the code in this function, we refer to the name of the function to call it. In the brackets, we tell Python what we expect to get with the function, which we refer to as the arguments. In this case, we want to user to give us one value, which we are calling inValue. We then take the inValue and apply the formula inValue * 2 + 2. After this, we do return inValue to tell the program to provide the user back the value of inValue, now that we have adjusted it. We can change our existing code to use this function as shown below.
First, we define the function at the top of the code, and get the users first set of inputs
Notice that when we append the value, we call applyFormula(valueIn). When this happens, we go to the function code, with the argument valueIn. We calculate the value of the formula, and the returned value is appended to the list. The second part of the program searches the list, and also includes a call to our new function.
Now if we go back to the previous example of needing to change the formula to input * 3 + 2, we can see why functions are useful. Since each instance of applying the formula calls the function, we only need to change the formula in one place instead of two. This means there is less work us to do, and a lower chance of missing one of the changes. In general, avoiding repeating code is a good practice to keep in mind, and functions make it significantly easier to do. In the world of business and applied programming, specifications of a program change all the time, so changes like this are required often. If the program is larger, it could reduce 50 code changes to a single one, which has a great value for time and testing purposes.