tags:

views:

119

answers:

4

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 ) ; 
}
+1  A: 

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;
} 
JonH
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 ) ; }
aja
@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.
Nick Meyer
I edited aja's example here into his question
Tyler McHenry
+4  A: 

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;
}
Jerry Coffin
thanks it worked
aja
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.
Colin DeClue
A: 

You probably need to declare the prototype of that function before the definiton of main().

karlphillip
+2  A: 

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 ) ; 
}
jramirez