How to Create a Simple Function in Python
A Python function lets you group code into a reusable block. This page shows the simplest working pattern first: define a function, then call it.
You will also learn how to avoid common beginner mistakes like missing parentheses or bad indentation. Once this basic pattern makes sense, it becomes much easier to learn Python functions explained, parameters, and return values.
Quick answer
def say_hello():
print("Hello")
say_hello()
Use def to create the function, indent the code inside it, then call the function by writing its name with parentheses.
What this page helps you do
- Create your first Python function
- Understand the basic function pattern
- Run the function by calling it
- Avoid beginner mistakes like missing parentheses or bad indentation
Start with the basic function structure
A function is a reusable block of code.
In Python, you create a function with the def keyword:
def function_name():
# code goes here
This pattern has four important parts:
defstarts the function definitionfunction_nameis the name you choose()means the function takes no inputs for now:starts the function body
The code inside the function must be indented.
Create and call a simple function
Here is a simple function that prints a message:
def say_hello():
print("Hello")
say_hello()
Expected output:
Hello
This example does two things:
- It defines the function
say_hello - It calls the function with
say_hello()
A very important rule for beginners:
- Defining a function does not run it
- Nothing happens until you call the function
If you only write this:
def say_hello():
print("Hello")
you will see no output, because the function was created but never called.
Understand each line
Look at the same example again:
def say_hello():
print("Hello")
say_hello()
Here is what each line does:
def say_hello():starts the function definitionsay_hellois the function name:tells Python that the function body starts nextprint("Hello")is inside the function because it is indentedsay_hello()runs the code inside the function
You can test that the function exists by printing its name without calling it:
def say_hello():
print("Hello")
print(say_hello)
print(type(say_hello))
You will see that Python treats the function as an object:
<function say_hello at 0x...>
<class 'function'>
This is why say_hello and say_hello() are different:
say_hellorefers to the functionsay_hello()calls the function
Choose a good function name
A good function name should describe what the function does.
Use:
- lowercase letters
- underscores between words
- action-based names
Good examples:
print_nameshow_menuget_total
Avoid vague names like:
thingstuffdo_stuff
Also avoid names that conflict with Python built-ins, such as list or print. Reusing built-in names can cause confusing bugs later.
Add a second simple example
Functions are useful because you can reuse them.
Without a function, you might repeat the same line many times:
print("Welcome!")
print("Welcome!")
print("Welcome!")
With a function, you write the code once and call it whenever you need it:
def show_welcome():
print("Welcome!")
show_welcome()
show_welcome()
show_welcome()
Expected output:
Welcome!
Welcome!
Welcome!
This is one of the main reasons functions are helpful:
- less repeated code
- easier to update later
- clearer program structure
Common beginner mistakes
Here are some common problems when creating a simple function.
Forgetting the colon
This is wrong:
def say_hello()
print("Hello")
Python expects a colon after the function header.
Correct version:
def say_hello():
print("Hello")
If you get this wrong, see how to fix SyntaxError from a missing colon.
Forgetting to indent the function body
This is wrong:
def say_hello():
print("Hello")
The body of the function must be indented.
Correct version:
def say_hello():
print("Hello")
If Python says it expected an indented block, read how to fix IndentationError: expected an indented block.
Using the function name without ()
This does not call the function:
def say_hello():
print("Hello")
say_hello
To run the function, you must add parentheses:
def say_hello():
print("Hello")
say_hello()
Calling the function before it is defined
In a Python file, the function usually needs to be defined before you call it.
Wrong order:
say_hello()
def say_hello():
print("Hello")
Correct order:
def say_hello():
print("Hello")
say_hello()
Expecting a printed value to be returned
This function prints a message:
def say_hello():
print("Hello")
But it does not return a value.
That means this:
result = say_hello()
print(result)
will output:
Hello
None
print("Hello")shows text on the screen- the function returns
Nonebecause there is noreturn
If this part feels confusing, the next step is learning return values in Python functions.
What to learn next
Once you can create and call a simple function, the next step is making functions more useful.
You can learn how to:
- pass inputs with function parameters and arguments in Python
- send a result back with return values in Python functions
- make inputs optional with default arguments in Python
FAQ
What is a function in Python?
A function is a reusable block of code that runs when you call it.
How do I create a function in Python?
Use def, write the function name, add parentheses and a colon, then indent the code inside it.
Why is my function not running?
It may be defined correctly but not called. Write the function name followed by parentheses to run it.
Do functions always need parameters?
No. A simple function can work without parameters.
What is the difference between print and return?
print shows output on the screen. return sends a value back from the function.