![]() |
| | #1 |
| Registered User Join Date: Apr 2003
Posts: 42
| I have been confused for a while on this: What is wrong with using void? Why is wrong to use 'void main()'? Is it okay to use 'void' for your functions? For example: PHP Code:
__________________ "All things come to an end" |
| Machewy is offline | |
| | #2 |
| Registered User Join Date: May 2002 Location: Cape Town
Posts: 777
| It's OK for your functions, but not for main(). main() returns an int, so you have to explicitly state that it returns an int. ISO requires main() to return an int.(if i'm not mistaken) |
| The Dog is offline | |
| | #3 |
| C++ Developer Join Date: Jun 2002 Location: UWaterloo
Posts: 2,718
| An explanation from Bjarne Stroustrup himself. (about void main( ) ) Yes, void is okay for functions, but it is better to return an int just so you can be sure the function didn't encounter any errors.
__________________ Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie |
| XSquared is offline | |
| | #4 |
| The Pantless Man Join Date: Jan 2003
Posts: 262
| So does Bjourne mean that you don't have to return a number after main()? Look at this: Code: In C++ main() need not contain an explicit return statement. In that case, the value returned is 0, meaning successful execution. For example:
#include<iostream>
int main()
{
std::cout << "This program returns the integer value 0\n";
}
__________________ If you ever need a hug, just ask. |
| CheesyMoo is offline | |
| | #5 |
| C++ Developer Join Date: Jun 2002 Location: UWaterloo
Posts: 2,718
| Yep. That's what he means. You don't have to write return 0 at the end of main(), although most compilers will spit out an error if you don't.
__________________ Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie |
| XSquared is offline | |
| | #6 |
| Registered User Join Date: Nov 2002
Posts: 1,109
| it's also good practice to type return 0; at the end of main. |
| alpha is offline | |
| | #7 |
| Bios Raider Join Date: Jul 2002 Location: South Africa
Posts: 765
| Maybe you could ask Prelude?
__________________ The knack of flying is learning to throw yourself at the ground and miss. |
| biosninja is offline | |
| | #8 |
| Registered User Join Date: Aug 2002
Posts: 87
| Just to put it simple it is best to return an int from main to tell the Operating System wherter or not the program ended succesfully or with some kinda error: return 0; // To OS - No Problem return 1; // To OS - Problem this can be useful for the OS in some ways, havent quite figured out how though .In the case of user written functions it is OK to return void but it is better to return either bool or int to indicate succes or failure of the function. This is mostly to be able to do good error-checking. It is understandeble that for some functions error-checking does is hardly unlikely to occur but when consider that you must assume the user of your program is the biggest idiot on the face of this earth as well a Murphy's Law (evrything that can go wrong, will go wrong) it is best to include as much as possible error-checking. |
| DirX is offline | |
| | #9 |
| Registered User Join Date: May 2002
Posts: 719
| I use void main() to test things in the console. Not for anything else.
__________________ "What are you after - the vague post of the week award?" - Salem IPv6 Ready. Travel the world, meet interesting people...kill them. Trying to fix or change something, only guaruntees and perpetuates its existence. I don't know about angels, but it is fear that gives men wings. The problem with wanting something is the fear of losing it, or never having it. The thought makes you weak. E-Mail Xei |
| Xei is offline | |
| | #10 |
| Registered User Join Date: Aug 2002
Posts: 87
| using void main() 'should' not give any problems with current compilers. but it is not standard and is not good pratice. For small programs it should not be to terrible but once you get into big programs and it crashes for some reason (not enough memory etc) it is good to let the OS know something is wrong. |
| DirX is offline | |
| | #11 |
| C++ Developer Join Date: Jun 2002 Location: UWaterloo
Posts: 2,718
| >>it is good to let the OS know something is wrong. Of course, if you're programming for a Windows console, its usually Windows that caused the error. And besides, Windows has enough errors to deal with on its own.
__________________ Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie |
| XSquared is offline | |
| | #12 |
| Registered User Join Date: Aug 2002
Posts: 87
| .... WHy are people always so negative about Windows =] If you think about it, it is a great OS and WinXP and Win2k have minimized the amount of crashes etc greatly.... |
| DirX is offline | |
| | #13 |
| C++ Developer Join Date: Jun 2002 Location: UWaterloo
Posts: 2,718
| Actually, in 4 months of running XP Pro, I've gotten more BSODs than in 2 years of 98SE. :P
__________________ Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie |
| XSquared is offline | |
| | #14 |
| Registered User Join Date: May 2002
Posts: 719
| Wierd. Windows just handles Exceptions differently. A new practice I am learning, Structured Exception Handling, will allow me to control such crashes from my application, and hopefully recover from the error. Many of you likely already use it But I was very happy when I learnt about it, I still am! It's wonderful!
__________________ "What are you after - the vague post of the week award?" - Salem IPv6 Ready. Travel the world, meet interesting people...kill them. Trying to fix or change something, only guaruntees and perpetuates its existence. I don't know about angels, but it is fear that gives men wings. The problem with wanting something is the fear of losing it, or never having it. The thought makes you weak. E-Mail Xei |
| Xei is offline | |
| | #15 |
| Grammar Police Join Date: Jan 2003
Posts: 355
| BSODs You'll probably find that all your blue screens are either yours, or your hardware's fault. XP is just as, if not more stable than 2k, and is CERTAINLY more stable than 9x. The problem with void main() is that so many tuts etc. still use it! I'm still learning C++, and the first mains i encountered were all integers, so i never questioned it. In fact this site's tutorials were my first brush with c++, unfortunately that included using c-style headers for a while. Last edited by HybridM; 04-15-2003 at 04:16 AM. |
| HybridM is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Inserting a swf file in a windows application | face_master | Windows Programming | 12 | 05-03-2009 11:29 AM |
| My Rpg system(Text) | kevinawad | C++ Programming | 56 | 06-25-2008 07:04 PM |
| i cannot see the mouse arrow | peachchentao | C Programming | 6 | 12-10-2006 04:14 AM |
| need help with handelling multiple source files | DarkMortar | C++ Programming | 38 | 05-26-2006 10:46 PM |
| oh me oh my hash maps up the wazoo | DarkDays | C++ Programming | 5 | 11-30-2001 12:54 PM |