Computer Science Grade 9 20 min

Creating Subtasks

Creating Subtasks

Tutorial Preview

1

Introduction & Learning Objectives

Learning Objectives Define problem decomposition and explain its importance in programming. Identify repeatable or distinct steps in a large problem that can be turned into subtasks. Write a 'helper function' that performs a specific, single subtask. Pass data to a subtask using parameters and receive results using a return value. Refactor a single block of code into a main function that calls multiple helper functions. Trace the flow of execution between a main program and its subtasks. Ever tried to build a huge LEGO castle by looking at the final picture instead of the step-by-step instructions? 🏰 It's confusing! Programming is the same. In this lesson, you'll learn how to break down big, complex programming challenges into smaller, manageable pieces...
2

Key Concepts & Vocabulary

TermDefinitionExample DecompositionThe process of breaking down a large, complex problem into smaller, more manageable, and self-contained subproblems or subtasks.Problem: 'Bake a cake'. Decomposition: Subtasks are '1. Gather ingredients', '2. Mix dry ingredients', '3. Mix wet ingredients', '4. Combine and bake', '5. Make frosting'. Subtask (Function/Method)A named block of code that performs a specific task. It can be 'called' or executed from other parts of the program whenever that task needs to be done.A function named `calculate_area(width, height)` that contains the code to multiply width by height and give back the answer. Helper FunctionA type of function created to handle a small, reusable part of a larger task...
3

Core Syntax & Patterns

Function Definition Syntax function function_name(parameter1, parameter2, ...): # code to perform the subtask return result Use this pattern to create a new subtask. Start with the `function` keyword, give it a descriptive name, list the parameters it needs inside parentheses, and use the `return` keyword to send a value back. Function Call Pattern variable_to_store_result = function_name(argument1, argument2, ...) Use this pattern to execute a subtask you have defined. You provide the actual values (arguments) that the function's parameters will hold. The function's return value is then stored in a variable.

4 more steps in this tutorial

Sign up free to access the complete tutorial with worked examples and practice.

Sign Up Free to Continue

Sample Practice Questions

Challenging
What is the final output of this code? `function square(n): return n * n function add_one(n): return n + 1 result = square(add_one(3)) print(result)`
A.10
B.16
C.12
D.7
Challenging
You are debugging the 'Total Cost' program. The subtotal for items is $50, and the expected final cost with 7% tax is $53.50. However, the program outputs $50. Which function is most likely the source of the bug? `function calculate_subtotal(items): # ... correctly sums items ... return sum function apply_tax(cost): total = cost * 1.07 # Bug is likely here # Main Code subtotal = calculate_subtotal([20, 30]) final_cost = apply_tax(subtotal) print(final_cost)`
A.The `apply_tax` function is missing a `return total` statement.
B.The `calculate_subtotal` function is returning the wrong value.
C.The main code is passing the wrong variable to `apply_tax`.
D.The tax rate is incorrect; it should be `cost * 0.07`.
Challenging
A function is defined as `function process_data(data_list, setting)` which does three things: it sorts the `data_list`, then removes any duplicate values, and finally, if `setting` is true, it converts all items to uppercase. What is the best way to refactor this function according to the principle of decomposition?
A.Keep it as one function because all the steps are related to processing data.
B.Create two functions: `sort_and_remove_duplicates(list)` and `convert_to_uppercase(list)`.
C.Create one function `sort(list)` and another `remove_duplicates_and_uppercase(list, setting)`.
D.Create three separate helper functions: `sort_list(list)`, `remove_duplicates(list)`, and `uppercase_list(list)`.

Want to practice and check your answers?

Sign up to access all questions with instant feedback, explanations, and progress tracking.

Start Practicing Free

More from Advanced Topics

Ready to find your learning gaps?

Take a free diagnostic test and get a personalized learning plan in minutes.