subroutine question int or void for declared return value

This is a discussion on subroutine question int or void for declared return value within the C Programming forums, part of the General Programming Boards category; I realize for the main function that int main(void) is recommended instead of void main(void) but what about subroutines? I ...

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

    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
    5,827
    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%.

  3. #3
    Registered User
    Join Date
    Sep 2006
    Location
    Beaverton, Oregon, United States
    Posts
    156
    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,261
    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
    5,827
    But not for main().
    Right 98% of the time, and don't care about the other 3%.

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, 02: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, 10: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

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