Solved

Exit the function

Posted on 2003-11-13
7
286 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
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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
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.

747 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