void return

This is a discussion on void return within the C++ Programming forums, part of the General Programming Boards category; At best it returns void. Not 0. robatino's statement was with respect to the global main() function, which returns an ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,717
    At best it returns void. Not 0.
    robatino's statement was with respect to the global main() function, which returns an int, and which specifically returns 0 if control does not reach a return statement within it.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,460
    Ah. I'm too distracted it seems. Need a rest.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #18
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    There have been buggy compilers from time to time that were so broken that they required a return at the end of each function, even those that didn't return anything.

    But nowadays, I would personally consider it a very strange habit if a programmer put an explicit return at the end of each function. Waste of a line and 7 characters, and it's so easy to accidentally put it in the wrong place, at least when later rearranging a function.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  4. #19
    Registered User
    Join Date
    May 2006
    Posts
    903
    @vart: But again, you can use if(a != b) and put your code in the if() block without using an empty return.

  5. #20
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    That's true, Des, but that can get very messy if there are a large number of conditions where you want to short-circuit out.
    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

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by XSquared
    That's true, Des, but that can get very messy if there are a large number of conditions where you want to short-circuit out.
    Also, if there are a large number of lines after the condition, they all have to be indented an extra level, which takes up more space than a single extra return, and I think makes the code harder to understand.

Page 2 of 2 FirstFirst 12
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. How to better manage large .cpp files
    By 39ster in forum C++ Programming
    Replies: 6
    Last Post: 08-25-2008, 08:24 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. ChangeDisplaySettings - Blank?
    By Tonto in forum Windows Programming
    Replies: 13
    Last Post: 12-26-2006, 03:17 PM
  5. i cannot see the mouse arrow
    By peachchentao in forum C Programming
    Replies: 6
    Last Post: 12-10-2006, 03:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21