CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Functions and error message

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest
Guest







Functions and error message
PostPosted: Mon Mar 07, 2005 11:09 am     Reply with quote

Code:

#use delay(clock=4000000)
#fuses NOWDT,HS,PUT,NOPROTECT,BROWNOUT,NOCPD,NOWRT,NOLVP

int state;
int start;
int number;

void start_function()
{
   if ( state == start)
   {
      new_function();
      return;
   
   }
 
}

void new_function()
{
   output_high(PIN_B3);
   number++;
}


main()
{

   state = start;
   while(1)
   {
      start_function();
   }
}


Why do i get compiler error message:
Undefined identifier new function

Compiler version is 3.150
Guest








PostPosted: Mon Mar 07, 2005 11:28 am     Reply with quote

Because you are using the function before it is declared. Just swap the order of the declarations:

Code:

#use delay(clock=4000000)
#fuses NOWDT,HS,PUT,NOPROTECT,BROWNOUT,NOCPD,NOWRT,NOLVP

int state;
int start;
int number;

void new_function()
{
   output_high(PIN_B3);
   number++;
}

void start_function()
{
   if ( state == start)
   {
      new_function();
      return;
   }
 
}

main()
{
   state = start;
   while(1)
   {
      start_function();
   }
}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Mar 07, 2005 11:31 am     Reply with quote

Quote:
Why do i get compiler error message:
Undefined identifier new function
In your start_function() you are calling new_function() before it is declared. A C-compiler must know about the parameters used and returned by a function before it can be used in another function.
Think about the C-compiler as starting to read at the beginning of your program and translating it to assembly language 'on the fly'. When the compiler comes across a function name it doesn't know it shows the given error message. Some other computer languages are 'smarter' in this respect where the compiler first scans the whole program to learn all function names in the program.

The solution is to tell the compiler all function names at the start of your program, this is called 'declaration' and consists of the function name and all parameters and the return type. A function 'definition' is the implementation of the function.

Code:
#use delay(clock=4000000)
#fuses NOWDT,HS,PUT,NOPROTECT,BROWNOUT,NOCPD,NOWRT,NOLVP

int state;
int start;
int number;

////////////////////////////////////////////////////////
// Function declarations
////////////////////////////////////////////////////////
void start_function();
void new_function();
void main();

////////////////////////////////////////////////////////
// Implementation
////////////////////////////////////////////////////////

void start_function()
{
   if ( state == start)
   {
      new_function();
      return;
   
   }
 
}

void new_function()
{
   output_high(PIN_B3);
   number++;
}


main()
{

   state = start;
   while(1)
   {
      start_function();
   }
}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group