Click here to Skip to main content
15,891,837 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
why we use (return0) in C++? if we Put 1 Or -1 does it work or why not?

What I have tried:

i am trying that if (return0) returned a value but why return 1 not return the value
Posted
Updated 7-Feb-19 2:08am
Comments
Maciej Los 7-Feb-19 4:28am    
Are you talking about main function?

There are two places you may be using return 0 in C++:
1) At the end of your main function
2) At some point in another function.

When your main function exits, it returns a status code to the operating system, which - if your app runs under DOS or CMD - is checked to decide if your app functioned normally. A value of 0 indicates "all ok, normal exit", any other value is normally an error code which says why your app exited at that point. U=You can return any value, but some values do have specific meanings: DOS ERROR CODES[^]

If you return zero in a "normal" function, it's probably a boolean value. Because C does not have a true or false value, it works with 0 / nonzero. 0 is "False", any other value is "True" - and because C++ grew from C it's still perfectly valid. So a return value of 0 is very often the equivelant of "return fasle", and any other value is "return true". That's why you often see code like:
C++
char myString[] = ...
char *p = myString;
while (*p)
   {
   ...
   p++;
   }
To traverse a null terminated string - it stops at the first null character '\0' because zero is "False".

[edit]:blush: wrong way round, Griff - go get caffeine [/edit]
 
Share this answer
 
v2
Comments
CPallini 7-Feb-19 5:28am    
It should be written
while (*p)
that stops at first '\0' character because zero is FALSE!!!!
0 is used to mean 'success'. At the same time 0 is like false in conditional expressions.
OriginalGriff 7-Feb-19 5:35am    
:blush:
Fixed, and I'm on my way to the kitchen ...
Maciej Los 7-Feb-19 8:41am    
Blood pressure fall?
I have it occasionally. If cafe won't help, i have to go outside the building to get short walk. :beer:
Cheers
Maciej
OriginalGriff 7-Feb-19 8:43am    
It's possible - guess I'll have to see if I can find where Herself has left the BP machine ... oh gawd, could be *anywhere* ...
It's what is known as a programming "convention" and it's been around a long, long time.

You can return void or any integer from main() in a C++ program - Main() Return Values - C# Programming Guide | Microsoft Docs[^] but the use of 0/-1 dates back to prior to that capability.

It's not just C++ programs, the same is true of SQL Server Stored Procedures for example, in particular the inbuilt ones.

Have a look at the link I provided for more information
 
Share this answer
 
The return value of the main function is the exit status[^] of your application. A piece of usefult info that can be checked by the 'outside world'.
 
Share this answer
 
You are free to return any result or object from your functions, but in C++ is the convention return integers as status code. And 0 means no error.

If you ask about the main function it has the prototype of return an integer, so return 0 for everything was fine.

For some batch modi it is possible to check the return value and do something with.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900