Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 347
  • Last Modified:

How to exit from a function ???

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
probine
Asked:
probine
1 Solution
 
mokuleCommented:
return;
0
 
SteHCommented:
But you place the statement
return;
Without a value you return from the function at that very place.

HTH
0
 
MafaldaCommented:
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
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
stefan73Commented:
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
 
MafaldaCommented:
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
 
caner_elciCommented:
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
 
toschCommented:
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
 
MafaldaCommented:
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
 
_ys_Commented:
My two cents worth:

'goto' doesn't perform a stack unwind.
0
 
waysideCommented:
> 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
 
toschCommented:
> 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
 
DogofwarsCommented:
void Test()
{
  for(;;)
  {
    if(functionA == functionB) return;
   }
}
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now