Procedures and functionsRunning a procedure in Python

When writing programs, we should avoid long, repetitive code. Procedures and functions help to keep our programs simple and short.

Part ofComputer ScienceProgramming

Running a procedure in Python

Once a is named and written, it can be at any point in the .

To call a procedure in , simply use its name (include the brackets):

Suppose you wanted to print out player information at the following points in a game:

  • at the end of a level
  • when the player loses a life
  • when the player beats the high score
  • when the game is over

Each time you needed to print out the player information, you could simply call the procedure:

def update_display(): print("Your score: " + str(score)) time.sleep(1) print("High score: " + str(high_score)) time.sleep(1) print("Lives remaining: " + str(lives)) time.sleep(1) # End of level
update_display() # Lose a life
update_display() # New high score
update_display() # Game over
update_display()

Using this procedure greatly reduces the amount of code that has to be included in the program.