Isn't a semicolon (';') needed after a function declaration in C++?

Logan picture Logan · Feb 20, 2019 · Viewed 17.8k times · Source

I just recently took an intermediate programming test, and one of the questions I got wrong was as follows:

A semicolon (';') is not needed after a function declaration.

True or False.

I chose "false" (and please correct me if I'm wrong because I feel like I'm going crazy), a function declaration is what you write before the definition (at the top of the code) so the compiler knows the function call before even calling it, and a function definition is what makes up the function as a whole.

I.e.,

Declaration:

int func();

Definition:

int func() {
  return 1;
}

Shouldn't the answer to this be false?

Answer

jwismar picture jwismar · Feb 20, 2019

You can have a situation where you declare and define the function in one step, i.e. if you include the function definition at the point where you're declaring it. So technically I suppose true is correct. But the question is worded in such a way that I would have answered it the way you did.