View previous topic :: View next topic |
Author |
Message |
diegostreetbob
Joined: 05 Sep 2009 Posts: 11
|
Help with main() |
Posted: Fri Nov 05, 2010 3:13 pm |
|
|
Hello,
In the first time thanks for your help....
I have a simple question:
main()
{
code....
.......
..........
function1();
}
function1()
{
code....
..............
...........
funtion2();
}
function2()
{
code........
............
.......
How I can go to the main() from here?
} |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Nov 05, 2010 4:31 pm |
|
|
When function2 ends, it returns to the place where it was called in
function1. That place is at the end of function1. So it returns from
there and goes back to main(). It's the normal operation of C.
Functions return to the caller when they are done.
If you want to call the sequence of functions again and again, then
put a while() loop in main. Example:
Code: |
void main()
{
while(1)
{
.
.
.
function1();
}
} |
CCS tutorial:
http://www.swarthmore.edu/NatSci/echeeve1/Ref/C%20for%20PIC/C_Intro.html |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Fri Nov 05, 2010 10:01 pm |
|
|
C does have a goto function, though it is VERY rarely used. Maybe in case of a catastrophic failure you could have a label at the beginning of main() and use goto label to get there.
Goto exists, but is almost never used in C. If you can not find the syntax on your own you should not be using goto. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
arunb
Joined: 08 Sep 2003 Posts: 492 Location: India
|
RE: |
Posted: Fri Nov 05, 2010 10:16 pm |
|
|
I agree a goto is never a good idea....it shows desperation... |
|
|
asmallri
Joined: 12 Aug 2004 Posts: 1635 Location: Perth, Australia
|
Re: RE: |
Posted: Fri Nov 05, 2010 11:06 pm |
|
|
arunb wrote: | I agree a goto is never a good idea....it shows desperation... |
A GOTO can be a good idea when used correctly. For example inside a function there may be several exit points, typically when parsing return codes from functions. These exit points may require common clean up code. In this case a GOTO local to the function is a good way of handling this scenario. _________________ Regards, Andrew
http://www.brushelectronics.com/software
Home of Ethernet, SD card and Encrypted Serial Bootloaders for PICs!! |
|
|
Douglas Kennedy
Joined: 07 Sep 2003 Posts: 755 Location: Florida
|
|
Posted: Sat Nov 06, 2010 8:08 am |
|
|
There is absolutely nothing wrong with a goto. Sure a goto can cause issues if used incorrectly but that is true about almost every line of code except a comment. Some coders see the abstinence from goto's as a kind of nirvana in which their code takes on a pseudo spiritual aspect and that only the unpure would use foul language like goto's. Goto's are not sinful. Beautiful code is only in the eyes of the beholder.
Code makes electrons take different paths through a silicon chip. There's nothing really special about coding,coders or languages. If the electrons take the correct path things work.
The rare coder that understands electrons (quantum mechanics) would be the goto guy ( pun intended). |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Sat Nov 06, 2010 12:56 pm |
|
|
Douglas, I enjoyed your comments and let me say you that it is like a breeze of fresh air in this cold and technological forum.
I could say that you are absolutely right from the "quantum mechanic" (I enjoy that phrase) point of you, but also I should
recognize that I'm more conservative and by no way I ever used a GOTO in my codes, I admit that. (no matter if you are an
"unpure"). Even if it was difficult to find out another resource, I always tried to avoid it. Being the C language so rich in
functions and so flexible in its resources, there is no GOTO that can't be replaced by another function.
Let me say also that a well written code is very comfortable to read and understand, and by no way this is a "pseudo spiritual"
(I like it too) argument.
It is not my intention to criticism you nor to convince somebody, it is just my opinion. After all, every one is the result of his
own experience, hence I send you my best regards from Nirvanaland!
Humberto |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Sat Nov 06, 2010 1:42 pm |
|
|
It is also important to point out that while there _may_ be a very few places where 'goto' may make code work better, this is _not_ one of them. Using goto here, if the code is genuinely built as a subroutine, _will_ result in the stack being imbalanced, and the code eventually crashing....
Best Wishes |
|
|
John P
Joined: 17 Sep 2003 Posts: 331
|
|
Posted: Sat Nov 06, 2010 1:58 pm |
|
|
If the original poster had said "How I can GET to the main() from here?" instead of "How I can go to the main() from here?" this discussion would never have taken place. |
|
|
diegostreetbob
Joined: 05 Sep 2009 Posts: 11
|
|
Posted: Sun Nov 07, 2010 2:30 pm |
|
|
It's ok, I need to know how I can return from here to main(). This is the question but the responses about goto are good for me, but don't ask the original question.
Thanks |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Sun Nov 07, 2010 4:12 pm |
|
|
You guys are making me flash back to when I was trying to learn how programs worked by reading other peoples interpretive BASIC source.
I think that could make anyone phobic about using GOTO's.
Remember the term, Spaghetti Code? C is more like Ravioli - everything is encapsulated in it's own little function.
Speed and memory (time & space, hehe) are not the constraints they used to be. (PIC chips, like the universe, keep expanding). I would say that most programmers and most programs can get by without having to resort to GOTO's or even #asm's. If you can do it in a way you're proud of, go for it. But I wouldn't recommend it for anyone who is just getting started in this stuff.
And to recap what PCMProgrammer said,
Make your MAIN loop an infinite loop, as he showed you.
Then no matter how many functions call other functions, you'll always, eventually pop back to the surface and back into the infinite MAIN loop.
( I didn't mean pop THAT way. Don't start, guys. Leave it alone. Slowly walk away...) |
|
|
Wayne_
Joined: 10 Oct 2007 Posts: 681
|
|
Posted: Mon Nov 08, 2010 3:21 am |
|
|
This is a good example of why goto is bad;)
Several posters have suggested the use of goto in this code, which would be catastrophic in most cases. It is also teaching the poster how not to program in C. As any C coder should know that functions return to where they were called from (generally) which has been pointed out by 1 person.
Most coders who write very large programs also work in a team or have to modify code written by someone else. Trying to decypher code riddles with goto's is a pain and it is easy to miss interpret the code.
I personally have NEVER had to use them and I write code in many different languages for a living, both embedded and Application programming.
The non use of goto's is not about functionality but about readability and reducing possible coding errors, this is why many people avoid using them. I personally do not like to see goto's in code and wonder why it was left in C, but that is just me:)
This is of cause my opinion, and yes, when the code is compiled/assembled goto's will be used in the final code. |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Nov 08, 2010 6:53 am |
|
|
diegostreetbob wrote: | It's ok, I need to know how I can return from here to main(). This is the question but the responses about goto are good for me, but don't ask the original question.
Thanks |
All of your functions except main() should end with a return(). That return() should send execution back to the calling function. Eventually it should bring you back to main().
The argument of return() is the value of the function, so:
Code: |
int funct1()
{
x = 2 * 3;
return(x);
}
main()
{
y = funct1();
}
|
leaves y equal to 6.
Note that a function may have several return() statements, but only one will be used in each execution. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
pmuldoon
Joined: 26 Sep 2003 Posts: 218 Location: Northern Indiana
|
|
Posted: Mon Nov 08, 2010 6:59 am |
|
|
I think part of his confusion may have been that he was missing the infinite loop. |
|
|
|