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

Problem about functions

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



Joined: 06 Oct 2011
Posts: 1

View user's profile Send private message

Problem about functions
PostPosted: Thu Oct 06, 2011 6:59 pm     Reply with quote

I need very useful help. I'm doing a program with the pic 18f452 and I need to call a function from another function but I can't do it, it shows me recursion not permitted.
Code:

void manual()
{
lcd_putc('\f');
lcd_gotoxy(1,1);
printf(lcd_putc,"1.Iluminacion");
delay_ms(500);
lcd_gotoxy(1,2);
printf(lcd_putc,"2.Puertas");
delay_ms(500);

while(TRUE)
{
k=kbd_getc();
x=k-48;
if(k!=0){

switch (x){
case 1:
lcd_putc('\f');
lcd_gotoxy(1,1);
printf(lcd_putc,"*****Cuartos*****");
delay_ms(500);
lcd_gotoxy(1,2);
printf(lcd_putc,"  1 2 3 4  ");
delay_ms(500);

main();

break;

case 2:
lcd_putc('\f');
lcd_gotoxy(1,1);
printf(lcd_putc,"****Puertas****");
delay_ms(500);
lcd_gotoxy(1,2);
printf(lcd_putc,"   1   2 ");
delay_ms(500);
motores();
break;
}
}

}
}



void main()
{
lcd_init();
kbd_init();
port_b_pullups (TRUE);
lcd_putc('\f');
lcd_gotoxy(1,1);
printf(lcd_putc,"1.Manual");
delay_ms(500);
lcd_gotoxy(1,2);
printf(lcd_putc,"2.Automatico");
delay_ms(500);

while(1)
{
k=kbd_getc();
x=k-48;
if(k!=0){
if(x==1){
manual();
}
}
}


}

Please someone how teach me how to call a function, maybe I need to put declarations first.
ünloco



Joined: 02 Oct 2011
Posts: 12
Location: Tunisia

View user's profile Send private message Visit poster's website

PostPosted: Fri Oct 07, 2011 3:31 am     Reply with quote

try "return;" instead of "main();"
_________________
for(;;);
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri Oct 07, 2011 9:07 am     Reply with quote

It looks like you're trying to use function calls like a goto statement. When you call a function you will branch out of your existing place in your code and execute the function that was just called. When that function has finished it's job the program will resume from where the function was called.

In your code you enter main() and then call manual(). Inside manual() you call main() which in turn will call manual() which will call main() which will call manual() and so on and so on, forever. You can't do this. This is called 'recursion'.

Instead of trying to call main() inside of manual(), just let the switch() statement exit and manual() will reach the end of it's code and you will be returned back to where manual() was called within main(). In other words, if none of the switch() cases match the variable being evaluated then the switch() will simply exit. Make sure you also have a default: statement in your switch(). Read up on switch() statements a bit.

One other point, you have a while(true) statement inside of manual(). This will keep your code inside of manual() forever and will never return back to main(). Remember, you call a function, when that function is finished it exits and your code continues from where the function was called.

Clear as mud?

Ronald
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