Lesson 11 • Intermediate

Procedures

20 minutes
Reusability

🔧 What are Procedures?

Procedures are reusable blocks of code that perform actions but don't return a value. They're perfect for tasks like printing reports, updating displays, or performing operations that don't need to give back a result.

Functions vs. Procedures
Functions Procedures
Return a value Don't return a value
Use Return No Return needed
Calculate and give back results Perform actions (print, update, etc.)
Example: CalculateArea() Example: PrintReport()

📐 Procedure Syntax

Procedure ProcedureName(parameters)
    # Procedure code
    # No return statement
Endprocedure

Calling a Procedure

ProcedureName(arguments)

Simple Procedure Example

Example: Basic Procedure
Algorithm ProcedureDemo

# Define the procedure
Procedure SayHello()
    Print "Hello, World!"
    Print "Welcome to iPseudo!"
Endprocedure

# Call the procedure
SayHello()
SayHello()

Endalgorithm
Output
Hello, World! Welcome to iPseudo! Hello, World! Welcome to iPseudo!

📊 Procedures with Parameters

Example: Procedure with Parameters
Algorithm ParameterProcedure

Procedure GreetUser(name)
    Print "Hello,", name, "!"
    Print "Nice to meet you!"
Endprocedure

Procedure GreetWithTime(name, timeOfDay)
    Print "Good", timeOfDay, ",", name, "!"
Endprocedure

# Call the procedures
GreetUser("Alice")
Print ""
GreetUser("Bob")
Print ""
GreetWithTime("Charlie", "morning")

Endalgorithm
Output
Hello, Alice ! Nice to meet you! Hello, Bob ! Nice to meet you! Good morning , Charlie !

When to Use Procedures

Use Procedures When:
  • Printing formatted output
  • Displaying menus
  • Drawing patterns or graphics
  • Updating displays
  • Performing actions that don't need to return a value
  • Organizing repetitive display code

📋 Real-World Examples

Menu Display

Example: Menu Procedures
Algorithm MenuSystem

Procedure ShowMainMenu()
    Print ""
    Print "================================"
    Print "        MAIN MENU"
    Print "================================"
    Print "1. Start Game"
    Print "2. View High Scores"
    Print "3. Settings"
    Print "4. Exit"
    Print "================================"
Endprocedure

Procedure ShowGameStarted()
    Print ""
    Print "🎮 Game Starting..."
    Print "Good luck!"
Endprocedure

Procedure ShowGoodbye()
    Print ""
    Print "Thanks for playing!"
    Print "👋 Goodbye!"
Endprocedure

# Main program
ShowMainMenu()
var choice = Input "Enter choice:"

If choice == 1 Then
    ShowGameStarted()
Else If choice == 4 Then
    ShowGoodbye()
Endif

Endalgorithm

Report Printing

Example: Report Procedures
Algorithm ReportGenerator

Procedure PrintHeader()
    Print "================================"
    Print "      SALES REPORT"
    Print "      October 2025"
    Print "================================"
    Print ""
Endprocedure

Procedure PrintSalesLine(product, quantity, price)
    var total = quantity * price
    Print product, ":"
    Print "  Quantity:", quantity
    Print "  Price: $", price
    Print "  Total: $", total
    Print ""
Endprocedure

Procedure PrintFooter(grandTotal)
    Print "--------------------------------"
    Print "GRAND TOTAL: $", grandTotal
    Print "================================"
Endprocedure

# Generate report
PrintHeader()
PrintSalesLine("Laptop", 5, 999.99)
PrintSalesLine("Mouse", 20, 29.99)
PrintSalesLine("Keyboard", 15, 79.99)

var total = (5 * 999.99) + (20 * 29.99) + (15 * 79.99)
PrintFooter(total)

Endalgorithm

Pattern Drawing

Example: Pattern Procedures
Algorithm PatternDrawer

Procedure DrawLine(length, character)
    For i = 1 To length
        Print character
    Endfor
    Print ""
Endprocedure

Procedure DrawBox(width, height, character)
    For row = 1 To height
        DrawLine(width, character)
    Endfor
Endprocedure

Procedure DrawSeparator()
    Print ""
    DrawLine(40, "-")
    Print ""
Endprocedure

# Draw patterns
Print "Pattern 1:"
DrawBox(5, 3, "* ")

DrawSeparator()

Print "Pattern 2:"
DrawBox(8, 2, "# ")

Endalgorithm

🔄 Combining Functions and Procedures

You can use functions and procedures together in your programs:

Example: Functions + Procedures
Algorithm CalculatorApp

# Function to calculate (returns value)
Function Add(a, b)
    Return a + b
Endfunction

Function Multiply(a, b)
    Return a * b
Endfunction

# Procedure to display (no return)
Procedure DisplayResult(operation, num1, num2, result)
    Print ""
    Print "=== CALCULATION ==="
    Print num1, operation, num2, "=", result
    Print "==================="
Endprocedure

# Main program
var x = 10
var y = 5

# Calculate using functions
var sum = Add(x, y)
var product = Multiply(x, y)

# Display using procedures
DisplayResult("+", x, y, sum)
DisplayResult("*", x, y, product)

Endalgorithm

📊 Procedure Organization

Example: Well-Organized Program
Algorithm StudentGradeSystem

# ===== DISPLAY PROCEDURES =====

Procedure ShowWelcome()
    Print "================================"
    Print "   STUDENT GRADE SYSTEM"
    Print "================================"
    Print ""
Endprocedure

Procedure ShowStudentInfo(name, id, grade)
    Print "Student Name:", name
    Print "Student ID:", id
    Print "Final Grade:", grade
Endprocedure

Procedure ShowPassFail(grade)
    If grade >= 60 Then
        Print "Status: ✅ PASSED"
    Else
        Print "Status: ❌ FAILED"
    Endif
Endprocedure

# ===== CALCULATION FUNCTIONS =====

Function CalculateGrade(test1, test2, test3)
    Return (test1 + test2 + test3) / 3
Endfunction

# ===== MAIN PROGRAM =====

ShowWelcome()

var name = Input "Enter student name:"
var id = Input "Enter student ID:"
var t1 = Input "Test 1 score:"
var t2 = Input "Test 2 score:"
var t3 = Input "Test 3 score:"

var finalGrade = CalculateGrade(t1, t2, t3)

Print ""
Print "=== STUDENT REPORT ==="
ShowStudentInfo(name, id, finalGrade)
ShowPassFail(finalGrade)
Print "======================="

Endalgorithm

Best Practices

Writing Good Procedures
  • Action Names: Use verb names (ShowMenu, PrintReport, DrawBox)
  • Single Purpose: Each procedure should do ONE thing
  • Consistency: Similar procedures should follow similar patterns
  • Group Related: Keep related procedures together
  • Document: Add comments explaining what the procedure does

Key Takeaways

  • ✅ Procedures perform actions but don't return values
  • ✅ Perfect for display, printing, and UI tasks
  • ✅ Define with Procedure, close with Endprocedure
  • ✅ No Return statement needed
  • ✅ Can have parameters like functions
  • ✅ Use functions for calculations, procedures for actions
  • ✅ Combine both for well-organized programs

💪 Practice Exercises

Try These Exercises

Exercise 1: Receipt Printer
Create procedures to print a receipt header, item lines, and footer. Build a shopping receipt system.
Exercise 2: Menu System
Create procedures for different menus (main, settings, help) and build an interactive menu system.
Exercise 3: Calendar Display
Create procedures to display calendar headers, day names, and month grids. Build a calendar viewer.
Exercise 4: Game Stats Display
Create procedures to show player stats, inventory, and game status. Combine with functions for calculations.
Well Done!
You now understand the difference between functions and procedures, and when to use each! In the next lesson, we'll learn about arrays to work with collections of data.