NameError: function is not defined in Python

Python provides several open-source modules and inbuilt functions that are used to perform simple to complex tasks. The “def” keyword is used in Python to define the function manually. To access the function in the program, it is essential to define the function. This is because if the function is not declared, the “NameError: function is not defined” occurs in the program. To fix this error, different solutions are used in Python.

This Python write-up will provide a comprehensive guide on how to rectify the “function is not defined” error using the given below contents:

Reason 1: Calling Function That is Not Declared/Defined

The prominent reason which causes this error in Python is when the user calls a function that is not declared in the program. Let’s look at the below snippet for clarification:

The above snippet shows “NameError” because the function “solve” is not declared/defined in the program.

Reason 2: Calling Function Before it is Declared

The other main reason which causes this error in Python is when a user tries to call/access a function before the declaration. Here is an example:

The above snippet shows “NameError” because the function “solve” is accessed before the declaration.

Solution: Declared Function Before Accessing

To resolve the “function is not defined” error in Python, you need to declare the function in the program.

The function must be declared/defined before accessing it in the program. Here is an example code:

Code:

def solve(a, b):
    return a/b
   
x =solve(4, 23)
print(x)

In the above code, the function “solve” is defined in the program. The function is accessed after the declaration, and parameter values for “a” and “b” are passed to the function at the time of the call.

Output:

The output authenticates that declaring the function before calling/accessing resolves the stated error.

Reason 3: Calling Function Incorrectly (Misspelled)

Typo mistakes(misspelled names) are another common cause of “NameError” in python programs. Here is an error snippet:

The above snippet shows an error because the “solve()” function is misspelled while accessing.

Solution: Call Function With Correct Name

To resolve this error, we need to recheck the program and correct the name of our defined function. Python is case-sensitive, so we also need to ensure that every alphabet of function must be the same while accessing.

Code:

def solve(a, b):
    return a/b
   
x =solve(4, 23)a
print(x)

In the above code, the “solve()” function is accessed using the correct name, so it returns the division of parameters “a” and “b”.

Output:

The output shows that correcting the function’s spelling resolves the “function is not defined” error.

Reason 4: Calling Built-in Module Function Without Importing

The error also occurs when users access the standard module function without being imported the module at the start of the program.

The above output shows an error because the panda’s function “DataFrame()” has been accessed without importing the “pandas” module.

Solution: Import the Module

The error can be resolved by importing the module at the beginning of the program before accessing the function. Here is an example code:

Code:

import pandas as pd

dataframe = pd.DataFrame({'Name': ['Alex','Lily'],'Position': [1, 5], 'Marks': [55, 70]})
print(dataframe)

In the above code, the “pandas” module is imported as pd at the start of the program. Next, the “pd.DataFrame()” function accepts the dictionary value as an argument and returns the DataFrame.

Output:

The above output shows that importing the module at the program’s start resolves the stated error, i.e., a “DataFrame” is created successfully.

Reason 5: Accessing Nested Function From Outer Scope

One other reason which causes this error in Python is when a user tries to access the nested function in the outer function. Here is an example:

The above snippet shows “NameError” because the inner function is accessed outside the scope.

Solution: Declare the Function in the Outer Scope

To fix this error, the nested function must be defined in the outer scope. The function will be accessed anywhere in the program after declaration. Let’s understand it via the following code:

Code:

def school():
    print('John is My Name: ')
def students():
    print('John')

school()
students()

In the above code, the function “school” and “students” is defined using the keyword “def” in the program at the outer scope.

Output:

The “school” and “students” functions are accessed without any error because both these functions have outer scope.

Conclusion

The “function is not defined” error occurs when the user tries to access the function without declaring it or accessing it before declaring it in Python. To fix this error, different solutions are used in Python, such as accessing the function after the declaration, importing the built-in function before accessing it, etc. This article explained different reasons and solutions that help us resolve the “function is not defined” error in Python.