Solved

Exit the function

Posted on 2003-11-13
7
293 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
[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
  • 4
  • 2
7 Comments
 
LVL 9

Accepted Solution

by:
tinchos earned 20 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
Independent Software Vendors: 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 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

Industry Leaders: 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!

Question has a verified solution.

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

Suggested Solutions

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
  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 …
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 learn how to clear a vector as well as how to detect empty vectors in C++.

752 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