Defining Functions in Python: The Keyword 'def'

Benjamin Jul 11, 2026

In the realm of programming, particularly in Python, defining functions is a fundamental aspect that enables code reusability and modularity. A key component of this process is the use of a specific keyword. Let's delve into the world of Python functions, focusing on the crucial role played by the 'def' keyword.

a table with words that are used to describe the keywords for each keyword
a table with words that are used to describe the keywords for each keyword

The 'def' keyword in Python is akin to a road sign, guiding the interpreter to understand that a function definition is about to commence. It's the first step in creating a function, which is a block of organized, reusable code that performs a related action.

Functions in Python | Types, Example
Functions in Python | Types, Example

Understanding the 'def' Keyword

The 'def' keyword is a reserved word in Python, meaning it has a predefined meaning and cannot be used as a variable or function name. It's used to define a function, marking the start of a function definition.

Coding For Beginners Python - Learn the Basics - Functions
Coding For Beginners Python - Learn the Basics - Functions

At its core, the 'def' keyword is followed by the function name, parentheses (which may contain parameters), and a colon. This is the basic syntax: 'def function_name(parameters):'.

Function Name

Minimalist Tech Workspace Ideas
Minimalist Tech Workspace Ideas

The function name in Python follows the same rules as variable names. It should be a descriptive word or phrase that gives an idea of what the function does. For example, 'calculate_area' is a more descriptive function name than 'f'.

Function names are case-sensitive, so 'calculate_area' and 'Calculate_area' would be considered two different functions. It's a best practice to use lowercase letters with words separated by underscores (snake_case) for function names.

Parameters

Functions
Functions

Parameters are optional values that a function can accept. They are defined inside the parentheses following the function name. For instance, in 'def greet(name):', 'name' is a parameter.

Parameters allow functions to perform different actions based on the values they receive. They make functions more versatile and reusable. For example, a 'greet' function can greet different people based on the name parameter it receives.

Defining Functions with 'def'

an info sheet with the words important method in python, set list and keywords
an info sheet with the words important method in python, set list and keywords

After the 'def' keyword and the function definition, the code block that follows (indented under 'def') is the function's body. This is where the actual work of the function is done.

Here's a simple example of a function definition using 'def': ```python def greet(name): print(f'Hello, {name}!') ``` In this example, 'greet' is the function name, and 'name' is the parameter. The function's body prints a greeting using the provided name.

a blackboard with an image of a hello kitty on it and the words what is a functions in python?
a blackboard with an image of a hello kitty on it and the words what is a functions in python?
IMPORTANT FUNCTIONS IN PYTHON
IMPORTANT FUNCTIONS IN PYTHON
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
Important Methods in Python
Important Methods in Python
Python Functions Cheat Sheet
Python Functions Cheat Sheet
Master Python Functions – Write Smarter Code! 🐍✨
Master Python Functions – Write Smarter Code! 🐍✨
an image of a computer screen with the words,'the input function in python '
an image of a computer screen with the words,'the input function in python '
Python Functions
Python Functions
User-Defined Functions in Python  Create Your Own Functions Easily📈🤩
User-Defined Functions in Python Create Your Own Functions Easily📈🤩
required functions
required functions
a blackboard with instructions on how to use functions in python
a blackboard with instructions on how to use functions in python
20 Python Built-in Functions Every Programmer Must Know
20 Python Built-in Functions Every Programmer Must Know
Functions in Python 🐍 Beginner friendly and easy to learn 🤩
Functions in Python 🐍 Beginner friendly and easy to learn 🤩
Imp💯 Python functions
Imp💯 Python functions
a table that has different types of words and phrases on it, all in purple
a table that has different types of words and phrases on it, all in purple
an image of different types of words in python
an image of different types of words in python
Python Functions Explained for Beginners (Day 19)
Python Functions Explained for Beginners (Day 19)
an important python functions chart with the words and numbers on it, including instructions to use them
an important python functions chart with the words and numbers on it, including instructions to use them
a diagram showing the different types of python and other programming languages, with text below it
a diagram showing the different types of python and other programming languages, with text below it
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast

Calling Functions

Once a function is defined, it can be 'called' or invoked to perform its action. This is done by writing the function name followed by parentheses. If the function has parameters, you provide the values inside the parentheses.

Using the 'greet' function from the previous example, you can call it like this: ```python greet('Alice') ``` This would output: 'Hello, Alice!'

Function Return Values

Functions in Python can also return a value, which can be used later in the code. The 'return' statement is used to specify the value to be returned. If no return statement is present, the function will return 'None'.

Here's an example of a function that returns a value: ```python def add_numbers(a, b): result = a + b return result ``` You can call this function and use its return value like this: ```python sum = add_numbers(3, 5) print(sum) # Outputs: 8 ``` In this case, the function 'add_numbers' returns the sum of 'a' and 'b', which is then stored in the variable 'sum' and printed.

Python's 'def' keyword is a powerful tool that enables code organization, reusability, and modularity. It's a key part of what makes Python a versatile and efficient programming language. Whether you're a seasoned programmer or just starting out, understanding and using 'def' effectively is crucial to your Python journey.

So, the next time you're writing Python code and you want to define a function, remember the 'def' keyword. It's your key to unlocking the power of functions in Python.