Developers define a function in Python. But when they call it in the program, an error message shows up: NameError: function is not defined
in Python. There are different causes for this issue. In this article, we will explain all the causes and reveal the solutions for each.
Cause of NameError: function is not defined in Python
There are 3 possible causes for NameError: function is not defined
in Python.
You have not defined the function
If you don’t define the function in Python, there is no way to call it. Python does not understand any function that is not either defined or built-in.
Here is the code sample that leads to the issue:
# Call the function printHello()
The error message will show up:
printHello()
NameError: name 'printHello' is not defined
You write the wrong name of function when calling it
You have defined the function. However, the error message still shows up. You have to check whether you have written the function name correctly when calling it or not. If the name is called wrong, Python will not understand the function.
Here is the code sample that leads to the issue:
# Define the function def printHello(): print('Hello!') # Call the function print_Hello()
The error message shows up:
print_Hello()
NameError: name 'print_Hello' is not defined
You call the function before defining it
The function has been defined and called correctly, but the code still does not run. You need to make sure that the function is called after it is defined.
While many other programming languages allow developers to call the function everywhere, Python does not support this feature.
Here is the sample for the NameError:
# Call the function printHello() # Define the function def printHello(): print('Hello!')
How to fix NameError: function is not defined in Python
Follow 2 steps below to fix the NameError: function is not defined in Python:
Step 1: Define the function before calling it
Make sure that the definition codes of the function stay above the function calling codes in your Python program. You define the function like so:
def [function name]:
[code]
Here is the code sample:
def printHello(): print('Hello!')
There is no output as you have just defined the function.
Step 2: Call the function correctly
After defining the function, you call it in your Python program to make it work. Don’t forget to write the function name correctly!
Below is a code sample:
# Define the function def printHello(): print('Hello!') # Call the function printHello()
The output will be:
Hello!
If you define the function to have a parameter, make sure to fill a value in the () of the function when calling it. Otherwise, the program may still not run properly.
Here is the code sample:
# Define the function def printHello(strHello): print(strHello) # The string strHello should work as a parameter for the function strHello = "Hello!" # Call the function printHello(strHello)
The output will be:
Hello!
Summary
In conclusion, the NameError: function is not defined in Python occurs when you have not defined a function, call it wrong in the program, or call it before defining it. To make the function works, you should follow our 2 steps carefully!

I am William Nguyen and currently work as a software developer. I am highly interested in programming, especially in Python, C++, Html, Css, and Javascript. I’ve worked on numerous software development projects throughout the years. I am eager to share my knowledge with others that enjoy programming!