Defining Functions in Python: The Keyword 'def' Explained

Benjamin Jul 11, 2026

In the realm of Python programming, defining a function is a fundamental operation that allows for code reusability and modularity. The keyword that plays a pivotal role in this process is 'def'.

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

This keyword, when followed by the function name and parentheses, initiates the function definition. It's the starting point of any function in Python, making it a crucial component of the language's syntax.

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

Understanding the 'def' Keyword

The 'def' keyword is used to define a function in Python. It's a reserved word, meaning it has a specific purpose in the language and cannot be used for any other purpose. When you see 'def' in Python code, you know that a function definition is about to begin.

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

Here's a simple example of a function definition using 'def': ```python def greet(name): print(f"Hello, {name}!") ```

Function 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?

After the 'def' keyword, you'll find the function name. In the example above, 'greet' is the function name. Function names should be descriptive and follow the same naming conventions as variables in Python, typically using lowercase letters with words separated by underscores.

For instance, 'greet_user' would be a more descriptive function name than 'greet'.

Parameters

Minimalist Tech Workspace Ideas
Minimalist Tech Workspace Ideas

Following the function name, you'll often see parentheses that may contain parameters. Parameters are variables that the function can accept as input. In the 'greet' function example, 'name' is a parameter that allows the function to greet any name passed to it.

Parameters are optional in Python functions. A function can be defined without any parameters, like so: ```python def say_hello(): print("Hello, world!") ```

Defining and Returning Values

IMPORTANT FUNCTIONS IN PYTHON
IMPORTANT FUNCTIONS IN PYTHON

Python functions can also return values using the 'return' keyword. This allows you to use the function as part of an expression, storing the result in a variable or using it in a larger calculation.

Here's an example of a function that returns the square of a number: ```python def square(n): return n ** 2 ```

Functions
Functions
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
a blackboard with instructions on how to use functions in python
a blackboard with instructions on how to use functions in python
Important Methods in Python
Important Methods in Python
Master Python Functions – Write Smarter Code! 🐍✨
Master Python Functions – Write Smarter Code! 🐍✨
a computer screen showing the different types of commands and symbols for each type of device
a computer screen showing the different types of commands and symbols for each type of device
Python Functions Cheat Sheet
Python Functions Cheat Sheet
All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)
required functions
required functions
Python Functions | def function | How to Create Python Functions?⋆
Python Functions | def function | How to Create Python Functions?⋆
User-Defined Functions in Python  Create Your Own Functions Easily📈🤩
User-Defined Functions in Python Create Your Own Functions Easily📈🤩
Python Functions Explained for Beginners (Day 19)
Python Functions Explained for Beginners (Day 19)
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
a blackboard with some text on it that says, what is dictionary comprehension in python?
a blackboard with some text on it that says, what is dictionary comprehension in python?
an image of a computer screen with the words, 1 single return value and numbers
an image of a computer screen with the words, 1 single return value and numbers
Functions in Python 🐍 Beginner friendly and easy to learn 🤩
Functions in Python 🐍 Beginner friendly and easy to learn 🤩
Important functions in Python(Part 2)
Important functions in Python(Part 2)
Python Cheat Sheet
Python Cheat Sheet
Imp💯 Python functions
Imp💯 Python functions
an image of what is lambda function in pyron? and how to use it
an image of what is lambda function in pyron? and how to use it

Returning a Value

The 'return' keyword is used to specify the value that a function should output. In the 'square' function example, the expression 'n ** 2' is returned, which calculates the square of the input number.

You can call this function and store the result in a variable, like so: ```python result = square(5) print(result) # Outputs: 25 ```

Multiple Returns

Python functions can also return multiple values using tuples. This allows you to pack multiple values into a single return statement and unpack them into multiple variables when you call the function.

Here's an example: ```python def divide(x, y): quotient = x // y remainder = x % y return quotient, remainder quotient, remainder = divide(10, 3) print(f"Quotient: {quotient}, Remainder: {remainder}") # Outputs: Quotient: 3, Remainder: 1 ```

In conclusion, understanding and correctly using the 'def' keyword is crucial for writing efficient and maintainable Python code. It's a powerful tool that enables code reuse, modularity, and structured programming. Whether you're a beginner or an experienced Python developer, mastering 'def' and its associated syntax will serve you well in your coding journey.