Solved

Exit the function

Posted on 2003-11-13
7
291 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
sorting efficency of sorting algorithm 30 122
gdb doesn't stop on breakpoint 2 98
Why isn't object file created? 6 69
How do I get Window Title of all opened process? 4 15
This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.

807 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