⚙️ Chapter 6: Functions - Your Daily Problem Solver
Test your understanding of Python functions and automation!
🛠️ Function Basics
1. What is a function in Python?
A type of variable that stores numbers
A custom command that performs specific tasks
A way to create loops
A method to format text
Correct! A function is like creating your own custom command or personal assistant that performs specific tasks whenever you call it.
2. Which keyword is used to create a function in Python?
function
def
create
make
Correct! The 'def' keyword is used to define (create) a function in Python. It tells Python "I'm about to create a custom command."
3. What are the main benefits of using functions?
They make code run faster
They stop repetition, make code readable, and easy to update
They automatically fix bugs
They convert Python to other languages
Correct! Functions help you avoid repeating code, make your programs more readable, and make updates easier since you only need to change code in one place.
📝 Parameters and Arguments
4. What are parameters in a function?
The values you pass to the function when calling it
The placeholders in the function definition
The results the function returns
The code inside the function
Correct! Parameters are the placeholders in the function definition - like blanks in a Mad Libs game that get filled when you call the function.
5. In this function call: greet_client("Sarah", "morning") - what are "Sarah" and "morning"?
Parameters
Arguments
Return values
Variables
Correct! "Sarah" and "morning" are arguments - the actual values you pass to the function when you call it.
Correct! The function calculates 50% of 1000: 1000 * (50/100) = 1000 * 0.5 = 500.
🔄 Return vs Print
9. What's the difference between print() and return in functions?
print() is faster than return
return is faster than print()
print() shows info on screen, return gives info back to use elsewhere
They do exactly the same thing
Correct! print() displays information on screen (like shouting into the void), while return gives information back to the program to use elsewhere.
10. If a function doesn't have a return statement, what does it return?
0
Empty string ""
None
False
Correct! Functions that don't explicitly return a value automatically return None, which can cause problems if you try to use the result in calculations.
11. What error would you get if you try to add None to a number?
SyntaxError
TypeError (can't add NoneType)
ValueError
NameError
Correct! You get a TypeError about "can't add NoneType" because None isn't a number and can't be used in mathematical operations.
📊 Multiple Returns
12. What can you return from a function?
Only numbers
Only text
Only one value
Multiple values separated by commas
Correct! Functions can return multiple values separated by commas, like a combo meal for data - you get everything you need in one function call.
subtotal, tax, discount, total = calculate_invoice(1000, 0.08, 0.1)
Both a and b work
You can't capture multiple values
Correct! You can capture multiple return values in separate variables using tuple unpacking, or store them all in one variable as a tuple.
✏️ Fill in the Blanks
14. Complete this function definition:
calculate_discount(price, discount_percent):
discount_amount = price * (discount_percent / 100)
final_price = price - discount_amount
discount_amount, final_price
# Using the function:
discount, final = (200, 15)
Fill in: The keyword to define a function, the keyword to give back values, and the function name to call it.
Correct answers: "def", "return", "calculate_discount" - This creates a function that calculates and returns both the discount amount and final price.
✅ True or False
15. Functions must always have parameters.
True
False
Correct! False! Functions can have zero parameters if they don't need any input to do their job, like a simple greeting function.
16. You can save functions in separate files and import them.
True
False
Correct! True! You can organize functions in separate files and import them when needed, like having a toolbox where each drawer contains specific tools.
17. Functions can only return one type of data (like only numbers or only text).
True
False
Correct! False! Functions can return different types of data - numbers, text, lists, or any combination you need.
18. The :.2f format ensures money values show exactly two decimal places.
True
False
Correct! True! The :.2f format ensures money always shows exactly two decimal places, preventing confusing amounts like $2500.333333.