Function Parameters and Return Values
Students will deepen their understanding of functions by working with parameters to pass data and return values to send results back.
About This Topic
Functions gain power through parameters, which pass specific data into them, and return values, which send computed results back to the caller. Grade 9 students in Ontario's Computer Science curriculum construct functions that accept multiple parameters, perform calculations, and return outputs, such as a function that takes base and height to return triangle area. This builds versatility, allowing the same function to work with different inputs without rewriting code. Students analyze how parameters enable reuse and differentiate action-oriented procedures from value-returning functions.
This topic fits within The Art of Programming unit, advancing from basic functions to modular code design. It supports standards like CS.HS.AP.8 on procedures with parameters and CS.HS.CT.9 on computational thinking through decomposition. Students practice breaking problems into functions, testing them independently before integration, which mirrors professional programming practices and reduces bugs in larger scripts.
Active learning excels for this content because students code, run, and debug functions collaboratively. Pair programming lets them trace parameter flow and return paths step-by-step, spotting errors instantly. Iterative challenges, like chaining functions, make abstract syntax tangible, boost confidence, and solidify skills through immediate feedback.
Key Questions
- Analyze how parameters enable functions to be more versatile and reusable.
- Construct a function that takes multiple parameters and returns a computed value.
- Differentiate between functions that perform an action and those that return a value.
Learning Objectives
- Analyze how function parameters increase code reusability by allowing functions to operate on different input values.
- Construct Python functions that accept multiple parameters and return a single computed value based on those parameters.
- Differentiate between functions designed to perform an action (e.g., printing output) and functions designed to return a computed value for further use.
- Trace the flow of data into a function via parameters and out of a function via a return statement in a given code snippet.
- Evaluate the effectiveness of a function based on its parameter design and the clarity of its return value.
Before You Start
Why: Students need a basic understanding of what functions are and how to define and call them before working with parameters and return values.
Why: Understanding different data types is essential for knowing what kind of data can be passed as arguments and what type of value a function can return.
Key Vocabulary
| Parameter | A variable listed inside the parentheses in a function definition. It acts as a placeholder for data that will be passed into the function when it is called. |
| Argument | The actual value that is sent to the function when it is called. This value is assigned to the corresponding parameter within the function. |
| Return Value | The value that a function sends back to the part of the program that called it. This is specified using the 'return' keyword. |
| Function Call | The act of executing a function. When a function is called, the program jumps to the function's code, passes any arguments, and then returns to the point after the call once the function is complete. |
Watch Out for These Misconceptions
Common MisconceptionParameters are global variables that change everywhere.
What to Teach Instead
Parameters create local scopes; changes inside functions do not affect caller variables. Pair tracing activities with print statements reveal scope boundaries clearly, as students step through code line-by-line and observe isolated effects.
Common MisconceptionAll functions must return a value; printing is the same.
What to Teach Instead
Procedures perform actions like printing without returning, while functions compute and return for reuse. Testing both types side-by-side in group challenges shows how unassigned returns discard results, helping students value returns through practical comparisons.
Common MisconceptionReturn statements can only appear at the end of a function.
What to Teach Instead
Returns exit early when conditions met, useful for validation. Debug sessions in small groups expose this by simulating paths, where students adjust code and rerun to see early exits work correctly.
Active Learning Ideas
See all activitiesPair Programming: Math Function Builder
Pairs define three functions: one for addition (two params), one for area (length, width), and one for volume (length, width, height), each returning a value. They test by calling functions in a main script and print results. Pairs swap scripts to verify outputs match expected values.
Small Groups: Function Chain Relay
Groups create a chain of three functions where the return value of the first feeds as a parameter into the second, and so on, to compute a final result like compound interest. Each member codes one function. Groups run the chain and adjust for accuracy.
Whole Class: Parameter Puzzle Share-Out
Students write a function with three parameters on paper or whiteboard, solving a shared problem like grading averages. Post solutions around the room. Class walks through, identifies parameter uses, and votes on most reusable designs.
Individual: Refactor Challenge
Provide code with repeated calculations. Students identify patterns, rewrite as functions with parameters and returns, then compare original and new run times or outputs. Submit before-and-after code.
Real-World Connections
- Software developers building video games use functions with parameters to control character actions. For example, a 'moveCharacter' function might take parameters for direction (e.g., 'up', 'down') and distance, returning the character's new position on the game map.
- Financial analysts use functions to calculate metrics like loan interest or investment returns. A function to calculate monthly mortgage payments would take parameters such as principal loan amount, interest rate, and loan term, returning the calculated monthly payment.
- Web developers create functions to handle user input and display dynamic content. A function that processes a user's search query might take the query string as a parameter and return a list of relevant search results to be displayed on a webpage.
Assessment Ideas
Provide students with a Python code snippet containing a function definition with parameters and a function call. Ask them to identify the parameters, the arguments passed, and the expected return value. For example: 'In the function `calculate_area(width, height)`, what are the parameters? If called as `calculate_area(5, 10)`, what are the arguments? What value will this function return?'
Give students a scenario: 'You need a function to convert Celsius to Fahrenheit.' Ask them to write the function signature (including parameters) and one line of code showing how they would call it with a specific temperature, and what the expected return value would be. For example: 'Write a function `celsius_to_fahrenheit(celsius_temp)`. Show how to call it with 25 degrees Celsius and state the expected return value.'
Pose the question: 'When might you choose to write a function that simply prints a message versus a function that returns a calculated value? Provide a specific programming example for each case.' Facilitate a brief class discussion where students share their examples and reasoning.
Frequently Asked Questions
How do function parameters improve code reusability?
What is the difference between action functions and those that return values?
How can active learning help students understand function parameters and returns?
What are common errors with return statements in functions?
More in The Art of Programming
Conditional Statements (If/Else)
Students will implement conditional statements to allow programs to make decisions based on specific criteria.
2 methodologies
Advanced Conditional Logic (Else If, Switch)
Students will expand their use of conditional statements to include 'else if' and 'switch' structures for multi-way decisions.
2 methodologies
Iteration with Loops (For/While)
Students will use 'for' and 'while' loops to repeat blocks of code efficiently.
2 methodologies
Nested Loops and Iteration Patterns
Students will explore how to use nested loops to solve problems requiring iteration over multiple dimensions or complex patterns.
2 methodologies
Functions and Modularity
Students will define and call functions to organize code into reusable, modular blocks.
2 methodologies
Introduction to Lists and Arrays
Students will learn to store and access collections of data using lists or arrays.
2 methodologies