Collections, Abstraction, and Modularity with Functions
Learning Objectives: By the end of this lesson, you will be able to:
- Represent and manipulate collections (lists/arrays) in pseudocode
- Use functions to achieve abstraction and modularity
- Decompose problems into reusable, logical pieces using functions
1. Welcome and Introduction (5 minutes)
Icebreaker Activity
Have you ever tried to organize a group of items (like a shopping list or playlist)? How do you keep track of each item, and what if you wanted to perform the same action on each one?
Lesson Overview
- Duration: 90 minutes
- Prerequisites: Day 1–4 lessons (algorithms, pseudocode basics, loops, selection)
- Materials Needed: Pen and paper or a text editor for writing pseudocode
2. Learning Objectives and Session Outline (5 minutes)
What We’ll Cover Today:
- Lists/arrays in pseudocode
- Abstraction and modularity with functions
- Decomposing problems into smaller, reusable parts
- Hands-on practice and exercises
- Common pitfalls and troubleshooting
Success Criteria:
By the end of this session, you should be able to write pseudocode that uses lists/arrays and functions to solve problems in a modular way.
3. Foundation Knowledge (15-20 minutes)
Core Concepts
Lists/Arrays in Pseudocode
A list (or array) is an ordered collection of items. You can access each item by its position (index).
Example:
studentScores = [85, 90, 78, 92]studentScores[0]is 85 (first item)
Why This Matters: Collections let you store and process multiple values efficiently, such as grades, names, or items in a cart.
Abstraction and Modularity with Functions
- Abstraction: Hiding complex details behind a simple interface. Functions let you “name” a process and use it without worrying about how it works inside.
- Modularity: Breaking a program into smaller, self-contained, reusable modules (functions/procedures). This makes code easier to understand, test, and maintain.
Example:
sumList(listOfNumbers)returns the sum of all numbers in a listcountList(listOfNumbers)returns the number of items in a list
Key Terminology
- List/Array: An ordered collection of items
- Function: A named block of code that performs a specific task
- Parameter: Input to a function
- Return Value: The result a function gives back
4. Detailed Explanation and Examples (25-30 minutes)
Understanding Collections and Functions in Pseudocode
Collections and functions help you organize and reuse your code. Instead of repeating the same logic, you can write a function once and use it many times.
Practical Applications
- Calculating averages, totals, or searching in a list
- Breaking down large problems into smaller, testable parts
Common Misconceptions
- Misconception: Functions are only for advanced programmers. Reality: Functions make even simple programs easier to manage.
- Misconception: You must write all code in one big block. Reality: Modular code is easier to read, debug, and reuse.
5. Live Coding Demonstration (20-25 minutes)
Example 1: Calculating Average Grade (Decomposition)
START
GET studentScores (as a list)
totalSum = CALL sumList(studentScores)
numberOfScores = CALL countList(studentScores)
IF numberOfScores > 0 THEN
average = totalSum / numberOfScores
DISPLAY average
ELSE
DISPLAY "No scores to average."
ENDIF
END
FUNCTION sumList(listOfNumbers)
SET sum TO 0
FOR i FROM 0 TO LENGTH(listOfNumbers) - 1 DO
sum = sum + listOfNumbers[i]
ENDFOR
RETURN sum
ENDFUNCTION
FUNCTION countList(listOfNumbers)
RETURN LENGTH(listOfNumbers)
ENDFUNCTION
Explanation:
- The main program is short and clear
sumListandcountListare reusable for any list- Each function has a single responsibility
Example 2: Finding the Maximum Value in a List
FUNCTION maxInList(listOfNumbers)
IF LENGTH(listOfNumbers) == 0 THEN
RETURN "List is empty"
ENDIF
SET max TO listOfNumbers[0]
FOR i FROM 1 TO LENGTH(listOfNumbers) - 1 DO
IF listOfNumbers[i] > max THEN
SET max TO listOfNumbers[i]
ENDIF
ENDFOR
RETURN max
ENDFUNCTION
Key Points:
- Handles empty lists
- Uses a loop to check each item
Example 3: Modularizing with Functions
FUNCTION printList(list)
FOR i FROM 0 TO LENGTH(list) - 1 DO
OUTPUT list[i]
ENDFOR
ENDFUNCTION
Debugging Demonstration
Common mistake: Forgetting to return a value from a function.
// Incorrect
FUNCTION sumList(listOfNumbers)
SET sum TO 0
FOR i FROM 0 TO LENGTH(listOfNumbers) - 1 DO
sum = sum + listOfNumbers[i]
ENDFOR
// Missing RETURN
ENDFUNCTION
// Corrected version
FUNCTION sumList(listOfNumbers)
SET sum TO 0
FOR i FROM 0 TO LENGTH(listOfNumbers) - 1 DO
sum = sum + listOfNumbers[i]
ENDFOR
RETURN sum
ENDFUNCTION
6. Hands-On Practice (30-40 minutes)
Exercise 1: Basic Practice (10 minutes)
Objective: Write a function to find the minimum value in a list.
Instructions:
- Handle empty lists
- Use a loop to check each item
- Return the minimum value
Starter Code:
FUNCTION minInList(listOfNumbers)
// Your steps here
ENDFUNCTION
Solution:
FUNCTION minInList(listOfNumbers)
IF LENGTH(listOfNumbers) == 0 THEN
RETURN "List is empty"
ENDIF
SET min TO listOfNumbers[0]
FOR i FROM 1 TO LENGTH(listOfNumbers) - 1 DO
IF listOfNumbers[i] < min THEN
SET min TO listOfNumbers[i]
ENDIF
ENDFOR
RETURN min
ENDFUNCTION
Exercise 2: Intermediate Challenge (15 minutes)
Objective: Write a function to count how many times a value appears in a list.
Requirements:
- Input: list and value to count
- Output: number of occurrences
Hints:
- Use a loop and a counter variable
- Compare each item to the value
Exercise 3: Creative Application (15 minutes)
Objective: Write a function to reverse a list.
Challenge:
- Input: a list
- Output: a new list with items in reverse order
Extension Ideas:
- Write a function to check if a list is a palindrome (reads the same forwards and backwards)
- Modularize your code by using helper functions
7. Common Pitfalls and Troubleshooting (10 minutes)
Frequent Mistakes
- Problem: Not returning a value from a function Cause: Forgetting the RETURN statement Solution: Always include RETURN in your functions
- Problem: Modifying the original list unintentionally Cause: Not creating a new list for results Solution: Use a new list variable for outputs like reverse
Debugging Tips
- Test your functions with different inputs (including empty lists)
- Print intermediate results to check logic
- Make sure each function does one thing well
8. Best Practices and Professional Tips (5 minutes)
Industry Standards
- Write small, focused functions
- Use descriptive names for functions and variables
- Reuse functions whenever possible
Next Steps in Your Learning Journey
- Practice writing functions for different list operations
- Learn about nested functions and higher-order functions
- Explore how functions and lists are used in real programming languages
9. Summary and Review (5 minutes)
Key Takeaways
- Collections (lists/arrays) let you store and process multiple values
- Functions provide abstraction and modularity
- Decomposing problems makes code easier to manage and reuse
Quick Review Questions
- What is the benefit of using functions in pseudocode?
- How do you access an item in a list?
- Why is modularity important in programming?
For Instructors
Teaching Tips
- Use relatable examples (shopping lists, grades)
- Encourage students to break problems into functions
- Discuss the difference between modifying and returning new lists
Assessment Ideas
- Have students write and test new list functions
- Peer review: students check each other’s function logic
- Short quiz on lists, functions, and modularity
Adaptation Notes
- For advanced students, introduce recursion or higher-order functions
- For online delivery, use collaborative coding tools
- Adjust timing based on student engagement and questions