Python · Functions
Quizz
The full text of this chapter, to read or revisit. To practise, do the exercises.
▶ Do the exercises
Exercise 1 — Why Use Functions?
What is the main benefit of using functions in your code?
Options
• Reuse code and avoid repetition (DRY principle)
• Make the program run faster
• Use less memory
• Print more information
Hint : Think about the DRY principle: Don't Repeat Yourself!
Exercise 2 — Complete the Function Structure
Complete this function that takes a Codémon name as parameter and returns a greeting:
____ greet(name)____ message = f"Hello, {name}!" ____ message result = greet(____) print(result)
Options: def function : ; return print "Aquachu" Aquachu
Hint : Remember: ```def``` to define, ```:``` after parameters, ```return``` to send back a value, and quotes for string arguments.
Exercise 3 — Return vs Print - Understanding the Difference
What's the difference between these two functions?
def function_a(): print(100) def function_b(): return 100
Options
• ```function_a``` displays 100 but returns None, ```function_b``` returns 100 without displaying
• They do exactly the same thing
• ```function_a``` returns 100, ```function_b``` displays 100
• Both return and display 100
Hint : ```print()``` displays to screen but doesn't return a value. ```return``` sends a value back but doesn't display.
Exercise 4 — Function Terminology Review
Match each term with its correct meaning:
Items to match
• Parameter
• Argument
• Return value
• Local variable
Definitions
• Variable in function definition
• Value passed when calling function
• Value sent back from function
• Variable created inside function
Exercise 5 — Local Variable Access
What happens when you try to access a local variable outside its function?
def battle(): damage = 50 print(damage) battle() print(damage)
Options
• A ```NameError``` occurs because ```damage``` doesn't exist outside the function
• It prints 50 twice
• It prints 50 then None
• The code works fine
Hint : Local variables only exist inside their function and are destroyed when the function ends.
Exercise 6 — Function with Default Parameter
Complete this function where
element
has a default value of
"water"
:
def create_codemon(name, element____): return f"{name} ({element} type)" # Call with default result1 = create_codemon(____) print(result1) # Call with custom element result2 = create_codemon("Pyrochu", ____) print(result2)
Options: ="water" =water "Aquachu" Aquachu "fire" fire
Hint : Use ```=``` to set default values in function definition. String arguments need quotes.
Exercise 7 — Parameter Order Rules
Which function definition is correct?
Options
• def battle(attacker, defender, damage=10):
• def battle(attacker, damage=10, defender):
• def battle(damage=10, attacker, defender):
• def battle(attacker=None, defender, damage):
Hint : Parameters with default values must come AFTER parameters without defaults.
Exercise 8 — Using Return Values in Calculations
Complete the code to calculate total stats by adding the return values of both functions:
def get_attack(): ____ 75 def get_defense(): ____ 60 total_stats = ____ + ____ print(f"Total: {total_stats}")
Options: return print get_attack() get_defense() attack defense return
Hint : Use ```return``` to send values back, then call the functions with ```()``` to get their return values.
Exercise 9 — What Does This Function Return?
What value does this function return?
def show_level(level): print(f"Level: {level}") result = show_level(25)
Options
• ```None```
• ```25```
• ```"Level: 25"```
• ```level```
Hint : Functions without a ```return``` statement automatically return ```None```.
Exercise 10 — Scope Concepts
Match each scope concept with its example:
Items to match
• Local variable
• Global variable
• global keyword
• Variable shadowing
Definitions
• Created inside function, destroyed after
• Created outside functions, accessible everywhere
• Needed to modify global variable in function
• Local variable hides global with same name
Exercise 11 — Return Value Data Types
What can a function return?
Options
• Any data type (int, string, list, dict, etc.)
• Only numbers (int and float)
• Only strings and numbers
• Only simple types, not lists or dicts
Hint : Functions are very flexible with what they can return!
Exercise 12 — Function with Three Parameters
Complete this function that takes 3 parameters and returns their sum:
def add_three_stats(attack, defense, speed): total = attack ____ defense ____ speed ____ total result = add_three_stats(____) print(result)
Options: + - * + - * return print 50, 40, 30 50 40 30
Hint : Use ```+``` to add values, ```return``` to send back result, and commas to separate multiple arguments.
Exercise 13 — Variable Shadowing
What does this code print?
level = 10 def show_level(): level = 25 print(level) show_level()
Options
• ```25```
• ```10```
• ```NameError```
• ```10``` then ```25```
Hint : The local ```level``` inside the function shadows (hides) the global one.
Exercise 14 — Function Returning Boolean
Complete this function that returns
True
if HP is above 50, otherwise
False
:
def is_healthy(hp): if hp ____ 50: ____ True else: ____ False result = is_healthy(75) print(result)
Options: > < == return print return print
Hint : Use ```>``` to compare, and ```return``` to send back the boolean result.
Exercise 15 — When Are Parameters Needed?
Given this function definition:
def heal_codemon(name, hp): return f"{name} healed for {hp} HP!"
Which function call is correct?
Options
• ```heal_codemon("Aquachu", 30)```
• ```heal_codemon()```
• ```heal_codemon("Aquachu")```
• ```heal_codemon(name="Aquachu")```
Hint : When calling a function, you must provide values for all parameters that don't have default values.
Exercise 16 — Function Behaviors
Match each function characteristic with its behavior:
Items to match
• Function without return
• Function with default parameter
• Function with return
• Function with only print
Definitions
• Returns None automatically
• Can be called with or without that argument
• Sends value back to be used
• Shows text but returns None
Exercise 17 — Function Returning a List
Complete this function that returns a list of Codémon moves:
def get_moves(codemon_type): if codemon_type == "fire": moves = ____"Flamme", "Inferno"] else: moves = ["Tackle", "Scratch"] ____ moves fire_moves = ____ print(fire_moves)
Options: [ { ( return print get_moves("fire") get_moves(fire)
Hint : Lists start with ```[```, functions return values with ```return```, and string arguments need quotes.
Exercise 18 — Function Naming Best Practice
Which function name follows Python best practices?
Options
• ```calculate_damage```
• ```CalculateDamage```
• ```calculateDamage```
• ```CALCULATE_DAMAGE```
Hint : Python functions use lowercase with underscores (snake_case).
Exercise 19 — Conditional Returns
Complete this function with conditional return values:
def check_battle_result(player_hp, enemy_hp): if player_hp ____ 0 and enemy_hp > 0: ____ "Defeat" elif player_hp > 0 ____ enemy_hp <= 0: ____ "Victory" else: return "Draw" result = check_battle_result(0, 50) print(result)
Options: <= >= and or return print return print
Hint : Use ```<=``` for "less than or equal", ```and``` to combine conditions, and ```return``` to send back values.
Exercise 20 — Default Parameter Values
What happens if you don't provide an argument for a parameter with a default value?
Options
• The default value is used automatically
• The parameter becomes ```None```
• Python raises a ```TypeError```
• The parameter is skipped
Hint : Default values exist specifically to make parameters optional!
Exercise 21 — Function Returning a Dictionary
Complete this function that returns Codémon stats as a dictionary:
def get_stats(name, hp, attack): stats = ____ "name": name, "hp": hp, "attack": attack } ____ stats aquachu = get_stats(____) print(aquachu)
Options: { [ return print "Aquachu", 100, 75 Aquachu, 100, 75 ["name"] ["hp"]
Hint : Dictionaries start with ```{```, use ```return``` to send them back, separate arguments with commas, and access values with ```["key"]```.
Exercise 22 — Parameter vs Argument
In this code, what is
"Pyrochu"
?
def display_codemon(name): print(name) display_codemon("Pyrochu")
Options
• An argument
• A parameter
• A return value
• A local variable
Hint : Parameter is in the definition, argument is the actual value passed.
Exercise 23 — Calling Multiple Functions
Complete the code where one function uses the result of another:
def get_base_damage(): ____ 50 def calculate_critical(damage): ____ damage * 2 base = ____ critical = calculate_critical(____) print(critical)
Options: return print return print get_base_damage() get_base_damage base damage
Hint : Both functions need ```return``` to send values back. Call the first function with ```()``` and store its result.
Exercise 24 — When to Use global Keyword
When do you need the
global
keyword inside a function?
Options
• When you want to MODIFY a global variable
• When you want to READ a global variable
• For all variables in a function
• When creating a new local variable
Hint : You can read global variables freely, but need ```global``` to change their values.
Exercise 25 — Function Concepts Review
Match each concept with its correct definition:
Items to match
• DRY principle
• return statement
• Default parameter
• Variable lifetime
• snake_case
Definitions
• Don't Repeat Yourself - use functions instead
• Sends a value back from function
• Parameter with =value, becomes optional
• Local variables destroyed after function ends
• Python naming: lowercase with underscores
Back to the course