What Are Functions? ¶
In Python, functions are consolidated blocks of code designed to perform specific tasks. They allow you to break down complex problems into smaller, manageable parts that only do one thing. Functions are reusable, meaning you can call the same function multiple times with different inputs, which is especially useful in computational tasks.
A function , by definition, is a named block of code that can accept input parameters, perform operations, and return a result. Think of it as a tool that takes some input, processes it, and provides an output.
The def keyword ¶
In Python, every function starts with the special keyword
def
.
This keyword tells Python "I want to create a new function."
Here's what it looks like:
def
You'll never use
def
by itself - it's always the starting point for creating a function.
Some languages use different keywords to create functions (like
function
in JavaScript), but Python keeps it short and simple with
def
.
❌ Common Mistake:
Define my_function(): # Wrong! Python won't understand this
DEF my_function(): # Wrong! Must be lowercase
✅ Correct:
def my_function(): # Correct! Always use 'def'
Function names ¶
After the
def
keyword, we give our function a name.
The name is crucial - it tells other programmers (and yourself!) what the function does.
def calculate_area(): # The name describes exactly what this function does
Python has strict rules about function names:
- Must start with a letter or underscore;
- Can contain letters, numbers, and underscores;
-
Cannot use Python keywords (like
def
,if
,for
); -
Case sensitive (
calculate_area
andCalculate_Area
are different).
Good function names clearly explain the function's purpose and often start with verbs. For example,
def calculate_total():
def convert_temperature():
def get_user_name():
def validate_email():
Functions should also be written in
snake_case
, which means all lowercase with underscores between words.
def send_email(): # ✅ Good
def SendEmail(): # ❌ Bad: Don't use CamelCase
def sendemail(): # ❌ Bad: Hard to read
You want to avoid very vague, non-descriptive names as they are not helpful to communicate what the function does.
def x(): # ❌ Too vague
def function1(): # ❌ Not descriptive
def do_stuff(): # ❌ Unclear purpose
def thisIsAFunction(): # ❌ Wrong convention
Think of your function name as a mini-description.
When another programmer sees
calculate_monthly_interest()
, they immediately know what the function does without having to read its code.
Parameters ¶
After the function name comes a pair of parentheses
()
.
These parentheses are where we define our function's
parameters
- the inputs that the function expects to receive.
def calculate_area(length, width): # length and width are parameters
Parameters are like variables that get their values when someone uses your function. They:
- Act as placeholders for the actual values;
- Make your functions flexible and reusable;
- Are listed inside the parentheses, separated by commas.
Here are some examples with different numbers of parameters.
def greet():
print("Hello!")
def greet_person(name):
print("Hello,", name + "!")
def greet_person_formally(first_name, last_name):
print("Hello,", first_name, last_name + "!")
greet()
greet_person("Bob")
greet_person_formally("Bob", "Smith")
Hello! Hello, Bob! Hello, Bob Smith!
Why parameters matter ¶
Compare these two versions of a greeting function.
# Without parameters (less useful)
def greet_bob():
print("Hello, Bob!")
# With parameters (more flexible)
def greet_anyone(name):
print(f"Hello, {name}!")
greet_anyone("Bob")
greet_anyone("Alice")
greet_bob()
Hello, Bob! Hello, Alice! Hello, Bob!
Parameter Naming ¶
Just like function names, parameters should be:
- Descriptive of what they represent
-
Written in
snake_case
- Clear and meaningful
# ✅ Good parameter names
def calculate_circle_area(radius):
def send_email(recipient, subject, message):
# ❌ Poor parameter names
def calculate_circle_area(x):
def send_email(a, b, c):