The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> C Programming
|
How to return to main()?
Discuss How to return to main()? in the C Programming forum on Dev Shed. How to return to main()? C programming forum discussing all C derivatives, including C#, C++, Object-C, and even plain old vanilla C. These languages are low level languages, and used on projects such as device drivers, compilers, and even whole computer operating systems.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

December 20th, 2012, 06:28 AM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 112
Time spent in forums: 1 Day 4 h 12 m 48 sec
Reputation Power: 1
|
|
|
How to return to main()?
let's say the main() calls for function1, and function1 calls for function 2.
is there a way to return (not necessarily with a value) directly to main?
for example:
Code:
int main()
{
code...
code...
func1();
code..
}
func1()
{
code...
func2();
}
func2()
{
code...
return; //returning to main()
}
is there a command that takes the program back to main?
thanks in advanced!
|

December 20th, 2012, 07:08 AM
|
 |
Contributed User
|
|
|
|
|
Perhaps it would be better for main() to call func2() directly.
There are the setjmp() and longjmp() functions, but you should really work on your program structure before resorting to using these functions.
Your example shows func1() returning anyway after calling func2(), so really there is nothing else to do.
Even if func1() did do something (say based on the success/fail of func2()), this is easily achieved (and the best approach) by having func2() return a result, and for func1() to test that result.
|

December 20th, 2012, 09:17 AM
|
 |
Contributing User
|
|
Join Date: Jan 2003
Location: USA
|
|
|
Or, trivially, you could have func1 return immediately after calling func2.
Each function call pushes that function's block onto the stack and as each function returns it pops its block off of the stack. Attempting to bypass normal calls and returns means that you also need to clean up the stack on your own. Why wish for such a mess?
|

December 20th, 2012, 09:32 AM
|
|
Contributing User
|
|
Join Date: Nov 2012
Posts: 112
Time spent in forums: 1 Day 4 h 12 m 48 sec
Reputation Power: 1
|
|
thanks guys.
Even if func1() did do something (say based on the success/fail of func2()), this is easily achieved (and the best approach) by having func2() return a result, and for func1() to test that result.
OK, yeah, that makes much more sense.
thanks for the tip. 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|