Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Exit the function

Posted on 2003-11-13
7
Medium Priority
?
298 Views
Last Modified: 2010-04-01
How to exit the function if it does not pass if statment? In delphi i always use Exit; function however, in C++ it does not work. Any ideas how should i do that? And example of such function:

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  for (int i = 0; i < ParamCount(); i++)
  {
    if (LowerCase(ParamStr(i)) == "-options")
    {
      Panel1->Visible = True;
      exit(0); // it should exit the program here
    }
  }

// MORE CODE HERE

}
0
Comment
Question by:kabix
  • 4
  • 2
6 Comments
 
LVL 9

Accepted Solution

by:
tinchos earned 80 total points
ID: 9744372
Hi kabix,

In order to  exit the function you can do several things.......

1) if you use the exit() function, you will exit the program, not just the function.

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  for (int i = 0; i < ParamCount(); i++)
  {
    if (LowerCase(ParamStr(i)) == "-options")
    {
      Panel1->Visible = True;
      exit(0); // it should exit the program here
    }
  }

// MORE CODE HERE

}

2) if you use the return; you will exit just the function
Note: I said that it is return; as the function is void.
I'm not sure if it is a standard convention, but in MSVC6 it is allowed to use return; in void functions

void __fastcall TForm1::FormCreate(TObject *Sender)
{
  for (int i = 0; i < ParamCount(); i++)
  {
    if (LowerCase(ParamStr(i)) == "-options")
    {
      Panel1->Visible = True;
      return; // it should exit the program here
    }
  }

// MORE CODE HERE

}

Hope this helps

Tincho
0
 
LVL 15

Expert Comment

by:efn
ID: 9744674
Just to supplement Tincho's comment:

Tincho> I'm not sure if it is a standard convention, but in MSVC6 it is allowed to use return; in void functions

It is standard and should work with any compiler.

"return" is the equivalent of the Delphi "Exit".  Both exit the current function, not the program.  If the function is declared to return a value, that value must be specified in a return statement.

--efn
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9744690
Ok

thanks efn.......

Glad to see your comment.........
now I know for sure that it is standard, I thought so, but wasn't sure

:D

Tincho

0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 15

Expert Comment

by:efn
ID: 9744731
No problem, buddy!  You answered it, I just filled in a small gap.  And by the way, Tincho, thanks for your cleanup efforts.  I appreciate the opportunity to get credit for my dusty old forgotten comments, and I'm sure others do too.  kabix, my apologies for going off-topic.

--efn
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9744746
No problem efn......

I saw a couple of yours in these last few days

:D
0
 
LVL 9

Expert Comment

by:tinchos
ID: 10249169
No comment has been added lately, so it's time to clean up this TA.
I will leave the following recommendation for this question in the Cleanup topic area:

Accept: tinchos {http:#9744372}

Please leave any comments here within the next seven days.
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
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 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.
Suggested Courses

824 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