Defining Functions
in Python
Learn to build reusable blocks of code with def and return: the foundation of every Python program.
What Is a Function?
A function is a named, reusable block of code that performs a specific task. Instead of writing the same logic over and over, you define it once and call it whenever you need it.
Anatomy of a Function
Why Functions Matter
Imagine you need to calculate the area of a circle in ten different places in your program. Without functions, you copy the formula each time. With a function, you write it once:
return 3.14159 * radius ** 2
The def Keyword
The def keyword tells Python you are defining a new function. Python reads the definition and stores it, but does not run the body until you call the function.
Parameters vs Arguments
These two terms are often confused. A parameter is the variable name in the function definition. An argument is the actual value you pass when calling the function.
def greet(name):
print("Hi, " + name)
# "Alice" is an argument
greet("Alice")
| Parameter | Argument | |
|---|---|---|
| Where | In the definition | In the call |
| What | A placeholder variable | An actual value |
| Example | name | "Alice" |
The return Statement
The return statement sends a value back to whoever called the function. Without it, the function returns None by default.
def double(x): return x * 2result = double(5)result → 10
def double(x): print(x * 2)result = double(5)result → None
print() vs return
How to Define a Function
calculate_tax, not CalcTax.() for no inputs, or (a, b) for multiple.: signals the indented body follows.Example 1: Simple Function
Write a function that takes a temperature in Celsius and returns it in Fahrenheit. The formula is: \( F = C \times \frac{9}{5} + 32 \)
return celsius * 9 / 5 + 32
Example 2: Multiple Parameters
Functions can accept multiple parameters, separated by commas. Here, we calculate the area of a rectangle:
area = width * height
return area
Example 3: Default Parameters
You can give parameters default values. If the caller omits an argument, the default is used instead.
return base ** exponent
power(5)exponent defaults to 2
Result: 25
power(5, 3)exponent set to 3
Result: 125
Returning Multiple Values
Python functions can return multiple values as a tuple. You can unpack them into separate variables.
return min(numbers), max(numbers)
lo, hi = min_max([3, 1, 7, 2])
# lo = 1, hi = 7
Scope: Local vs Global
Variables created inside a function are local: they exist only while the function runs. Variables created outside are global.
Common Mistakes
print() instead of return. The function outputs to console but returns None.def add(a, b) without the : at the end causes a SyntaxError.def statement causes a NameError at runtime.Function Cheat Sheet
| Concept | Syntax | Example |
|---|---|---|
| Define | def name(params): |
def add(a, b): |
| Return | return value |
return a + b |
| Call | name(args) |
add(3, 5) |
| Default param | param=value |
def f(x=0): |
| Multiple returns | return a, b |
lo, hi = f() |
Question 1 of 5
What keyword is used to define a function in Python?
Question 2 of 5
Consider this code:
def mystery(a, b):
print(a + b)
result = mystery(3, 4)
print(result)
What does print(result) output?
Question 3 of 5
Consider this code:
def greet(name, greeting="Hello"):
return greeting + ", " + name
What does greet("Bob") return?
Question 4 of 5
Consider this code:
def calculate(x):
y = x * 2
return y
calculate(5)
print(y)
What happens when print(y) runs?
result = calculate(5) and then print(result).Question 5 of 5
Which of the following correctly defines a function that returns the sum of three numbers?