A function error stops your code before it runs

"Could not find function" is an error message that appears when your code tries to use a function that does not exist, has not been loaded, or is spelled wrong. The program looks for the function by name, cannot find it anywhere, and stops running. This is one of the most common errors in programming because functions are how code actually does things — if the function is missing, nothing happens.

The error usually appears in the console or terminal window where you ran your code. It tells you the name of the function it could not find, and sometimes which line of code tried to use it. That information is your map to fixing it.

Key Takeaways

  • A "could not find function" error means your code tried to call a function that does not exist, is misspelled, or has not been loaded into memory yet.
  • Check the spelling of the function name first — capitalization matters, and a single letter wrong will cause this error.
  • Make sure the function is defined before you try to use it, or that the library or package containing it has been imported or installed.
  • Different programming languages show this error with different names: Python says "NameError", JavaScript says "ReferenceError", and R says "could not find function".
  • The error message usually tells you the exact function name and line number, which narrows down where to look.

Spelling and capitalization mistakes are the most common cause

Functions are case-sensitive in most programming languages, meaning Print and print are two different things. If you type the function name with even one letter wrong, the program will not find it. Check the exact spelling against where the function is defined or documented.

Common mistakes include adding or removing underscores, confusing similar names, or using camelCase when the function uses snake_case. For example, if a function is named calculate_total but you typed calculateTotal, the error will appear. Read the error message carefully — it usually shows you the name it was looking for, which makes the typo obvious once you see it side by side with the correct name.

The function may not be defined yet in your code

In many languages, you must define a function before you use it. If your code tries to call a function on line 5 but the function is not defined until line 20, you will get this error. The program reads your code from top to bottom and does not know the function exists when it reaches the call.

Move the function definition to the top of your file, or move the function call to after the definition. Some languages like JavaScript have hoisting, which moves function definitions automatically, but this does not work for all types of functions or all situations. The safest approach is to define functions first, then use them.

The library or package containing the function has not been loaded

Many functions live in libraries or packages that you have to import or load before using them. If you try to use a function from a library without importing it first, you will get this error. For example, in Python, the requests library has a function called get(), but you must write import requests at the top of your file before you can use requests.get().

Check the documentation for the function to see which library it belongs to and what import statement you need. Make sure the library is installed on your computer or server. In Python, you install packages with pip install package_name. In JavaScript, you use npm install package_name. In R, you use install.packages("package_name"). Once installed, you still need to import it in your code.

The function may be in a different file or module

If you are working with multiple files, a function defined in one file is not automatically available in another. You have to import it or reference it by its file path. In Python, if a function called add_numbers is in a file named math_helpers.py, you would write from math_helpers import add_numbers at the top of the file where you want to use it.

Check where the function is actually defined. If it is in another file, look up the import syntax for your language and add the correct import statement. If the function is in a class or object, you may need to use dot notation like object.function_name() instead of just function_name().

How to read the error message to find the problem

The error message usually contains three useful pieces of information: the name of the function it could not find, the line number where the error happened, and sometimes the file name. Start by looking at that line in your code and checking the function name spelling. Then trace backward to see if the function is defined or imported above that line.

If the error message shows a line number that seems wrong, remember that some languages count lines starting at 0 instead of 1, or the error might be reported on the line after the actual problem. Look at the line number given and the few lines around it. The function name in the error message is your best clue — search your entire project for where that function should be defined.

Different languages use different names for this error

Python calls this a NameError and says something like "name 'function_name' is not defined". JavaScript calls it a ReferenceError and says "function_name is not defined". R says "could not find function" directly. Java and C++ may say "method not found" or "undefined reference". The message is the same — the function does not exist where the code is looking — but the wording changes by language.

If you are learning a new language, search for the error message using that language's name. A search for "Python NameError" will give you Python-specific solutions, while "JavaScript ReferenceError" will show you JavaScript fixes. The underlying problem is always the same, but the solution may depend on how that language handles functions and imports.

Frequently Asked Questions

Why does the error say the function is not defined when I can see it in my code?

The function is probably defined after the line that tries to use it. Your program reads code from top to bottom, so it does not know the function exists yet. Move the function definition to the top of your file, or move the function call to after the definition. Some languages hoist functions automatically, but not all.

I imported the library but still get "could not find function". What is wrong?

Check that you imported the function correctly. If the library is called requests and the function is get, you need from requests import get or import requests followed by requests.get(). Check the library's documentation for the exact import syntax. Also make sure the library is actually installed by running the install command for your language.

The function works in one file but not another. Why?

Functions defined in one file are not automatically available in other files. You must import them. In Python, if the function is in helpers.py, write from helpers import function_name at the top of the file where you want to use it. Check your language's import syntax and make sure the file path is correct.

Does capitalization really matter for function names?

Yes. In almost all programming languages, Print, print, and PRINT are three different functions. If the function is defined as print but you type Print, you will get this error. Always match the exact capitalization shown in the documentation or where the function is defined.

How do I know which library a function belongs to?

Search the function name plus your language name in a search engine, or check the official documentation for your language. Most functions are documented online with examples showing which library to import. If you copied code from somewhere, check the source — it usually shows the import statements at the top of the example.