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

C++FunctionSyntaxDeclaration

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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.

Solution 2 - C++

In addition to the "a definition is also a declaration" thing, the following is legal C++:

int f(), g();

This declares two functions,f and g, both without arguments and with a return type of int, but the definition of f is not followed (immediately) by a semicolon. Likewise, this is legal:

int f(), i = 42;

But it is indeed not allowed to omit the semicolon entirely in these cases, so it would be somewhat surprising if either was taken as an example of a declaration without a following semicolon. In fact, the following is illegal:

void *p, f() {}

Other than a (mere) function declaration, a function definition cannot be combined with any other declaration or definition to the same type-specifier. (If this were legal, it would define both a void *p and a void f() {}.)

In any case, this seems to be a "gotcha" type of question that should not be in an intermediate programming test.

(Oh, by the way, please don't actually write code like int f(), i = 42;.)

Solution 3 - C++

The other answers and comments call out several of the many ways that this is a horrid, misleading and badly-written question. But there is another problem that no one else has identified yet. The question is:

> A semicolon (';') is not needed after a function declaration. True or False.

OK, let's look at a function declaration:

int func();       /* */
/*           ^       */
/*           |       */
/* That whitespace is "after the function declaration". */

That whole thing is the declaration. The declaration is not int func() and then followed by a ;. The declaration is int func(); and then is followed by whitespace.

So, the question is: is a semicolon needed after the declaration? Of course not. The declaration already has a semicolon in it which terminated it. A semicolon after the declaration would be pointless. By contrast, int func(); ; would be a semicolon after a function declaration.

The question was almost certainly intended to ask the question "true or false: the last token in a function declaration is always a semicolon" But that's not the question that they wrote, because the author of the quiz was not thinking clearly about the problem.

My advice is to avoid programming language quizzes altogether. They're pretty awful.


Fun fact, while we are on the subject. In C#, these are all legal:

class C {}
class D {};
struct E {}
struct F {};

In C#, a class or struct declaration may end in a semicolon, or not, at your discretion. This odd little feature was added for the benefit of C/C++ programmers coming to C# who have it in their fingertips that type declarations end in a pointless semicolon; the design team didn't want to punish them for having this habit. :-)

Solution 4 - C++

You can declare a function like this too:

int func(){
    return 1;
}

The statement is very ambiguous. The right answer should be: it depends on how you declare the function.

Anyway, I'd have chosen false too, and maybe you can report the question to someone.

Solution 5 - C++

> A semicolon (';') is not needed after a function declaration. > > True or False.

True. A semicolon is not needed after any declaration. Nor after any definition. Nor after any statement.

Many kinds of declaration have to end with a semicolon, as the syntax in section 7 [dcl.dcl] specifies. But there is never any need to write a second one after that.

Solution 6 - C++

This depends on whether we are declaring or defining the function. If we are declaring the the function, we need to include the semicolon (;), and if we are defining the function, the semicolon is not needed.

A declaration is like this:

int add(int, int);

And a definition is like this:

int add(int a, int b)
{
    // ...
}

Solution 7 - C++

It's a pity the question that you took doesn't say "directly after". We could for example write this:

int func()  /* My function */ ;

Or I could write:

int func()
int a = 42;

In the first case the semicolon is not directly after the declaration, but that would be OK.

In the second case there is a semicolon "after" the declaration, but not directly after.

I think Eric Lippert has the right idea in his answer.

It's like saying "should there be a period after the end of a sentence in English?". Arguably, a sentence already has a period at the end (otherwise it wouldn't be a sentence) and therefore there should not be a period after the sentence..

Solution 8 - C++

Even though I agree with almost all of the other answers, stating that the question is worded very ambiguous, and that your answer is technically correct, allow me to give a different perspective:

This is how I've always called them:

void func();  // The function prototype

...

void func()
{
    // The function definition
}

I'm assuming the question was made up with this terminology in mind.

Definition and declaration are both the same concept in my eyes. "I define x = y" == "I declare x = y".

But of course, there's a big difference between the function prototype (on top) and the actual definition of the function.

Solution 9 - C++

You can use ; for prototypes only.

Solution 10 - C++

It's kind of a tricky question, but they used the word declaration which means something like this:

int example();

So it's true in this case.

If they'd used the word implementation then it'd been false.

Solution 11 - C++

Semicolon (;) is used to tell the compiler that after this semicolon (;) a new statement starts.

So I think the semicolon (;) is required during a function declaration only. So according to me, the answer will be true.

Solution 12 - C++

When functions are defined before main():

  • Don't need semicolon because the function is already defined

When functions are defined after main():

  • Need semicolon because because you are prototyping that function and telling the compiler that the function exits.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionLoganView Question on Stackoverflow
Solution 1 - C++jwismarView Answer on Stackoverflow
Solution 2 - C++Arne VogelView Answer on Stackoverflow
Solution 3 - C++Eric LippertView Answer on Stackoverflow
Solution 4 - C++Luca CorsiniView Answer on Stackoverflow
Solution 5 - C++Marc van LeeuwenView Answer on Stackoverflow
Solution 6 - C++Rocx En RuffView Answer on Stackoverflow
Solution 7 - C++Nick GammonView Answer on Stackoverflow
Solution 8 - C++OpifexView Answer on Stackoverflow
Solution 9 - C++M7offView Answer on Stackoverflow
Solution 10 - C++user4836681View Answer on Stackoverflow
Solution 11 - C++JatinderView Answer on Stackoverflow
Solution 12 - C++shiv shahView Answer on Stackoverflow