Functions allow us to reuse bits of code. For example, say that we are calculating tax on transactions. We can wrap up our code in a functional unit and give it a name:
def calculate_tax(price, tax):
sales_tax = price * tax
total = price + sales_ tax
From then on we can just call that name and pass it some information (called arguments):
This will save us time as well as make our code easier to review. Lets take a look at some more in-depth learning resources.
- Chapter 4 - Functions (Slides, Printable Slides, Streaming Video, Download Video, Lecture Audio)
- Worked Exercise Screencasts: 4.4 (suggest download)
Post chapter excercises and questions below.
Q: What is an example of a machine or process that takes external input and returns a modified output?
Q: How many tasks do you think that an individual function should generally perform?