Computer Science
Grade 7
20 min
Building a Big Tower: Teamwork
Building a tower together, emphasizing communication and shared responsibility.
Tutorial Preview
1
Introduction & Learning Objectives
Learning Objectives
Decompose a complex problem ('build a tower') into smaller, single-purpose functions ('build a floor', 'add a roof').
Define functions that accept parameters to customize their behavior (e.g., floor width, character symbol).
Use return values to pass information from a helper function back to the main program.
Design a program where a main 'coordinator' function calls other helper functions in a specific order to achieve a goal.
Explain how breaking a program into functions makes it easier to read, test, and reuse code.
Trace the flow of data as it is passed into functions via parameters and returned as results.
Have you ever built a huge LEGO castle? 🏰 You don't build it all at once; you build small sections...
2
Key Concepts & Vocabulary
TermDefinitionExample
Modular DesignThe practice of breaking a large, complex program into smaller, self-contained, and manageable parts called modules or functions.Instead of one giant 'buildTower' program, we create smaller functions like 'buildFloor(width)', 'buildRoof()', and 'buildBase()'. The main program then calls these functions in order.
Function ParameterA piece of information (like a variable) that you send INTO a function to tell it how to do its job.In a function `drawBox(width, height)`, `width` and `height` are parameters. You could call it with `drawBox(10, 5)` to get a specific size.
Return ValueThe single piece of information that a function sends BACK to the part of the program that called it after it has finished its job.A funct...
3
Core Syntax & Patterns
The Function Call & Return Pattern
variable = function_name(argument1, argument2)
Use this pattern when you need a function to do a calculation or create something and give you the result back. The value returned by the function is stored in the variable on the left side of the '='.
The Modular Program Structure
def helper_function1(params):
# ... does one specific task
return result
def helper_function2(params):
# ... does another specific task
return result
def main():
# Coordinator logic
result1 = helper_function1(data)
final_result = helper_function2(result1)
print(final_result)
main()
This is the blueprint for teamwork. Define small, focused helper functions first. Then, create a `main` function that calls the helpers in the right order...
4 more steps in this tutorial
Sign up free to access the complete tutorial with worked examples and practice.
Sign Up Free to ContinueSample Practice Questions
Challenging
You need to write a new coordinator function `build_uniform_tower(height, width, symbol)`. Which implementation correctly uses a loop and the `create_floor` helper to build a tower of a given height and uniform width?
A.def build_uniform_tower(height, width, symbol):
floor_design = create_floor(width, symbol)
for i in range(height):
print(floor_design)
B.def build_uniform_tower(height, width, symbol):
for i in range(height):
floor_design = create_floor(width, symbol)
print(floor_design)
C.def build_uniform_tower(height, width, symbol):
for i in range(width):
print(create_floor(height, symbol))
D.def build_uniform_tower(height, width, symbol):
print(create_floor(width, symbol))
Challenging
Your `main` function needs to build a pyramid. The top floor has width 1, the next has width 3, the next 5, and so on. Given the `height` of the pyramid, which code inside a `for i in range(height):` loop correctly calculates the `width` for each floor?
A.width = height * 2 + 1
B.width = i + 1
C.width = 1
D.width = i * 2 + 1
Challenging
A teammate argues, "Why use a `create_floor` function? I can just write `print('|' + '#' * 7 + '|')`. It's shorter!" Based on the tutorial's concepts, what is the strongest counter-argument?
A.Your way is harder to read and understand.
B.Using functions makes the computer run the program much faster.
C.function is better because if the design of ALL floors needs to change (e.g., using '!' instead of '|'), we only have to edit one place: the function's definition.
D.You should never use the `+` symbol to build strings.
Want to practice and check your answers?
Sign up to access all questions with instant feedback, explanations, and progress tracking.
Start Practicing Free