|
|
View previous topic :: View next topic |
Author |
Message |
Guest Guest
|
Functions and error message |
Posted: Mon Mar 07, 2005 11:09 am |
|
|
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
|
|
Posted: Mon Mar 07, 2005 11:28 am |
|
|
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
|
|
Posted: Mon Mar 07, 2005 11:31 am |
|
|
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();
}
} |
|
|
|
|
|
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
|