Skip to content
Computer Science · 9th Grade · Programming with Purpose · Weeks 19-27

Introduction to Functions

Students will design reusable code blocks to improve readability and maintainability.

Common Core State StandardsCSTA: 3A-AP-17CSTA: 3A-AP-18

About This Topic

Functions are the building blocks of organized, maintainable code. When students define a named block of code that performs a specific task, they gain the ability to use that block as many times as needed without rewriting it. In US K-12 computer science, this aligns with CSTA 3A-AP-17 and 3A-AP-18, which ask students to create artifacts that are reusable and modular. For 9th graders, functions mark the transition from writing scripts to thinking like software engineers who design systems.

Beyond eliminating repetition, functions give code a readable structure. A program built from well-named functions reads almost like an outline: first validate the input, then calculate the result, then format the output. Each function hides its implementation details, letting a programmer work on one piece without mentally holding the entire program at once. This is a concrete introduction to the abstraction principle.

Active learning benefits this topic because function design is a social process in professional practice. Code review, collaborative design, and peer explanation help students articulate why one function structure is clearer than another, and that articulation builds the design intuition they will use throughout their programming work.

Key Questions

  1. Explain the benefits of using functions to organize code.
  2. Construct a simple function that performs a specific task.
  3. Differentiate between parameters and arguments in function calls.

Learning Objectives

  • Explain the benefits of using functions to organize code for improved readability and maintainability.
  • Construct a simple function in Python that performs a specific, well-defined task.
  • Differentiate between parameters and arguments when defining and calling Python functions.
  • Analyze a given code snippet and refactor it by introducing functions to reduce redundancy.
  • Evaluate the clarity and efficiency of different function designs for a given problem.

Before You Start

Basic Python Syntax

Why: Students need to be familiar with variable assignment, basic data types, and print statements to understand how functions operate and interact with data.

Sequential Execution

Why: Understanding that code runs line by line is essential before students can grasp how functions alter the flow of execution by being called and returning control.

Key Vocabulary

FunctionA named block of code designed to perform a specific task. Functions allow code to be reused without repetition.
ParameterA variable listed inside the parentheses in a function definition. It acts as a placeholder for a value 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.
CallThe act of executing a function. When a function is called, its code block runs, and it can receive arguments.
Return ValueThe value that a function sends back to the part of the program that called it. Not all functions return a value.

Watch Out for These Misconceptions

Common MisconceptionA function only makes sense if the code will be used more than once.

What to Teach Instead

Functions also improve readability and testability even when code runs only once. A function named validate_user_age() tells a reader exactly what that block does without requiring them to read the implementation. Peer code review activities where students evaluate readability without running the code build appreciation for this benefit.

Common MisconceptionThe function definition runs the code.

What to Teach Instead

Defining a function stores the code but does not run it. The code runs only when the function is called. This confusion often shows up in debugging sessions; collaborative code tracing exercises, where students annotate when each line executes, address it directly.

Common MisconceptionFunctions and methods are the same thing.

What to Teach Instead

A method is a function that belongs to a class or object in object-oriented programming. All methods are functions, but not all functions are methods. This distinction becomes important when students encounter object-oriented code, so establishing it early prevents confusion later.

Active Learning Ideas

See all activities

Real-World Connections

  • Software developers at Google use functions extensively to build complex applications like Google Maps. For example, a function might be responsible for calculating the shortest route between two points, a task that can be reused for any user's navigation needs.
  • Game designers employ functions to manage character actions and game logic. A function named 'jump()' could be called whenever a player presses the jump button, ensuring consistent behavior across multiple instances of gameplay.
  • Web developers use functions to handle user interactions on websites. A function could process a form submission, validate user input, or update content dynamically, making the website interactive and efficient.

Assessment Ideas

Quick Check

Present students with a short Python script that repeats a block of code multiple times. Ask them to identify the repeated block and then write a new function that encapsulates this block, demonstrating how to call it in place of the original repetitions.

Exit Ticket

Provide students with a function definition that includes parameters. Ask them to write a line of code that calls this function, passing in appropriate arguments, and then write one sentence explaining the difference between the parameter and the argument they used.

Peer Assessment

Students write a simple function to solve a small problem (e.g., calculate the area of a rectangle). They then exchange their functions with a partner. Each partner reviews the code for clarity, correctness, and proper use of parameters/arguments, providing one specific suggestion for improvement.

Frequently Asked Questions

What is the difference between a parameter and an argument?
A parameter is the variable name listed in the function definition; it is a placeholder for the value the function will receive. An argument is the actual value passed to the function when it is called. If a function is defined with parameter 'name' and you call it with 'Alice', then 'name' is the parameter and 'Alice' is the argument.
Does every function need a return statement?
No. A function that performs an action, such as printing output, modifying a list, or writing a file, may not need to return a value. A function that calculates a result typically should return it so the caller can use it. Whether to return a value depends on the function's purpose and how the result will be used.
How does active learning help students understand how functions work?
Functions involve two distinct moments: definition and calling. Students who only read about functions often merge these mentally. When they practice explaining a function to a partner in plain English, then watch the partner try to code from that description, the gap between what they think they communicated and what was understood reveals the precision functions require.
How many functions should a program have?
There is no fixed number, but a common professional guideline is that each function should do one thing well. If describing what a function does requires the word 'and', it may be doing too much. Programs that follow this single-responsibility principle are usually easier to test, debug, and update when requirements change.