Thread: Why should you specify that a function expects no arguments with the 'void' keyword?

  1. #1
    c_beginner_
    Guest

    Why should you specify that a function expects no arguments with the 'void' keyword?

    Hi!

    I've just finished K&R and am now reading some modern C source code to see how things have changed since 1978.

    One of the things that I immediately noticed is that in functions that expect no parameters, the author puts the keyword 'void' between the parenthesis. Why is this? Why not just leave the brackets empty; in the other language that I've played with - Pascal - empty brackets have never caused ambiguity or confusion.

    Thanks for any input,
    c_beginner_

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    739
    Quote Originally Posted by c_beginner_ View Post
    Hi!

    I've just finished K&R and am now reading some modern C source code to see how things have changed since 1978.

    One of the things that I immediately noticed is that in functions that expect no parameters, the author puts the keyword 'void' between the parenthesis. Why is this? Why not just leave the brackets empty; in the other language that I've played with - Pascal - empty brackets have never caused ambiguity or confusion.

    Thanks for any input,
    c_beginner_
    A lot has changed! K&R is from 1978! Many official C Standards have been released from ANSI & ISO. You should NOT rely on K&R! It is NOT a C Standard, and a lot has been added, changed, or removed from the official C Standards.

    ANSI/ISO C89/90, C99, C11, and C18 (Sometimes called C17)

    In my opinion, and the opinion of most of the professional programmers, is that all functions should be fully prototyped and fully defined with a formal parameter list, or using the keyword "void", if no arguments are to be passed, or returned. It is legal to leave the formal parameter list empty, but I consider it poor programming practice.

  3. #3
    c_beginner_
    Guest
    Quote Originally Posted by rstanley View Post
    A lot has changed! K&R is from 1978! Many official C Standards have been released from ANSI & ISO. You should NOT rely on K&R! It is NOT a C Standard, and a lot has been added, changed, or removed from the official C Standards.

    ANSI/ISO C89/90, C99, C11, and C18 (Sometimes called C17)

    In my opinion, and the opinion of most of the professional programmers, is that all functions should be fully prototyped and fully defined with a formal parameter list, or using the keyword "void", if no arguments are to be passed, or returned. It is legal to leave the formal parameter list empty, but I consider it poor programming practice.
    Thank you for your reply - so it's just a matter of style, rather than for any technical reason - got it!

  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    722
    Quote Originally Posted by c_beginner_ View Post
    Thank you for your reply - so it's just a matter of style, rather than for any technical reason - got it!
    Not only a matter of style. This:
    Code:
    int f() { return 2; }
    is different than this:
    Code:
    int f(void) { return 2; }
    Since if you call the first with:
    Code:
    y=f(1,2,"abc", 3.14, NULL);
    Your compiler probably will not emit a warning...

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,930
    Quote Originally Posted by c_beginner_
    One of the things that I immediately noticed is that in functions that expect no parameters, the author puts the keyword 'void' between the parenthesis. Why is this? Why not just leave the brackets empty; in the other language that I've played with - Pascal - empty brackets have never caused ambiguity or confusion.
    I'd say it's a historical artifact of how specifying parameters evolved in the history of C. A long time ago, I believe back when K&R was authoritative, it was common to specify parameters when defining functions using an identifier list: you would list the identifiers in the parentheses, and then you would list the corresponding types before the opening brace. An empty identifier list thus meant that there are no parameters. I don't know what was the practice for forward declarations at the time, but presently with this syntax, when you forward declared the function in this way, you would only specify the return type and name, providing no information about the parameter types, hence such a forward declaration isn't what we would now call a function prototype.

    In modern C, we use parameter type lists instead of identifier lists. But this meant that there had to be some way to differentiate between an empty identifier list and and empty parameter type list, while addressing the issue of forward declarations. The solution was to have void as the parameter type list denote an empty parameter type list. Thus, if you use an empty identifier list for a forward declaration, you won't get the same benefit as if you used void, and if you use it for a function definition, you're technically using an obsolescent feature instead of the modern equivalent.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 01-24-2011, 05:37 AM
  2. Using function from library (which expects a struct)
    By appelflap in forum C Programming
    Replies: 24
    Last Post: 01-19-2010, 06:32 PM
  3. too many arguments to function 'void
    By Queatrix in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2005, 07:14 PM
  4. void keyword
    By anomaly in forum C++ Programming
    Replies: 8
    Last Post: 11-17-2003, 07:31 PM

Tags for this Thread