473,497 Members | 2,190 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

how can i set jump from a function to the main function??

2 New Member
Expand|Select|Wrap|Line Numbers
  1. void main()
  2. {
  3. ________            //statements....
  4. ________
  5. step:
  6. ________
  7. ________
  8. ________           //statements......
  9. }
  10. int fun()
  11. {
  12. goto step;         //Have to jump to the label step in main function.
  13. }                      //IS IT POSSIBLE, IF NOT TELL ME HOW CAN I DO IT.
Nov 18 '09 #1
6 5984
Banfa
9,065 Recognized Expert Moderator Expert
Under no circumstances should you ever use the goto statement in c (or C++).

Why don't you just return a value from fun that indicates what main should do next and use a if statement in main to make the execution path decision.
Nov 18 '09 #2
RRick
463 Recognized Expert Contributor
Gotos have function scope which means you can not jump between functions. Be thankful that you can't because you could cause all kinds of problems if you did. Memory leaks and a corrupted stack are just the beginnings of the troubles you would encounter.

As Banfa said, there is no reason to use gotos anymore. There are lots of alternatives that don't cause problems. The trick is to use the proper construct for the proper problem.


If you want to ignore all of this good information, there are setjmp and longjmp system routines in unix. They only jump backwards, but are a great way to programmatically shoot yourself in the foot, hand, or head.
Nov 18 '09 #3
donbock
2,426 Recognized Expert Top Contributor
Structured programming, a revolutionary innovation in programming, was popularized in Edsger Dijkstra's seminal 1967 article "Goto statement considered harmful". Younger programmers aren't aware of the truly sorry state of programming when the only method available to alter program flow was the unconstrained goto statement. All they see is the reflexive antipathy to 'goto' from those of us old enough to remember the spaghetti code of yesteryear.

Unlike the earlier respondents, I feel there is an valid use for the goto statement. I have found in rare instances that using 'goto' is helpful to trap error cases into a common clean-up snippet immediately before returning an error. In most cases, you can avoid the goto by either rearranging your logic or by refactoring a large function into several smaller functions.

As a C programmer, I'm intrigued by the C++ exception handling mechanism. I'm not familiar enough with it to determine if it is the magic bullet that eliminates this last valid use of 'goto' or if it is itself a kind of 'goto' that should be avoided.
Nov 19 '09 #4
Tassos Souris
152 New Member
RRick setjmp and longjmp are in the Standard C Library in <setjmp.h>.

The only place i have seen calls to setjmp and longjmp, is from signal handlers where some clean-up work must be done. setjmp and longjmp effectively clear the stack but they are not "signal-safe" and they do not clear memory allocated on the heap... in other words, setjmp and longjmp and all other forms of "i am here and now i will go there" except by means of a function call is to be forbidden!!

Really, we should post a list here of things that a programmer is forbidden to do, things that he/she is allowed to do only once, only twice and such stuff... :)
Nov 20 '09 #5
RRick
463 Recognized Expert Contributor
There are other accepted forms of gotos in C.
  • A break statement in a loop or switch
  • Continue or next in a loop
  • A return in the middle of a subroutine
  • Even the normal If-then-else statements and loops are forms of gotos
These types of gotos are acceptable because they are controlled and do not cause adverse or unpleasant side affects in the code. Once you start using gotos, you loose all of that.

When Dijkstra came out with Structured Programming, people didn't believe you could program without gotos and just use the 3 constructs he defined (i.e. if-then-else, while loop, and simple statement). People actually dared Dijkstra to solve some problems with out gotos. He solve them, and solved them elegantly, and the rest is history.
Nov 20 '09 #6
Banfa
9,065 Recognized Expert Moderator Expert
@RRick
These 3 are certainly legal forms of altering execution path, however I have seen plenty of coding standards that discouraged or even outlawed them mainly in order to increase the maintainability of the code.
Nov 20 '09 #7

Sign in to post your reply or Sign up for a free account.

Similar topics

4
2924
by: wongjoekmeu | last post by:
Hello All, >From a book where I have learned C++ it says that each application must have at least one function which is the main() function. Now I have the source code of a C++ program from...
89
6381
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
8
3567
by: Rody Reulen | last post by:
I made an console application with Visual Basic .net. Visual Basic will automatically create the sub procedure main for you. I try to convert the main procedure to a function. During the...
1
2173
by: wayne.denier | last post by:
Ran across this well documented, nice looking library for C++ gaming called Amaltheia ( http://home.gna.org/amaltheia/ ), but they don't have a community :P. I'm not sure if anybody has used it...
19
28540
by: beliavsky | last post by:
I think I read a suggestion somewhere to wrap the code where a Python script starts in a main() function, so one has def main(): print "hi" main() instead of
5
1816
by: PencoOdStip | last post by:
Do i have to have a main function? If i don't have a function called main,i get erors.Why do i have to have a function called main?
1
1810
by: shiniskumar | last post by:
i go tto perform some validation while ck=liking a button without refreshing the value.. i used ajax.My code is as follows...on button click i call the function validateGridAdd(); Inside tat...
28
6005
by: ravi | last post by:
Hello everybody, I am writing a small application which does some work before the user main function starts execution. I am trying to #define the main function. But the problem is that,
11
3532
by: aarklon | last post by:
Hi all, I have heard many discussions among my colleagues that main is a user defined function or not. arguments in favour:- 1) if it is built in function it must be defined in some header...
0
7120
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7196
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7373
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
4897
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3088
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1405
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
649
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
286
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.