Raymond Daugherty

Functions in Python

Functions have notorious been a difficult concept to grasp for me as a self-taught “developer” (I use that term loosely because I feel I am FAR from any type of skilled developer). So, In order for me to better understand how functions… ummm… function(?), I ‘ll try my best to explain how I think functions process information. Here’s the “How to change a tire” program I’m going to use to explain the process:

#Display the instructions for each step individually.
#Ask the user to press Enter to see the next step.

def main():
    
    startup_message()
    input("Press ENTER to begin.")
        
    step1()
    input("Press ENTER to continue.")
        
    step2()
    input("Press ENTER to continue.")

    step3()
    input("Press ENTER to continue.")

    step4()
    input("Press ENTER to continue.")
    
    step5()
    input("Press ENTER to continue.")
    
    step6()
    input("Press ENTER to continue.")
    
    step7()
    input("Press ENTER to continue.")
    
    step8()
    input("Press ENTER to continue.")
    
    step9()
    print("You can now change a flat!")

def startup_message():
    print('This program will explain how to change a flat tire.')
    print('We\'ll assume you\'ve already taken the spare and jack out of the trunk, or wherever it\' being held.')
    print()
# The step1 function displays the instructions
# for step 1.
def step1():
    print('Step 1: Secure the other tires with someting to prevent them from rolling forward or backwards. This could be a large rock, a tires stop, or anything else that will keep the car from rolling forwards and backwards.')
    print()

# The step2 function displays the instructions
# for step 2.
def step2():
    print('Step 2: Using the jack handle, or a lug wrench, loosen the lug nuts on the flat tire.')
    print('** It\'s not necessary to loosen them completely, you simply want to "unstick" them so that removing them doesn\'t put much pressure on the car once it\'s on the jack.')
    print()

# The step3 function displays the instructions
# for step 3.
def step3():
    print('Step 3: Once the lug nuts are slightly loosened, slid the jack under the frame of the car, and using the jack handle slowly jack the corner of the car with the flat tire up so that the flat tire is only about an inch off the ground.')
    print('** Make sure to position it under the frame, and not the metal shell of the car, since putting it then the shell will cause the shell to bend once you begin to lift the car with the jack.')
    print()

# The step4 function displays the instructions
# for step 4.
def step4():
    print('Step 4: Once the flat tire is no longer touching the ground, Use the lug wrench or jack handle to finish removing the lug nuts from the flat tire so you can remove the flat.')
    print('** When you remove the flat, it\'s always best to lay the flat tire on it\'s side and slide it half under the side of the car that\'s off the ground. This will ensure that if the jack fails for any reason, the car doesn\'t slam onto the ground causing severe damage to the wheel base and the under carriage.')
    print()

# The step5 function displays the instructions
# for step 5.
def step5():
    print('Step 5: With the flat tire off the car, simply slide the spare onto the exposed lugs of the wheel.')
    print('** You may need to jack the car up a little to fit the spare onto the hub, since the air in the spare makes the height of the spare a little taller than the flat. If this is the case, very gently jack the car up, little by little until you can slide the tire on.')
    print()

# The step6 function displays the instructions
# for step 6.
def step6():
    print('Step 6: Once the spare is on the hub, hand tighten the lug nuts back onto the rim. Try to get them on as far as you can, and use the lug wrench or jack handle if necessary, but don\'t tighten them down with the car still on the jack.')
    print('** It\'s best to put the lug nuts on in a star shaped pattern. This allows the tire to mount evenly on the hub and will help you with putting on the latter lug nuts.')
    print()

# The step6 function displays the instructions
# for step 7.
def step7():
    print('Step 7: Once the lug nuts are somewhat on the tire, remove the spare tire from under the car, and very gently lower the car down to the ground.')
    print()

# The step8 function displays the instructions
# for step 8.
def step8():
    print('Step 8: When the car is firmly on the ground, use the lug wrench or jack handle to tighten the lugs once again. This time, try to tighten them as much as you can without using too much unnecessary effort, like standing on the lug wrench to tighten them.')
    print('** It\'s best to tighten the lug nuts on in a star shaped pattern. This allows the tire to mount evenly in this final step.')
    print()

# The step9 function displays the instructions
# for step 9.
def step9():
    print('Step 9: Once all of the lug nuts are firmly tightened down, remove the objects you used to stop the car from rolling, stow the flat somewhere in your car, and drive carefully someplace where you can get your original tire repaired.')
    print()

# Call the main function to begin the program.
main()

First, every line that begins with a “#” is simply a comment in Python. I usually place these types of comments after the line of code that the comment refers to, but that’s just a style of commenting that works for me. For this one though, I wanted to first state what I was trying to do. You can think of this as pseudocode. It just helps me organize my thoughts.

The way I wrote the code is considered to be in the “top-down” method. This means that the way the program runs is from the top to the bottom, executing code with each step. I’m not going to go into detail with every function created for this program, since a lot of it is repetition, so I’ll do the first two, and the last one.

It opens with defining the “main()” function.

def main():

The reason why we define it here is so that when it’s “called” at the end of the code, the program goes to the top and starts processing all of the information that’s been read. And since we aren’t passing any arguments to it, it simply gets followed with the “()”.

Following the defining of the “main()” function, we have indented function calls, “startup_message(), “step1()”, etc… in order.

    startup_message()
    input("Press ENTER to begin.")

This tells the program the order that each of these functions needs to be processed, and to process them one at a time, requiring the user to press “Enter” in order to move onto the next step with the input() function. (The input() function is a standard function in Python, and just reads input from the keyboard in this example.)

When the startup_message() function is called, Python skips to where that function is defined, and processes the information that is output by THAT function, and displays it to the user.

def startup_message():
    print('This program will explain how to change a flat tire.')
    print('We\'ll assume you\'ve already taken the spare and jack out of the trunk, or wherever it\' being held.')
    print()

This process is repeated through Step 9.

At the very bottom of the page, we call the “main()” function.

# Call the main function to begin the program.
main()

This is what tells Python to go to the top and process the program since we’ve already coded all of the necessary information the program needs to run successfully. Notice that this isn’t indented. Since it’s not indented, we’re telling Python that it needs to run this block of code independently of the function definitions.

As Python goes to the top and reads each line of code, and the user progresses through the program, each step is printed out to the user describing how to change a tire. This is a very basic example of how functions process information, and as I progress through learning Python, I’m sure I will find better ways to write such a repetitive program in a better format.