Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to exit from a function ???

Posted on 2004-04-19
15
Medium Priority
?
339 Views
Last Modified: 2010-04-01
I want to exit from a function, but not from all the program, only from the function.

My function is of type void, therefore I can not return a value. If my function was of type int, then I could jst return 1....

How do I exit from this function:

void test()
{
/* code containing an infinite loop. I want to exot the function, but not the whole program. How do I do it */
}
0
Comment
Question by:probine
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
15 Comments
 
LVL 17

Accepted Solution

by:
mokule earned 150 total points
ID: 10857922
return;
0
 
LVL 13

Expert Comment

by:SteH
ID: 10857925
But you place the statement
return;
Without a value you return from the function at that very place.

HTH
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 10857947
infinite loop ?
A better solution would be to make a condition to exit the loop !

Example:

bool stay = true;

while (stay)
{

// loop code here
  if (condition)
    stay = false;
 
}
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 12

Expert Comment

by:stefan73
ID: 10858307
Hi Mafalda,
> A better solution would be to make a condition to exit the loop !

If this really makes more sense depends if you need "cleanup" code before returning.

When you can plainly return without problems, do so. Then you also don't have to worry about how to exit from nested loops.

Cheers,
Stefan
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 10858406
stefan73,
I don't want to start a discussion about such minor issue.
In my opinion a return in this case behaves like a goto statement and I always didn't like programming this way ;o)
When people are used to use return they usually start abusing it.
If I can help it I design the function not to exit in this manner.

Cheers,
Mafalda
0
 
LVL 4

Expert Comment

by:caner_elci
ID: 10858577
Mafalda,

Well, what are you going to do if you are doing your job in a nested loop? always, use boolean variables to exit them? at least use "break" man! return is not goto.. We don't like "goto" because it's a unconditional jump and makes things unclear..

By the way, some information: return is compiled as ret instruction, not jmp..

Cheers,

Caner
0
 
LVL 3

Expert Comment

by:tosch
ID: 10859128
Hehe what a funny discussion...

> In my opinion a return in this case behaves like a goto statement and I always didn't like programming this way ;o)
> When people are used to use return they usually start abusing it.

This is cool - sounds like the seventies... What about "throw" ?!

There ARE algorithms which are implemented much nicer when you terminate a loop in the middle.
There ARE cases (for example in recursion)  where an "if (abort) return;" greatly increases the readability of a source code (if you have to ident more than 4 tabs you are definitely doing something wrong).

And nearly every GUI application uses an endless loop for message processing...  

Anyway, in a loop I would say "break" is the more general form of termination - especially if there´s any cleanup to be done.

But we are talking  C++ here - return doesn´t hurt -  cleanup is done automatically for variables of aggregate type and can be done with proxy classes for non aggregates (smart pointers).

Even harder - if you decide to do plain old C - you most likely do this to write low level code.
"goto" as the direct absrtaction of "jmp" in assembler is perfectly legal there.

I agree on that beginners better use "if" instead of "goto", though.  

Keep on preaching ;+)....
0
 
LVL 6

Expert Comment

by:Mafalda
ID: 10859511
I didn't want to open a discussion ;o)
One small remark though  . ..  break and continue are part of the loop syntax:

break -  Terminates the current loop, or if in conjunction with a label, terminates the associated statement
continue - Stops the current iteration of a loop, and starts a new iteration.

return on the other hand is not, it should be used for functions:

return - Exits from the current function and returns a value from that function.

I ofcourse use break and continue ... there is nothing wrong with it as it ends the loop and continue to the next statement unlike return which aborts the whole function

The preacher (?!?!) Mafalda ;o)
0
 
LVL 9

Expert Comment

by:_ys_
ID: 10862358
My two cents worth:

'goto' doesn't perform a stack unwind.
0
 
LVL 14

Expert Comment

by:wayside
ID: 10862505
> By the way, some information: return is compiled as ret instruction, not jmp..

That depends; usually if you have more than one return in a function, all but one is a jump to a block that cleans things up such as deleting local objects, and then does a ret instruction.
0
 
LVL 3

Expert Comment

by:tosch
ID: 10864304
> return - Exits from the current function and returns a value from that function.

Still there are cases where this is exactly the behaviour required.

Probably it doesn't matter with a real smart optimizing compiler but this:

some_loop
{
   do_something
   if (we_are_done_here) return;
   do_something_more
}
some_other_loop
{
   do_even_more
}

is

a) more efficient
b) better readable

than this:

some_loop
{
   do_something
   if (we_are_done_here) break;
   do_something_more
}
if (! we_are_done_here)
{
  some_other_loop
  {
     do_even_more
  }
}

// think about we_are_done being changed by do_something AND do_something_more ;+)...




In plain old C goto is pretty useful for cleanup on errors (in C++ there are more elegant solutions - but this is still a simple and efficient way to go):
Using if here will ident your code to the right margin if you need a lot of dynamically allocated structures.
And hey: that models exaclty what I want the compiler to generate - conditional jumps into the cleanup section at a specific point. There's absolutely nothing wrong with it (or have I written to much assembler ;?).

{
 some_struct1* s1 = (some_struct1*) malloc(sizeof(some_struct1));
 if (allocation_failed(s1)) goto_cu_1;
 if (failed(do_something_on_some_struct1(s1)) goto _cu_1;
 some_struct2* s2 = (some_struct2*) malloc(sizeof(some_struct2));
 if (allocation_failed(s2)) goto_cu_2;
 if (failed(do_something_on_some_struct2(s2)) goto _cu_2;
 [...]
_cu_s2:
  free(s2);
_cu_s1:
  free(s1);
}


All of C/C++ language constructs have their place - it's narrowminded to condamn them because of not being popular in classrooms.
I admit that they might be confusing when learning the language, though.
0
 

Expert Comment

by:Dogofwars
ID: 10935716
void Test()
{
  for(;;)
  {
    if(functionA == functionB) return;
   }
}
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question