- Courses
- 💻Programming Languages
- 🐍Python
- Functions and Modular Programming
- Defining Functions
On this page
Defining Functions
By Enrico Hochkoeppler
Web Developer & TutoriaLibre Founder
Understanding What a Function Is
A function is a named, reusable block of code designed to perform a specific task. Instead of repeating the same logic throughout a program, you define it once and call it whenever needed. This is the foundation of modular programming and one of the most important skills in Python.
Functions improve code quality by:
- Reducing duplication
- Organizing logic into meaningful units
- Making programs easier to read and maintain
- Supporting testing and long‑term scalability
The Structure of a Function Definition
Python uses the def keyword to define a function. A function definition consists of:
- A name
- A parameter list (which may be empty)
- A colon
- An indented block of code (the function body)
Example:
def greet():
print("Hello!")
Calling the function:
greet()
Each call executes the function body.
Parameters: Providing Input to a Function
Parameters allow a function to receive data. They are listed inside the parentheses in the function definition.
def greet(name):
print("Hello,", name)
Calling the function:
greet("Alice")
The value "Alice" is passed into the parameter name.
Multiple Parameters
You may define as many parameters as needed:
def add(a, b):
print(a + b)
This function can now operate on any two numbers.
Returning Values
A function can compute a result and return it using the return statement.
def add(a, b):
return a + b
Using the returned value:
result = add(3, 4)
print(result)
Important points:
returnends the function immediately.- The expression after
returnbecomes the function’s output. - If no
returnis used, the function returnsNone.
Returning Multiple Values
Python allows returning multiple values as a tuple:
def stats(a, b):
return a + b, a * b
You can unpack the result:
total, product = stats(3, 4)
This is a common pattern in Python programs.
Default Parameter Values
Default values make parameters optional.
def greet(name="friend"):
print("Hello,", name)
Usage:
greet() # uses default
greet("Alice") # overrides default
Defaults improve usability and reduce repetitive calls.
Positional and Keyword Arguments
Arguments can be passed by position or by name.
Positional:
introduce("Alice", 30)
Keyword:
introduce(name="Alice", age=30)
Keyword arguments improve clarity and allow reordering.
Documenting Functions with Docstrings
A docstring is a string placed immediately after the function definition. It explains what the function does, its parameters, and its return value.
def add(a, b):
"""Return the sum of a and b."""
return a + b
Docstrings are essential for maintainability and professional‑quality code.
Local Scope and Variable Lifetime
Variables created inside a function are local to that function.
def compute():
x = 10
return x * 2
The variable x exists only while the function runs. This prevents accidental interference between different parts of a program.
Characteristics of Well‑Designed Functions
Effective functions share several qualities:
- Clear, descriptive names
- A single, well‑defined purpose
- Explicit inputs and outputs
- Short, readable bodies
- Proper documentation
These principles lead to code that is easier to test, maintain, and extend.
Summary
In this lesson, you learned how to define Python functions using def, how to work with parameters and return values, how to document functions properly, and how scope affects variable behavior. These concepts form the foundation for modular programming and prepare you for more advanced function features in the next lessons.
Found this helpful? Share the knowledge...
... or support this project to keep it alive!
Previous
Functions and Modular Programming
Next
Arguments & Return Values
-
Understanding What a Function Is
-
The Structure of a Function Definition
-
Parameters: Providing Input to a Function
- Multiple Parameters
-
Returning Values
- Returning Multiple Values
-
Default Parameter Values
-
Positional and Keyword Arguments
-
Documenting Functions with Docstrings
-
Local Scope and Variable Lifetime
-
Characteristics of Well‑Designed Functions
-
Summary
