Solved

How to exit from a function ???

Posted on 2004-04-19
15
301 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
15 Comments
 
LVL 17

Accepted Solution

by:
mokule earned 50 total points
Comment Utility
return;
0
 
LVL 13

Expert Comment

by:SteH
Comment Utility
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
Comment Utility
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
 
LVL 12

Expert Comment

by:stefan73
Comment Utility
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
Comment Utility
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
Comment Utility
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 3

Expert Comment

by:tosch
Comment Utility
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
Comment Utility
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_
Comment Utility
My two cents worth:

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

Expert Comment

by:wayside
Comment Utility
> 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
Comment Utility
> 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
Comment Utility
void Test()
{
  for(;;)
  {
    if(functionA == functionB) return;
   }
}
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.

771 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now