Skip to content
Computer Science · Grade 9 · The Art of Programming · Term 1

Functions and Modularity

Students will define and call functions to organize code into reusable, modular blocks.

Ontario Curriculum ExpectationsCS.HS.AP.8CS.HS.CT.9

About This Topic

Functions and modularity teach students to define and call reusable blocks of code, transforming linear scripts into organized programs. In Grade 9 Computer Science, students break complex tasks, such as data processing or shape drawing, into functions with parameters for flexibility. They explain how this boosts readability and maintainability, directly addressing key questions on design and evaluation.

This topic anchors The Art of Programming unit in Ontario's curriculum, aligning with CS.HS.AP.8 for abstraction procedures and CS.HS.CT.9 for modular artifacts. Students design programs like multi-step calculators, passing arguments to functions, which builds computational thinking and prepares for collaborative projects.

Active learning excels with this content. When students pair program to decompose tasks, refactor code collaboratively, or debug shared modules, they grasp modularity's practical value through iteration and peer feedback. This hands-on approach makes abstract benefits concrete, reduces frustration with tangled code, and builds lasting programming skills.

Key Questions

  1. Explain how functions improve code readability and maintainability.
  2. Design a program that breaks down a complex task into multiple functions.
  3. Evaluate the benefits of passing parameters to functions for increased flexibility.

Learning Objectives

  • Design a program that decomposes a complex problem into at least three distinct functions.
  • Analyze the impact of passing different arguments on function output and program behavior.
  • Evaluate the readability and maintainability of code before and after refactoring into functions.
  • Create a set of interconnected functions to simulate a simple real-world process, such as a basic transaction system.

Before You Start

Basic Programming Constructs

Why: Students need a foundational understanding of variables, data types, and sequential execution before they can effectively define and call functions.

Control Flow (If/Else, Loops)

Why: Understanding how to control the order of execution is necessary for comprehending how functions alter program flow and for writing the logic within functions.

Key Vocabulary

Function DefinitionThe block of code that specifies what a function does, including its name, parameters, and the statements it executes.
Function CallAn instruction to execute the code within a defined function. This involves providing any required arguments.
ParameterA variable listed inside the parentheses in the function definition, acting as a placeholder for values that will be passed into the function.
ArgumentA value passed to a function when it is called. This value is assigned to the corresponding parameter within the function.
ModularityThe principle of breaking down a large program into smaller, independent, and interchangeable parts or modules (functions).

Watch Out for These Misconceptions

Common MisconceptionFunctions execute immediately when defined.

What to Teach Instead

Functions are defined but run only on call. Pairs trace code execution step-by-step with print statements, revealing the difference and building call stack awareness.

Common MisconceptionGlobal variables work better than parameters.

What to Teach Instead

Parameters enable reuse without side effects across contexts. Group comparisons of global vs parameterized versions highlight flexibility during refactoring activities.

Common MisconceptionEvery function must return a value.

What to Teach Instead

Procedures perform actions like printing without returns. Students experiment with both in labs, calling them in loops to see outputs clarify the distinction.

Active Learning Ideas

See all activities

Real-World Connections

  • Software developers at companies like Google use functions extensively to build complex applications such as the Google Maps navigation system. Each function might handle a specific task, like calculating a route, displaying traffic data, or rendering the map, making the overall system manageable and updatable.
  • Video game designers employ functions to manage character actions, game physics, and user interface elements. For example, a 'jump' function might be called whenever the player presses the spacebar, ensuring consistent behavior across different game scenarios.

Assessment Ideas

Quick Check

Present students with a short, linear script (e.g., calculating area and perimeter of multiple rectangles). Ask them to identify repetitive code blocks and suggest how they could be turned into functions. Then, have them write the definition for one proposed function.

Exit Ticket

Provide students with a simple program that uses functions. Ask them to trace the execution flow, writing down the value of a specific variable after each function call. Include one question: 'What would happen if you removed the argument from this function call?'

Discussion Prompt

Pose the question: 'Imagine you are working on a team project and need to add a new feature to an existing program. How does using functions and modularity make this task easier or harder compared to a single, long script? Discuss specific examples.'

Frequently Asked Questions

How do functions improve program maintainability?
Functions isolate code blocks, so changes affect only that part without rippling elsewhere. Students editing one function in a multi-part program see fewer bugs overall. This modularity scales to larger projects, teaching habits like single-responsibility that prevent spaghetti code in future work. Peer reviews reinforce these gains through shared examples.
What role do parameters play in function flexibility?
Parameters let functions handle varied inputs without rewriting, like a drawShape function using size and color args. Students test one function with multiple calls, observing outputs change predictably. This practice shows reusability, reduces duplication, and mirrors real-world APIs, deepening appreciation for abstraction.
How can active learning help students grasp modularity?
Pair programming and group refactoring let students actively decompose tasks, code functions, and integrate them, experiencing readability gains firsthand. Debugging shared modules reveals tangled code's pitfalls, while gallery walks of peers' functions spark critique and iteration. These collaborative methods build confidence faster than lectures, as students own the design process and see modularity's impact immediately.
How to assess understanding of functions and modularity?
Use code walkthroughs where students explain their function designs, justify parameters, and refactor samples. Rubrics score modularity, readability, and test cases. Portfolios of before-after code plus reflections show growth. Live demos with varied inputs confirm flexibility, providing clear evidence aligned to curriculum standards.