In programming, as we start to write bigger and more complex programs, one thing we will start to notice is we will often have to repeat the same set of steps in many different places in our program.
Let’s imagine we were building an application to help people plan trips! When using a trip planning application we can say a simple procedure could look like this:
1. Establish your origin and destination 2. Calculate the distance/route 3. Return the best route to the user
We will perform these three steps every time users have to travel between two points using our trip application. In our programs, we could rewrite the same procedures over and over (and over) for each time we want to travel, but there’s a better way! Python gives us a useful concept called functions.
Functions are a convenient way to group our code into reusable blocks. A function contains a sequence of steps that can be performed repeatedly throughout a program without having to repeat the process of writing the same code again.
In this lesson, we are going to explore the idea of a function by slowly building out a Python program for our trip planning steps!
At the end of this lesson, you’ll know how to:
- Write a function and return values from it.
- Allow functions to take custom input.
- Experiment with how functions access our other python code.
And much more!
Review the visual for the function
Notice how the function
navigation_steps() serves as a container for the three steps in the procedure and can be reused across multiple users as they plan their trips to different locations.
Click Next when you are ready to learn more about functions.