Thread: subroutine question int or void for declared return value

  1. #1
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176

    subroutine question int or void for declared return value

    I realize for the main function that int main(void) is recommended instead of void main(void) but what about subroutines?

    I still see subroutines using void return declarations 'void insertString(source[],int index, int remove)' for example.

    So I'm not sure what the guideline is for declared return values with subroutines.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    main() is special. A program is not even required to work correctly (or compile or link) if main() does not return int. Or, more formally, an implementation (compiler, library, etc) is only required to support int main().

    Other functions can have any return type the programmer might consider appropriate (int, void, pointers, double, struct types, enums, etc).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    176
    Quote Originally Posted by grumpy View Post
    main() is special. A program is not even required to work correctly (or compile or link) if main() does not return int. Or, more formally, an implementation (compiler, library, etc) is only required to support int main().

    Other functions can have any return type the programmer might consider appropriate (int, void, pointers, double, struct types, enums, etc).
    Understood, void is fine if its returning no value? Thanks!

  4. #4
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Absolutely
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  5. #5
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    But not for main().
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. "variable or field declared void"
    By Treborh in forum C++ Programming
    Replies: 13
    Last Post: 02-06-2010, 03:33 PM
  2. Replies: 4
    Last Post: 04-03-2009, 04:16 PM
  3. Replies: 9
    Last Post: 06-09-2008, 09:53 AM
  4. void return
    By l2u in forum C++ Programming
    Replies: 20
    Last Post: 12-15-2006, 11:55 AM
  5. was previously implicitly declared to return `int'
    By Schwarzhelm in forum C Programming
    Replies: 1
    Last Post: 10-14-2003, 10:13 PM