01 / 20

Defining Functions
in Python

Learn to build reusable blocks of code with def and return: the foundation of every Python program.

Python

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.

Reusable
Write once, call many times throughout your program.
Organized
Break complex problems into small, manageable pieces.
Testable
Each function can be tested independently for correctness.

Anatomy of a Function

1 def greet ( name ): 2 message = "Hello, " + name 3 return message keyword starts definition parameter input variable return sends back output body 5 result = greet ( "Alice" ) → "Hello, Alice"

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:

def circle_area(radius):
    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.

DEFINE def add(a, b):   return a + b Stored in Memory Not executed yet CALL result = add(3, 5) EXECUTE result → 8

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.

# 'name' is a parameter
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.

With return
def double(x):
  return x * 2

result = double(5)
result → 10
Without return
def double(x):
  print(x * 2)

result = double(5)
result → None

print() vs return

print(value) Console Screen output value lost return value Caller Receives value value saved Use return when you need the result later. Use print for debugging.

How to Define a Function

1
Start with def. This keyword tells Python a function definition follows.
2
Choose a descriptive name. Use lowercase with underscores: calculate_tax, not CalcTax.
3
List parameters in parentheses. Use () for no inputs, or (a, b) for multiple.
4
End the header with a colon. The colon : signals the indented body follows.
5
Indent the body by 4 spaces. Write your logic and optionally use return to send back a result.

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 \)

def to_fahrenheit(celsius):
    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:

def rectangle_area(width, height):
    area = width * height
    return area
rectangle_area(4, 7) Parameter Mapping width ← 4 height ← 7 return → 28 result = 28

Example 3: Default Parameters

You can give parameters default values. If the caller omits an argument, the default is used instead.

def power(base, exponent=2):
    return base ** exponent
With default
power(5)
exponent defaults to 2
Result: 25
Override default
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.

def min_max(numbers):
    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.

Global Scope x = 10 Local Scope (inside def double): y = x * 2 ← reads global x return y ← y is local print(y) ← NameError! y does not exist here

Common Mistakes

✕ Forgetting return
Using print() instead of return. The function outputs to console but returns None.
✕ Missing colon
Writing def add(a, b) without the : at the end causes a SyntaxError.
✕ Wrong indentation
Forgetting to indent the function body. Python uses indentation instead of braces, so this is required.
✕ Calling before defining
Calling a function above its 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

What does print(result) output?

Question 3 of 5

What does greet("Bob") return?

Question 4 of 5

What happens when print(y) runs?

Question 5 of 5

Which of the following correctly defines a function that returns the sum of three numbers?