Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to Exit Function in C++?

Status
Not open for further replies.

hvytas

Programmer
Aug 10, 2005
23
LT
Hello,

I want to exit from any place of function that return somthing and I don't want to use If.. for that, any ideas?

For example:

int demo()
{
string test;
...
<something doing here and exit from function>
...
<something doing here and exit from function>
...
etc.

};

Thank's.
 
I want exit from any place of function like doing in Visual Basic (VB), for example in VB: Exit Function
 
I don't think I understand the question.

You just need to do

Code:
 return 0;

Cheers,
Dian
 
You can exit at any point in the function as described above. The return value you specify must match to your function type
i.e.
Code:
int main ()
{
  if ( IwantToReturnEarly ) return ( 1 );
  return ( 0 );
}

void main()
  if ( IwantToReturnEarly ) return;
  return;
}

bool main()
  if ( IwantToReturnEarly ) return ( false );
  return ( true );
}
note: brackets are optional.

Robert Cumming
 
So if I use return where I want and return different information do the same like do in VB Exit Function?
Becouse like in
VB for example:
Function demo() as Integer
Dim i as Integer
i=1
If i=0 then demo=0:Exit Function
demo=i
End Function

In C++:
int demo()
{
int i;
i=1;
if (i==0) return 0;
return i;
}

Is this ok? This meen, then Exiting Function not going to other return and etc...
 
Things work a little different in C++ than in VB.

If I'm correct, in VB you just assign the return value to the function itself in any moment and it's not actually returned until you perform an ExitFunction.

In C++, the functions ends in the moment you perform the return sentence, returning the value specified. If you feel more confortable, you can define your C++ function this way:

Code:
int demo()
{
  int returnValue
  int i;
  i=1;
  if (i==0) returnValue=0;
  else returnValue = i;

  return returnValue;
}

But this just makes the same thing as the function you wrote above.

You can take a look at
Cheers,
Dian
 
shetlandbob: Note that
Code:
void main()
{ 
  return;
}
Is not really valid C++ since there is no such thing as void main().

Even if your compiler accepts "void main()" avoid it, or risk being considered ignorant by C and C++ programmers.
See

/Per
[sub]
www.perfnurt.se[/sub]
 
Thank's for all,

Other way is only possible to use in C++ goto like in VB Goto, so other ways I don't know.

If I use this example that I write before,
in C++:
Code:
int demo()
{
  int i;
  int iTmp;
  i=1;
  if (i==0) 
  {
     iTmp = 0;
     goto Exit;
  }
  iTmp = i;
  ...<more code here>...
Exit:
  return iTmp;
}

If we use big function where is requare to exit from any place so this is more better then use if style.
 
You should use C++ with the mindset that there is no such thing as goto. It clutters the code and makes debugging harder.

I'd recommend this approach:
Code:
int demo()
{
  int i;
  int iTmp;
  i=1;
  if (i==0) 
  {
     iTmp = 0;
  }
  else
  {
    iTmp = i;
    ...<more code here>...
  }
  return iTmp;
}




/Per
[sub]
www.perfnurt.se[/sub]
 
Let's remember a wonderful PL/I SELECT stmt:
Code:
select;
  when (condition1) do; ... end;
  when (condition2) do; ... end;
  ...
  othewise do; ... end;
end;
The PL/I was one of C ideological ancestors (see K&R) but SELECT stmt was standardized later (what's a pity;)...

How funny to discuss such global program cases...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top