call to undefined function in function main()

c
15,056

Solution 1

You need to define (or at least declare) any function before use. You can do that by including a header that includes a declaration (or prototype) for the function, or the declaration or definition can be contained directly in the source file at hand. For example:

#include <stdio.h>

void doit() { 
    // call function declared in <stdio.h>
    printf("Function called from main");
}

int main() {
    // call function defined above.
    doit();
    return 0;
}

Solution 2

make sure you create a prototype for your functions if these are global function then prototype them in a header file then include this file in your C file where you would like to use these functions.

hope this helps

#include<stdio.h> 

// this is the prototype
void display ( int m );

void main( ) 
{ 
  int i ; 
  int marks[ ] = { 1, 2, 3, 4, 5, 6, 7 } ; 

  for ( i = 0 ; i <= 6 ; i++ ) display ( marks[i] ) ; 
} 

void display ( int m ) 
{ 
  printf ( "%d ", m ) ; 
}

Solution 3

Just add the below code above the main()

     void display(int);
     main() {
     // code
     }

Solution 4

Post your code for us to help you.

You need to make sure your functions are defined before you implement your main function. That is there defintion is known before main.

Here is an example:

#include<stdio.h>

void someFunc()
{
 //define this function
 //notice it is before the main function
}

int main(void)
{
 someFunc();
 return 0;
}

The other way you could do it is to define the protocol or signature of the function before main and then you should be able to define this function after main.

If this involves a function that you may want to be included in your code, maybe from some 3rd party or library, you will need ot include the header file at the top of your file (just like the stdio.h header I've entered in my example).

**Your version edited**

Change your code to this:

#include<stdio.h> 

void display(int m)
{
  printf ( "%d ", m ) ; 
}

int main(void) 
{ 
  int i ; 
  int marks[ ] = { 1, 2, 3, 4, 5, 6, 7 } ; 

  for ( i = 0 ; i <= 6 ; i++ ) display ( marks[i] ) ; 
  return 0;
} 
Share:
15,056
aja
Author by

aja

Updated on November 19, 2022

Comments

  • aja
    aja over 1 year

    whenever i use a function which is called in main function, it gives this error: Call to undefined function in function main() i am using turbo c++ compiler version 4.5 and windows vista ultimate service pack 2 Can you tell which header file or something else is to be used. I am beginner in C language.

    An example which produces this error:

    #include<stdio.h> 
    
    main( ) 
    { 
      int i ; 
      int marks[ ] = { 1, 2, 3, 4, 5, 6, 7 } ; 
    
      for ( i = 0 ; i <= 6 ; i++ ) display ( marks[i] ) ; 
    } 
    
    display ( int m ) 
    { 
      printf ( "%d ", m ) ; 
    }
    
    • user1066101
      user1066101 over 13 years
      Please post the smallest piece of code that shows your problem.
    • Nick Meyer
      Nick Meyer over 13 years
      Do you get this error when you compile the program, link it, or run it?
    • rubber boots
      rubber boots over 13 years
      Generally, you need the header file (.h) where your function is declared. What functions do you use?
    • AnT stands with Russia
      AnT stands with Russia over 13 years
      If you are using C++ compiler, why is your question tagged C?
  • aja
    aja over 13 years
    i tried to compile this : and it gave the error Call to undefined function in function main() #include<stdio.h> main( ) { int i ; int marks[ ] = { 1, 2, 3, 4, 5, 6, 7 } ; for ( i = 0 ; i <= 6 ; i++ ) display ( marks[i] ) ; } display ( int m ) { printf ( "%d ", m ) ; }
  • Nick Meyer
    Nick Meyer over 13 years
    @aja the compiler needs to have seen either a prototype for or a definition of display() before you use it for the first time. Try moving its definition above main(). I would also edit your question and include the code there.
  • Colin DeClue
    Colin DeClue over 13 years
    You can also just do void display(int m); at the beginning of the file (A prototype, as Jerry mentioned.), and then do the actual implementation later, if you prefer.