Python 7a – Procedures

Reusable blocks of code that perform actions but don’t return values—great for structuring your programs.

A subroutine is a reusable section of code you can call whenever you need it. Subroutines help you keep programs tidy, reduce repetition, and break complex problems into smaller parts.

Procedures vs Functions

  • Procedures: perform actions and do not return a value. You simply call them to do something.
  • Functions: take input (parameters) and return a value back to the caller.

How to Define & Call a Procedure

Use the def keyword to define a subroutine near the top of your program. Then call it from your main code when needed.

Example: Multiply and Divide Procedures

Two procedures—one to multiply, one to divide—plus a loop that lets the user choose an operation.

# Define subroutines at the top

def multiply(a, b):
    result = a * b
    print("The product is", result)

def divide(a, b):
    if b != 0:
        result = a / b
        print("The quotient is", result)
    else:
        print("Cannot divide by zero!")

# Main program
while True:
    choice = input("Type M to multiply, D to divide, or S to stop: ").upper()
    if choice == "S":
        break
    num1 = float(input("Enter the first number: "))
    num2 = float(input("Enter the second number: "))
    if choice == "M":
        multiply(num1, num2)
    elif choice == "D":
        divide(num1, num2)
    else:
        print("Invalid option!")

Practice Tasks

Task 1: Hello Procedure

Create a procedure called hello that prints "Hello there!". Then call it 10 times using a for loop.

def hello():
    print("Hello there!")

for i in range(10):
    hello()

Task 2: Addition & Subtraction with Global Variables

Make two procedures, one to add and one to subtract. Use global variables to hold the inputs. Keep asking the user which operation to perform until they type STOP.

# Global variables for numbers
num1 = 0
num2 = 0

def add_numbers():
    global num1, num2
    print("The sum is", num1 + num2)

def subtract_numbers():
    global num1, num2
    print("The difference is", num1 - num2)

while True:
    operation = input("Type ADD to add, SUBTRACT to subtract, or STOP to exit: ").upper()
    if operation == "STOP":
        break
    num1 = float(input("Enter the first number: "))
    num2 = float(input("Enter the second number: "))
    if operation == "ADD":
        add_numbers()
    elif operation == "SUBTRACT":
        subtract_numbers()
    else:
        print("Invalid operation!")

Local & Global Variables

Global variables can be accessed anywhere in the file. Local variables only exist inside the subroutine where they’re created. The example above uses the global keyword so procedures can modify numbers defined outside them.

Extra Task: Greet the User

Ask for the user’s name in the main program, store it in a global variable, then call a greet_user procedure that prints a welcome message using that name.

# Global variable for the user's name
user_name = ""

def greet_user():
    global user_name
    print("Hello,", user_name + "! Welcome to our program.")

# Main program
user_name = input("Enter your name: ")
greet_user()