6

Basically, I am reading through this book right here and in Section 1.6: Some Differences between C and C++ it is stated:

Another subtle difference between C and C++ is that in a C++ program, all functions must be prototyped.

I am sure that this is not true from all the C++ programs that I have written. Is this only true for some versions of C++? Is it also true for C?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
SDG
  • 2,260
  • 8
  • 35
  • 77
  • 7
    Well, remind me never to buy _that_ book. – Mooing Duck Apr 12 '17 at 21:25
  • 4
    A 1994 book? Please burn the book. It's doing you and this world a disservice... :-(.. The only C++ books you should read from the 90s is those of Scott Meyers. – WhiZTiM Apr 12 '17 at 21:26
  • 3
    Does it say what it means to "prototype a function"? – juanchopanza Apr 12 '17 at 21:26
  • 1
    Haha, please explain people. – SDG Apr 12 '17 at 21:26
  • They are probably talking about implicit `int`. Welcome to pre-1999... – Baum mit Augen Apr 12 '17 at 21:27
  • @BaummitAugen they talk about it after 2 paragraphs. – SDG Apr 12 '17 at 21:27
  • 1
    @MooingDuck Apparently the author was responsible for writing the C99 Standard right? – SDG Apr 12 '17 at 21:28
  • 2
    **Please** do yourself and your potential co-workers a favour and burn the book, grind the askes to dust and send it into the sun! – too honest for this site Apr 12 '17 at 21:30
  • 2
    @SharanDuggirala http://www.seebs.net/c/c_tcn4e.html - Back in 1996, having heard horror stories about Herbert Schildt's C: The Complete Reference, I decided to check it out. I flipped the book open; I found glaring errors. I paged through it. I found more glaring errors. In short, the book had lived up to the hype; it was awful. –  Apr 12 '17 at 21:31
  • 2
    Why am I being devoted? This is a genuine question – SDG Apr 12 '17 at 21:32
  • 2
    Read about valid downvote reasons. One is "no research". – too honest for this site Apr 12 '17 at 21:41
  • @Olaf I researched this, but couldn't find anything relevant. So I just defaulted to asking here. I didn't orthogonally think of checking out the review of the book and it's errors errata. – SDG Apr 12 '17 at 21:46
  • 1
    Citing a 23 year old, obviously outdated book does not really qualify as research. – too honest for this site Apr 12 '17 at 21:48
  • @Olaf Sufficed to say, lesson learned. – SDG Apr 12 '17 at 21:50
  • 2
    @Olaf: the downvote reason is "no research", not "I disapprove of the book you chose". Don't get me wrong: it is a terrible book--but the OP should be steered toward the [book list](http://stackoverflow.com/q/388242/179910), not punished for making an innocent mistake. – Jerry Coffin Apr 12 '17 at 22:16
  • @JerryCoffin: Please read the question. The book raised a question in OP, which he directly passed to us, instead of doing some research on his own first. At least that's the information given in the question. Sorry if my comment was not clear enough about this. – too honest for this site Apr 13 '17 at 01:46
  • @Olaf: I did read the question. I also read your: "Citing a 23 year old, obviously outdated book does not really qualify as research." It was specifically with that comment that I was disagreeing. Penalizing him because he didn't realize that Schildt sucks is unreasonable. As for the book being out of date: what he asked about remains as true today as it was when the book was new (as in: typical BullSchildt, but a newer book probably wouldn't make a significant difference). – Jerry Coffin Apr 13 '17 at 02:36
  • @JerryCoffin: "what he asked about remains as true today" - err - no! It is not true for C since 18 years now (and non-pototype declarators are an obsolecent feature at least since C11, i.e. 6 years). but you missed the point: OP already has doubts about what the book wrote, but did not do any further research. Neither for C nor C++. There are more recent freely available books about both languages. – too honest for this site Apr 13 '17 at 04:16

1 Answers1

15

It has been true of C++ since the beginning (although in C++ it's just called a "declaration", not a "prototype").

As C existed decades ago, it allowed you to call a function without declaring it. You could, however, declare a function if you wanted to--usually to tell the compiler that it had a return type different from what the compiler would deduce on its own. So, in C a function declaration looks something like this:

long f();

Note that empty parens there. That's what separates a function "declaration" from a function "prototype" (though a prototype is basically a superset of a declaration, so a prototype also declares the function in question). A prototype always has something inside the parens to indicate the number and type of parameters the function accepts, on this general order:

short g(int a, double b);

If it doesn't accept any parameters, you have to put in void to indicate that:

int h(void);

If you leave the parens empty, that (as noted above) means it's a function declaration instead of a prototype--and that means you're telling the compiler the function's return type, but you're not telling it anything about the number or type of parameters.

C++ (since before it was called C++, if I recall correctly) has only had one concept instead of the two in C. In C++ every function must be declared--and a declaration always includes the number of parameters, and the type of each. This is absolutely necessary to support (for one obvious example) function overloading, where the correct function to call is determined from the number and types of arguments you pass in the call.

A function definition in C++ also acts as a function declaration. Every function must be declared, but the declaration doesn't have to be separate from the definition.

In reasonably modern C, you normally get pretty much the same--that is, a "new" (i.e., not ancient) type function definition also acts as a prototype for that function. As C was originally defined, it included a syntax for a function definition that looked like this:

int f(a, b, c) 
int a;
short b;
long c;
{
    // function body here
}

This defines the function, but the compiler treats it only as a function declaration, not a prototype--that is, it tells the compiler the return type, but the number and types of parameters (even though they're specified) are not used by the compiler in the same way they would be with a function prototype. C++ has never used or supported this style of function definition though.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
  • A prototype is a declaration, too. – too honest for this site Apr 12 '17 at 21:32
  • 1
    I use functions without declaring/prototyping them, especially static functions within a module. Note this has nothing to do with class methods. Hence, C++ does NOT require functions to be declared/prototyped. – franji1 Apr 12 '17 at 21:35
  • @franji1: This is very bad practice in both languages and not allowed in C either. You at least have to declare a function before usage with the correct arguments. I'd recommend you instantly leave the 1980ies, a declaration is required in C since 18 years (and strongly recommended since 1989)! But maybe you are just not aware what a _declaration_ actually is. In that case: reading the standard and getting a more recent book would be a good start. – too honest for this site Apr 12 '17 at 21:39
  • 5
    @franji1: Sorry, but if your compiler allowed that, it's thoroughly broken. My guess, however, is that you're simply mistaken (probably by missing the fact that a function definition also acts as a function declaration). – Jerry Coffin Apr 12 '17 at 21:42
  • 1
    @JerryCoffin: Point is, C **requires** a declaration before usage! – too honest for this site Apr 12 '17 at 21:42
  • 2
    @Olaf: Was there something about: "**As C existed decades ago**, it allowed you to call a function without declaring it" that wasn't clear about that? – Jerry Coffin Apr 12 '17 at 21:43
  • I somehow missed the "but it is required since ..." part. And a declaration can (and should) very well specify the types. – too honest for this site Apr 12 '17 at 21:44
  • @Olaf: Seems like a silly bit of trivia to me. Knowing what (for example) the Magna Carta did, and having some idea of how it changed (at least some) people's lives is real information. Knowing that it was signed on a Tuesday at around 2 o'clock in the afternoon...not so much. What the C standard calls a declaration cannot specify the number or type of parameters--to do that, you need to use a prototype. – Jerry Coffin Apr 12 '17 at 21:50
  • @JerryCoffin: To repeat: A prototype **is** a declartion. And a declaration can be a superset. (and AFAIK the Magna Carta was signed on a Wednesday after tea-time) – too honest for this site Apr 12 '17 at 21:54
  • @JerryCoffin Yes, that is the case for me (so the compiler is fine). I did not realize that a definition is ALSO a declaration. I got confused when I saw that a prototype is also a declaration (misinterpreted it as _a declaration is a prototype_). – franji1 Apr 13 '17 at 12:50