175

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?

23
  • 42
    A definition is also a declaration. But I would say your answer was correct.
    – user2100815
    Feb 20, 2019 at 16:16
  • 218
    It's a tricky nitpicking question and has no bearing on anyone's ability to program well. Feb 20, 2019 at 16:18
  • 41
    I always find the questions, that result in double-negatives, confusing. In my mind, such questions are designed to trip students up. Why couldn't the question be formed in a following way: "A semicolon (';') is always needed after a function declaration. True or False."? :/ Feb 20, 2019 at 16:20
  • 19
    @phonetagger All this confusion goes to show how badly worded the question is. Feb 20, 2019 at 16:20
  • 34
    Hanlon's Razor suggests that the author of the test mixed up "declaration" and "definition".
    – Sneftel
    Feb 20, 2019 at 16:21

12 Answers 12

163

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.

5
  • 10
    I'd argue that true isn't correct because of the reason you gave. If there are cases when a semicolon is needed, then it is false (or not true). True is the absolute to me, if there are clear cases when it is needed then you cannot say true.
    – I Funball
    Feb 21, 2019 at 18:55
  • 16
    @IFunball Good argument. Stupid natual languages. The sentence "A semicolon (';') is not needed after a function declaration" can be read as "A semicolon (';') is not (ever) needed after a function declaration" or as "A semicolon (';') is not (always) needed after a function declaration". Whether to qualify the statement as true or false hinges on choosing an interpretation. Strictly spoken the question is unclear and hence has no clear answer. Feb 22, 2019 at 14:44
  • 6
    @IFunball It's because "declaration", with no further context and no statement that we're language lawyering, is commonly understood to mean "a non-defining declaration". The question was unfair. Feb 23, 2019 at 13:23
  • 2
    Any exam question that's unclear to someone who knows the content being tested for is bugged.
    – Nat
    Feb 25, 2019 at 20:47
  • 2
    Sounds like we need to add an undefined behavior clause to the English language Feb 26, 2019 at 19:52
148

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;.)

3
  • 2
    It's also possible to use a typedef to define a function type, and then make use of that to declare many functions at once, e.g. typedef int fooProc(int); fooProc a,b.c.d.e; I'm not sure why standard headers for floppy-drive-based compilers didn't do that back in the day, since I would think it would have allowed header files to be quite a lot smaller and thus faster to process.
    – supercat
    Feb 21, 2019 at 18:41
  • Also consider int f(int(&g)(),int(*h)()){return g()+h();} This has three function declarations, one of which is followed by an open curly brace, another by a comma, and a third by a close parenthesis. Feb 23, 2019 at 22:23
  • 1
    @DavidHammen: That doesn't strictly declare functions other than int f(stuff). Even in the scope of the function, g is an automatic variable of type reference to function, and h is a pointer to function. Feb 24, 2019 at 2:14
85

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. :-)

1
  • Comments are not for extended discussion; this conversation has been moved to chat.
    – Samuel Liew
    Feb 23, 2019 at 14:28
25

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.

4
  • 3
    Anyway, don't put the thing on a personal level. The important thing is that you understood how a function declaration-definition works, so don't worry about it too much, just make sure that the question will be at least checked and go on Feb 20, 2019 at 16:27
  • 11
    Absolutely. Honestly, I learned more about function declaration-definition from getting the question wrong than I would've had I gotten in it correct.
    – Logan
    Feb 20, 2019 at 16:35
  • 1
    @Logan don't worry too much. If you know how to write and read a function that's all you need. I personally hate these kind of questions that 1. are not well defined 2. test your theoretical knowledge of the syntax. To me it's like muscle memory. When I write each digit goes effortlessly to the key it is supposed to go, but if you give me a test about what keys should a digit press I would be completely hopeless without a keyboard to physically do the action ...
    – bolov
    Feb 20, 2019 at 19:17
  • 2
    ... Writing common syntax (e.g. like a function) will become a second nature to you. And when you will mess it up because you just switched languages, well... intellisense and syntax highlighting make for quick and efficient solutions. Invest your time and energy in something more useful.
    – bolov
    Feb 20, 2019 at 19:17
20

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.

3
  • 1
    I see that Eric Lippert already argued this point. I guess all the upvotes made me overlook it. Feel free to cast your votes there. Feb 22, 2019 at 14:35
  • Pretty much any question that asks, "X is always true: true or false?" is going to have the answer "false." Heck, there's no need to have a semicolon anywhere; the compiler may complain and refuse to compile your program, but that's hardly the end of the world; I wouldn't call it a fundamental need. ;) Feb 23, 2019 at 6:40
  • @Quuxplusone if the compiler rejects your program, your program doesn't have any function declarations in it :) Feb 24, 2019 at 6:04
6

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)
{
    // ...
}
1
  • 10
    The problem with this answer is that it suggests that definitions and declaration are mutually exclusive. In fact, every definition is a declaration; definitions are a subset of declarations.
    – MSalters
    Feb 21, 2019 at 8:22
6

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.

4
  • To me your prototype is the declaration based on how I learned (not saying you're wrong either though), but then I'd also expect a prototype to specify the number and type of arguments, or void, but I expect you omitted that for brevity.
    – David S
    Feb 22, 2019 at 13:41
  • David S: Yes, ofcourse it would also contain the number and type of arguments, but I indeed omitted them for brevity (note that there are also no arguments in the actual function declaration). However, I don't really agree when you say the full function declaration is called the prototype. I quote Wikipedia: "a function prototype or function interface is a declaration of a function that specifies the function's name and type signature (arity, data types of parameters, and return type), but omits the function body."
    – Opifex
    Feb 22, 2019 at 15:56
  • @DavidS: In C++, function declarations are always prototypes (or definitions), and void func(); is exactly equivalent to void func(void);. This is very different from C, where void func(); doesn't tell the compiler anything about the args, and isn't the same thing as void func(void);. A later prototype or definition are a good idea, otherwise the caller has to apply the default arg promotions (e.g. float -> double, and narrow integer types to int. Same rules as for args to variadic functions.) Feb 22, 2019 at 19:36
  • My apologies, I ended up here looking at something C related and didn't note the change of language. I won't delete my comment in the interests of clarity, but consider it retracted.
    – David S
    Feb 26, 2019 at 10:13
6

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..

5
  • 4
    Nice. Ending that sentence with an extra period. I see what you did there.
    – David S
    Feb 22, 2019 at 13:43
  • 2
    int func() int a=42; doesn't compile. You need a comma, not another int. See @Arne's answer posted over a day before this. The only new thing in this answer is the last paragraph, with the analogy to English sentences. Feb 22, 2019 at 19:32
  • 1
    I didn't say the second example compiled. I was pointing out that saying that a semicolon was needed "after" the declaration was ambiguous. My example had a semicolon after the declaration but it doesn't compile. Feb 22, 2019 at 19:44
  • 1
    This same problem occurs in error messages; a favourite example from C# is "A params parameter must be the last parameter in a formal parameter list". Now, suppose instead I said "A frob must be the last gloob in a gloob list". Does this mean (1) Every gloob list have exactly one frob at the end, like every question has exactly one question mark at the end, (2) A gloob list can have any number of frobs, but if it has one or more frobs, the last item must be a frob, like an even number can have any number of 02468, but one of the must be the last, or... Feb 25, 2019 at 22:56
  • ... (3) a gloob list can have zero or one frobs, and if it has one, it comes at the end? If you don't know the context, I'd think that (1) is the most sensible explanation, but in the case of "params parameter", (3) is the correct explanation. Many informal descriptions of programming language elements have a property that my technical editor friends call "COIK" -- Clear Only If Known. If you do not already understand the material thoroughly, a description of it is useless to you, but if you already understand it thoroughly, you don't need the description! Feb 25, 2019 at 22:58
4

You can use ; for prototypes only.

4

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.

2

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.

2
  • Declarations are not statements though. Mar 24, 2019 at 8:01
  • but after the function declaration, we are executing a new line of code using compiler. so I think before executing a new line of code compiler have to know where the previous line of code ends only then a compiler can generate native code(i.e 0101).
    – Jatinder
    Mar 25, 2019 at 10:27
2

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.

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.