- Core Definitions
- Key Differences:
printvs.return - Defining and Calling Functions
- Parameters and Arguments
- Return Values
- Advanced Concepts
- Quick Reference
- Pro Tips
- Common Mistakes to Avoid
- Function: A named, reusable block of code that performs a specific action.
def: The keyword used to define a function.- Call: The act of executing a function by writing its name followed by parentheses
(). - Parameter: A variable name listed in a function's definition. It's a placeholder for an input value.
- Argument: The actual value that is passed to a function when it is called.
return: A keyword that exits a function and sends a value back to the caller.- Recursion: The process of a function calling itself to solve a problem.
| Feature | print() |
return |
|---|---|---|
| Purpose | Displays a value to the screen for a human to see. | Sends a value back to the program for further use. |
| Output | Visual output in the console. | A value that can be stored in a variable. |
| Use Case | Showing status, results, or messages. | Providing a result for calculations or assignments. |
# 1. Define the function
def show_greeting():
print("Hello from a function!")
# 2. Call the function to run its code
show_greeting()# 'name' is the parameter (a placeholder)
def greet(name):
print(f"Hello, {name}!")
# "Alice" and "Bob" are the arguments (the actual values)
greet("Alice")
greet("Bob")# This function calculates a value and sends it back
def square(number):
return number * number
# You must store the returned value in a variable to use it later
result = square(5) # result is now 25
print(result)Functions can be broken down into smaller "helper" functions.
def calculate_tax(price):
"""Calculates an 8% tax for a given price."""
return price * 0.08
def get_final_price(price):
"""Calculates the final price by adding tax."""
tax = calculate_tax(price) # Calling the helper function
return price + tax
final_cost = get_final_price(100)
print(f"The final cost is ${final_cost:.2f}") # The final cost is $108.00A function can call itself, but it must have a base case to stop.
def countdown(n):
# Base Case: The condition to stop the recursion.
if n <= 0:
print("Blast off!")
return
# Recursive Step: The function calls itself with a smaller problem.
print(n)
countdown(n - 1)
countdown(3)# Defining a function
def function_name(parameter1, parameter2):
# Code to be executed
result = parameter1 + parameter2
return result
# Calling a function and storing its return value
my_result = function_name(argument1, argument2)It's good practice to add a "docstring" to explain what your function does.
def add(a, b):
"""This function takes two numbers and returns their sum."""
return a + b-
One Function, One Job: The best functions do one specific thing and do it well. This makes them easy to test, reuse, and understand.
-
Use Helper Functions: Don't be afraid to break a large function into several smaller ones. This improves readability and organization.
-
Default Parameter Values: You can provide a default value for a parameter, making it optional when the function is called.
def greet(name="World"): print(f"Hello, {name}!") greet("Alice") # Prints "Hello, Alice!" greet() # Prints "Hello, World!"
❌ Forgetting to Call the Function
# Wrong: This code defines the function but never runs it.
def say_hello():
print("Hello!")
# Nothing will be printed.✅ Always Call the Function
# Correct
def say_hello():
print("Hello!")
say_hello() # This line actually executes the code.❌ Forgetting to return a Value
# Wrong: This function calculates a sum but doesn't send it back.
def add(a, b):
result = a + b
# No return statement!
total = add(5, 3)
print(total) # This will print 'None' because the function returned nothing.✅ Always return the Result
# Correct
def add(a, b):
return a + b
total = add(5, 3)
print(total) # This correctly prints 8.❌ Infinite Recursion
# Wrong: There is no base case to stop the function from calling itself.
def uh_oh(n):
print("Oh no!")
uh_oh(n) # This will run forever and crash.✅ Always Have a Base Case
# Correct: The `if` statement provides an exit.
def safe_recursion(n):
if n <= 0:
return
safe_recursion(n - 1)Happy coding! 🐍✨