What "Could Not Find Function" Means and Why It Happens
A "Could not find function" error means your code is trying to use a function that does not exist, is not loaded, or is not visible in the part of the code where you are trying to use it. The error stops your program from running because the computer cannot do what you asked it to do.
This happens most often when you misspell a function name, forget to import or load the code that contains the function, or try to use a function from a library or package that is not installed on your computer. Sometimes the function exists but is hidden inside another function or file, and your code does not know where to look for it.
The exact error message changes depending on what programming language you are using — Python, R, JavaScript, Java, and others all word it slightly differently — but the cause is always the same: the function you called is not where your code expects it to be.
Key Takeaways
- Check the spelling of the function name first, character by character, because even one wrong letter will cause this error.
- Make sure you have imported or loaded the library or package that contains the function before you try to use it.
- Verify that the function is defined in your code before the line where you are trying to call it, not after.
- Look at the exact error message your program gives you, because it usually tells you the name of the function it could not find and sometimes the line number where the problem is.
Check the Spelling of the Function Name
The most common reason for this error is a typo. Function names are case-sensitive, which means myFunction, myfunction, and MyFunction are three different things to your computer. If you write one and your code defines the other, you will get the error.
Go back to the line where the error happened and read the function name character by character. Compare it to where the function is actually defined in your code. Check for capital letters in the wrong places, extra letters, missing letters, or numbers that should be spelled out as words. If you are using a function from a library, check the official documentation for that library to see the exact spelling.
Many code editors have a search function (usually Ctrl+F on Windows or Command+F on Mac) that lets you find every place a word appears in your file. Use it to search for the function name and make sure it is spelled the same way every time.
Import or Load the Library Before You Use It
If the function comes from a library or package — something like NumPy in Python, ggplot2 in R, or jQuery in JavaScript — you have to tell your code to load that library before you try to use any of its functions. If you skip this step, your code will not know the function exists.
In Python, this means writing import numpy or from numpy import array at the top of your file, before any line that uses a NumPy function. In R, you write library(ggplot2) before you use ggplot2 functions. In JavaScript, you include a <script> tag that points to the jQuery file before you write code that uses jQuery.
If the library is not installed on your computer at all, you will need to install it first. In Python, this usually means running pip install numpy in your terminal. In R, you run install.packages("ggplot2") in the R console. Check the library's official website for the exact installation command for your system.
Make Sure the Function Is Defined Before You Call It
In many programming languages, you have to define a function before you use it. If you write code that calls a function on line 5, but the function is not defined until line 20, you will get the "could not find function" error when you run line 5.
Look at your code from top to bottom. Find the line where the error happens, then search above that line for where the function is defined. If the definition comes after the error line, move the definition to the top of your file, or move the code that calls the function to below the definition. Some languages like JavaScript and Python have different rules about this — check your language's documentation if you are not sure.
Read the Error Message for the Exact Problem
When your code fails, it prints an error message. This message usually tells you the name of the function it could not find and sometimes the line number where it tried to use it. Read this message carefully — it is your first clue.
If the error says something like "could not find function 'myFuntion'" (note the misspelling), you know the problem is a typo. If it says the function name is correct but you have not imported anything, the problem is a missing import. If the error message mentions a line number, go to that line and look at what you wrote there.
Copy the exact error message into a search engine or ask in a programming forum if you cannot figure out what it means. Other people have usually seen the same error before, and their solutions might work for you.
Check That the Function Is in Scope
Some functions are defined inside other functions or inside classes, which means they are not visible everywhere in your code. If you try to use a function from outside the place where it was defined, you will get the "could not find function" error even though the function exists.
Look at where the function is defined. If it is inside another function or class, you may need to call it differently — for example, by using the class name first, like myClass.myFunction() instead of just myFunction(). If the function is truly private and you are not supposed to use it from outside, you may need to use a different function that is public instead.
Verify the Library or Package Is Actually Installed
Sometimes you write the import statement correctly, but the library is not actually installed on your computer. Your code will still say it could not find the function because it could not find the library that contains it.
Try to import the library by itself and see what error you get. In Python, open a terminal and type python -c "import numpy". If the library is not installed, you will get an error message. If you get no error, the library is installed and the problem is something else. Install the library using the command for your system (usually pip install for Python, install.packages() for R, or a package manager for other languages).
Frequently Asked Questions
Why does the error say the function does not exist when I can see it in my code?
The function might be defined after the line where you are trying to use it, or it might be inside another function or class where your code cannot see it. Check the order of your code from top to bottom, and make sure the function is defined before the line that calls it. If the function is inside another function, you may need to call it using a different syntax.
I imported the library but still get the error. What do I do?
Check the spelling of the import statement and the function name. Make sure you are using the exact name from the library's documentation. Try uninstalling and reinstalling the library, because sometimes the installation gets corrupted. If the problem continues, search for the library name plus "could not find function" to see if other people have had the same issue.
Does the error message tell me which line has the problem?
Usually yes. Most programming languages print the line number where the error happened. Look for a number in the error message, then go to that line in your code and check what you wrote there. The actual problem might be on that line or a few lines before it.
What if I am using a function from someone else's code?
Make sure you have the right version of their code and that you have imported it correctly. Check their documentation or README file for the exact import statement and function name. If you are using code from GitHub or another source, make sure you downloaded the whole project, not just one file.