Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Exit the function

Posted on 2003-11-13
7
Medium Priority
?
295 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 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
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!

 
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

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…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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

670 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