What if you could write instructions once and use them over and over without repeating yourself?
Why Functions (reusable code blocks) in Intro to Computing? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you have to bake 10 cakes, and for each cake, you write down every single step again and again on paper.
It feels tiring and takes a lot of time.
Writing the same instructions repeatedly wastes time and can cause mistakes.
If you want to change one step, you must find and fix it in every copy.
This is slow and confusing.
Functions let you write the instructions once and reuse them whenever needed.
If you want to change a step, you update it in one place, and it applies everywhere.
This saves time and reduces errors.
print('Step 1: Mix ingredients') print('Step 2: Bake for 30 minutes') print('Step 1: Mix ingredients') print('Step 2: Bake for 30 minutes')
def bake_cake(): print('Step 1: Mix ingredients') print('Step 2: Bake for 30 minutes') bake_cake() bake_cake()
Functions make your code cleaner, faster to write, and easier to fix or improve.
Think of a coffee machine: you press a button once, and it follows the same steps every time to make coffee without rewriting instructions.
Functions let you reuse code easily.
They reduce mistakes by centralizing instructions.
They save time and make programs easier to manage.
Practice
Solution
Step 1: Understand what a function does
A function is a reusable block of code designed to perform a specific task.Step 2: Identify the main purpose
Functions help avoid repeating the same code by allowing reuse whenever needed.Final Answer:
To reuse a block of code multiple times -> Option BQuick Check:
Function purpose = reuse code [OK]
- Confusing functions with variables
- Thinking functions store data permanently
- Believing functions only display output
greet that takes no inputs?Solution
Step 1: Recognize Python function syntax
In Python, functions are defined using the keyworddef, followed by the function name and parentheses with parameters (empty if none), ending with a colon.Step 2: Check each option
def greet():matches Python syntax.function greet() {}andfunction greet:use JavaScript style or incorrect syntax.def greet()misses the colon.Final Answer:
def greet(): -> Option DQuick Check:
Python function = def name(): [OK]
- Omitting the colon at the end
- Using JavaScript syntax in Python
- Leaving out parentheses
def add(x, y):
return x + y
result = add(3, 4)
print(result)Solution
Step 1: Understand the function call
The functionaddtakes two inputsxandyand returns their sum.Step 2: Calculate the return value
Callingadd(3, 4)returns 3 + 4 = 7, which is stored inresult. Printingresultoutputs 7.Final Answer:
7 -> Option CQuick Check:
3 + 4 = 7 [OK]
- Thinking it concatenates numbers as strings
- Expecting a TypeError due to missing return
- Assuming print shows None
def multiply(a, b)
return a * bSolution
Step 1: Check function header syntax
In Python, the function header must end with a colon (:). Here, it is missing afterdef multiply(a, b).Step 2: Verify other parts
The return statement and function name are correct. Parameters use parentheses, not square brackets.Final Answer:
Missing colon after function header -> Option AQuick Check:
Function header ends with : [OK]
- Forgetting the colon at the end
- Using square brackets for parameters
- Misnaming the function
Solution
Step 1: Understand the condition
The function should return the square ofnonly ifnis positive (greater than 0). Otherwise, it returns 0.Step 2: Check each option
def square_if_positive(n): if n > 0: return n * n else: return 0 correctly checksn > 0and returnsn * n, else 0. def square_if_positive(n): if n >= 0: return n ** 2 else: return n includes zero as positive and returnsnif negative, which is incorrect. def square_if_positive(n): return n * n if n > 0 else None returns None instead of 0 whennis not positive. def square_if_positive(n): if n < 0: return n * n else: return 0 squares negative numbers and returns 0 otherwise, which is opposite.Final Answer:
def square_if_positive(n): if n > 0: return n * n else: return 0 -> Option AQuick Check:
Positive n squared, else zero [OK]
- Including zero as positive
- Returning None instead of zero
- Reversing the condition logic
