How To Fix Implicit Declaration of function Error?

In the previous post, we discussed functions in detail and here in this post, we are going to discuss one error that we often encounter while working with functions which is the "implicit declaration of function". 


The implicit declaration function error is encountered when you use (or call) the function before declaring or defining it. It is because the compiler has no information about that function at the time of calling it so the compiler implicitly assumes something about the function.

Example:

//C++ Implicit Declaration of function
#include<iostream>
using namespace std;

int main(){
    
    int m = 6, n = 8;
    //function call
    int greater = find(m, n);
    cout<<"Greater number is: "<<greater;

    return 0;
}
//function definition
int find(int x, int y){

    if(x < y){
        return y;
    }
    else{
        return x;
    }
}
Output:
../src/main.c:48:9: error: implicit declaration of function 'find' [-Werror=implicit-function-declaration]
         find();
Here the find() function is not declared before calling inside the main function and the definition of the function is written after the main function so when it is called inside the main function the compiler has no information about the find() function and gives us an error message. (alert-warning)

How Execution takes place in C++?

Before understanding the solution for the above error it is important to understand how execution takes place in C++ Programming. 

If you are familiar with C++ programming you must have heard that execution starts from the global function main() but it is partially correct. The compiler starts the execution of the program from the first statement and all statements are executed one by one from top to bottom. Even if you have defined any function before or after the main then also the flow of execution will remain the same, but the statements inside the function get executed only when that function is called. 

How to resolve Implicit Declaration of Function Error?

There are two ways to resolve this error:
  • If you define the function before the main function then you only have to define it and you don't have to separately declare or define it. 
Example:  
//C++ Example program to find greater
#include<iostream>
using namespace std;

//function definition before using it
int find(int x, int y){

    if(x < y){
        return y;
    }
    else{
        return x;
    }
}
int main(){
    
    int m = 6, n = 8;
    //function call
    int greater = find(m, n);
    cout<<"Greater number is: "<<greater;

    return 0;
}
Output:
Greater number is: 8

  • If you define a function after the main function then you have to put a matching declaration of that function before the main.
Example:

//C++ Example program to find greater
#include<iostream>
using namespace std;

//function declaration before using it
int find(int, int);

int main(){
    
    int m = 6, n = 8;
    //function call
    int greater = find(m, n);
    cout<<"Greater number is: "<<greater;

    return 0;
}
//function definition after main
int find(int x, int y){

    if(x < y){
        return y;
    }
    else{
        return x;
    }
}
Output:
Greater number is: 8

Here find() function is declared before the main function at the top but it is defined after the main function so the program will execute successfully because the compiler already knows about the find function from its declaration.

⚡ Please share your valuable feedback and suggestion in the comment section below or you can send us an email on our offical email id ✉ algolesson@gmail.com. You can also support our work by buying a cup of coffee ☕ for us.

Similar Posts

No comments:

Post a Comment


CLOSE ADS
CLOSE ADS