Isn't a semicolon (';') needed after a function declaration in C++?
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;
}
My question is, shouldn't the answer to this be false?
c++ function syntax declaration
|
show 18 more comments
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;
}
My question is, shouldn't the answer to this be false?
c++ function syntax declaration
40
A definition is also a declaration. But I would say your answer was correct.
– Neil Butterworth
Feb 20 at 16:16
213
It's a tricky nitpicking question and has no bearing on anyone's ability to program well.
– phonetagger
Feb 20 at 16:18
40
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."? :/
– Algirdas Preidžius
Feb 20 at 16:20
18
@phonetagger All this confusion goes to show how badly worded the question is.
– François Andrieux
Feb 20 at 16:20
34
Hanlon's Razor suggests that the author of the test mixed up "declaration" and "definition".
– Sneftel
Feb 20 at 16:21
|
show 18 more comments
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;
}
My question is, shouldn't the answer to this be false?
c++ function syntax declaration
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;
}
My question is, shouldn't the answer to this be false?
c++ function syntax declaration
c++ function syntax declaration
edited 19 hours ago
Bhargav Rao♦
30.8k2092114
30.8k2092114
asked Feb 20 at 16:14
LoganLogan
879225
879225
40
A definition is also a declaration. But I would say your answer was correct.
– Neil Butterworth
Feb 20 at 16:16
213
It's a tricky nitpicking question and has no bearing on anyone's ability to program well.
– phonetagger
Feb 20 at 16:18
40
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."? :/
– Algirdas Preidžius
Feb 20 at 16:20
18
@phonetagger All this confusion goes to show how badly worded the question is.
– François Andrieux
Feb 20 at 16:20
34
Hanlon's Razor suggests that the author of the test mixed up "declaration" and "definition".
– Sneftel
Feb 20 at 16:21
|
show 18 more comments
40
A definition is also a declaration. But I would say your answer was correct.
– Neil Butterworth
Feb 20 at 16:16
213
It's a tricky nitpicking question and has no bearing on anyone's ability to program well.
– phonetagger
Feb 20 at 16:18
40
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."? :/
– Algirdas Preidžius
Feb 20 at 16:20
18
@phonetagger All this confusion goes to show how badly worded the question is.
– François Andrieux
Feb 20 at 16:20
34
Hanlon's Razor suggests that the author of the test mixed up "declaration" and "definition".
– Sneftel
Feb 20 at 16:21
40
40
A definition is also a declaration. But I would say your answer was correct.
– Neil Butterworth
Feb 20 at 16:16
A definition is also a declaration. But I would say your answer was correct.
– Neil Butterworth
Feb 20 at 16:16
213
213
It's a tricky nitpicking question and has no bearing on anyone's ability to program well.
– phonetagger
Feb 20 at 16:18
It's a tricky nitpicking question and has no bearing on anyone's ability to program well.
– phonetagger
Feb 20 at 16:18
40
40
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."? :/
– Algirdas Preidžius
Feb 20 at 16:20
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."? :/
– Algirdas Preidžius
Feb 20 at 16:20
18
18
@phonetagger All this confusion goes to show how badly worded the question is.
– François Andrieux
Feb 20 at 16:20
@phonetagger All this confusion goes to show how badly worded the question is.
– François Andrieux
Feb 20 at 16:20
34
34
Hanlon's Razor suggests that the author of the test mixed up "declaration" and "definition".
– Sneftel
Feb 20 at 16:21
Hanlon's Razor suggests that the author of the test mixed up "declaration" and "definition".
– Sneftel
Feb 20 at 16:21
|
show 18 more comments
12 Answers
12
active
oldest
votes
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.
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 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.
– Peter A. Schneider
Feb 22 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.
– Lightness Races in Orbit
Feb 23 at 13:23
2
Any exam question that's unclear to someone who knows the content being tested for is bugged.
– Nat
Feb 25 at 20:47
2
Sounds like we need to add an undefined behavior clause to the English language
– Nick Mertin
Feb 26 at 19:52
add a comment |
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;
.)
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 at 18:41
Also considerint 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.
– David Hammen
Feb 23 at 22:23
1
@DavidHammen: That doesn't strictly declare functions other thanint f(stuff)
. Even in the scope of the function,g
is an automatic variable of type reference to function, andh
is a pointer to function.
– Peter Cordes
Feb 24 at 2:14
add a comment |
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. :-)
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Feb 23 at 14:28
add a comment |
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.
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
– Luca Corsini
Feb 20 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 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 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 at 19:17
add a comment |
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.
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.
– Marc van Leeuwen
Feb 22 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. ;)
– Quuxplusone
Feb 23 at 6:40
@Quuxplusone if the compiler rejects your program, your program doesn't have any function declarations in it :)
– Ben Millwood
Feb 24 at 6:04
add a comment |
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)
{
// ...
}
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 at 8:22
add a comment |
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 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..
3
Nice. Ending that sentence with an extra period. I see what you did there.
– David S
Feb 22 at 13:43
2
int func() int a=42;
doesn't compile. You need a comma, not anotherint
. 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.
– Peter Cordes
Feb 22 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.
– Nick Gammon
Feb 22 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...
– Eric Lippert
Feb 25 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!
– Eric Lippert
Feb 25 at 22:58
add a comment |
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.
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 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 at 15:56
@DavidS: In C++, function declarations are always prototypes (or definitions), andvoid func();
is exactly equivalent tovoid func(void);
. This is very different from C, wherevoid func();
doesn't tell the compiler anything about the args, and isn't the same thing asvoid 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 toint
. Same rules as for args to variadic functions.)
– Peter Cordes
Feb 22 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 at 10:13
add a comment |
You can use ;
for prototypes only.
add a comment |
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.
add a comment |
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.
add a comment |
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.
Declarations are not statements though.
– HolyBlackCat
18 hours ago
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54790795%2fisnt-a-semicolon-needed-after-a-function-declaration-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
12 Answers
12
active
oldest
votes
12 Answers
12
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
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 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.
– Peter A. Schneider
Feb 22 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.
– Lightness Races in Orbit
Feb 23 at 13:23
2
Any exam question that's unclear to someone who knows the content being tested for is bugged.
– Nat
Feb 25 at 20:47
2
Sounds like we need to add an undefined behavior clause to the English language
– Nick Mertin
Feb 26 at 19:52
add a comment |
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.
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 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.
– Peter A. Schneider
Feb 22 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.
– Lightness Races in Orbit
Feb 23 at 13:23
2
Any exam question that's unclear to someone who knows the content being tested for is bugged.
– Nat
Feb 25 at 20:47
2
Sounds like we need to add an undefined behavior clause to the English language
– Nick Mertin
Feb 26 at 19:52
add a comment |
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.
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.
edited Feb 20 at 16:23
Sneftel
25.1k64481
25.1k64481
answered Feb 20 at 16:17
jwismarjwismar
10.8k32439
10.8k32439
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 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.
– Peter A. Schneider
Feb 22 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.
– Lightness Races in Orbit
Feb 23 at 13:23
2
Any exam question that's unclear to someone who knows the content being tested for is bugged.
– Nat
Feb 25 at 20:47
2
Sounds like we need to add an undefined behavior clause to the English language
– Nick Mertin
Feb 26 at 19:52
add a comment |
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 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.
– Peter A. Schneider
Feb 22 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.
– Lightness Races in Orbit
Feb 23 at 13:23
2
Any exam question that's unclear to someone who knows the content being tested for is bugged.
– Nat
Feb 25 at 20:47
2
Sounds like we need to add an undefined behavior clause to the English language
– Nick Mertin
Feb 26 at 19:52
10
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 at 18:55
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 at 18:55
16
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.
– Peter A. Schneider
Feb 22 at 14:44
@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.
– Peter A. Schneider
Feb 22 at 14:44
6
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.
– Lightness Races in Orbit
Feb 23 at 13:23
@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.
– Lightness Races in Orbit
Feb 23 at 13:23
2
2
Any exam question that's unclear to someone who knows the content being tested for is bugged.
– Nat
Feb 25 at 20:47
Any exam question that's unclear to someone who knows the content being tested for is bugged.
– Nat
Feb 25 at 20:47
2
2
Sounds like we need to add an undefined behavior clause to the English language
– Nick Mertin
Feb 26 at 19:52
Sounds like we need to add an undefined behavior clause to the English language
– Nick Mertin
Feb 26 at 19:52
add a comment |
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;
.)
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 at 18:41
Also considerint 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.
– David Hammen
Feb 23 at 22:23
1
@DavidHammen: That doesn't strictly declare functions other thanint f(stuff)
. Even in the scope of the function,g
is an automatic variable of type reference to function, andh
is a pointer to function.
– Peter Cordes
Feb 24 at 2:14
add a comment |
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;
.)
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 at 18:41
Also considerint 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.
– David Hammen
Feb 23 at 22:23
1
@DavidHammen: That doesn't strictly declare functions other thanint f(stuff)
. Even in the scope of the function,g
is an automatic variable of type reference to function, andh
is a pointer to function.
– Peter Cordes
Feb 24 at 2:14
add a comment |
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;
.)
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;
.)
answered Feb 20 at 17:35
Arne VogelArne Vogel
4,97521326
4,97521326
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 at 18:41
Also considerint 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.
– David Hammen
Feb 23 at 22:23
1
@DavidHammen: That doesn't strictly declare functions other thanint f(stuff)
. Even in the scope of the function,g
is an automatic variable of type reference to function, andh
is a pointer to function.
– Peter Cordes
Feb 24 at 2:14
add a comment |
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 at 18:41
Also considerint 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.
– David Hammen
Feb 23 at 22:23
1
@DavidHammen: That doesn't strictly declare functions other thanint f(stuff)
. Even in the scope of the function,g
is an automatic variable of type reference to function, andh
is a pointer to function.
– Peter Cordes
Feb 24 at 2:14
2
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 at 18:41
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 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.– David Hammen
Feb 23 at 22:23
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.– David Hammen
Feb 23 at 22:23
1
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.– Peter Cordes
Feb 24 at 2:14
@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.– Peter Cordes
Feb 24 at 2:14
add a comment |
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. :-)
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Feb 23 at 14:28
add a comment |
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. :-)
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Feb 23 at 14:28
add a comment |
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. :-)
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. :-)
edited Feb 21 at 2:10
Peter Mortensen
13.8k1987113
13.8k1987113
answered Feb 20 at 22:05
Eric LippertEric Lippert
546k14610671951
546k14610671951
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Feb 23 at 14:28
add a comment |
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Feb 23 at 14:28
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Feb 23 at 14:28
Comments are not for extended discussion; this conversation has been moved to chat.
– Samuel Liew♦
Feb 23 at 14:28
add a comment |
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.
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
– Luca Corsini
Feb 20 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 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 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 at 19:17
add a comment |
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.
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
– Luca Corsini
Feb 20 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 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 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 at 19:17
add a comment |
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.
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.
edited Mar 18 at 19:29
Peter Mortensen
13.8k1987113
13.8k1987113
answered Feb 20 at 16:23
Luca CorsiniLuca Corsini
514313
514313
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
– Luca Corsini
Feb 20 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 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 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 at 19:17
add a comment |
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
– Luca Corsini
Feb 20 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 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 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 at 19:17
3
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
– Luca Corsini
Feb 20 at 16:27
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
– Luca Corsini
Feb 20 at 16:27
11
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 at 16:35
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 at 16:35
1
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 at 19:17
@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 at 19:17
2
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 at 19:17
... 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 at 19:17
add a comment |
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.
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.
– Marc van Leeuwen
Feb 22 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. ;)
– Quuxplusone
Feb 23 at 6:40
@Quuxplusone if the compiler rejects your program, your program doesn't have any function declarations in it :)
– Ben Millwood
Feb 24 at 6:04
add a comment |
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.
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.
– Marc van Leeuwen
Feb 22 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. ;)
– Quuxplusone
Feb 23 at 6:40
@Quuxplusone if the compiler rejects your program, your program doesn't have any function declarations in it :)
– Ben Millwood
Feb 24 at 6:04
add a comment |
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.
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.
edited Feb 22 at 14:35
answered Feb 22 at 14:15
Marc van LeeuwenMarc van Leeuwen
2,5491630
2,5491630
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.
– Marc van Leeuwen
Feb 22 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. ;)
– Quuxplusone
Feb 23 at 6:40
@Quuxplusone if the compiler rejects your program, your program doesn't have any function declarations in it :)
– Ben Millwood
Feb 24 at 6:04
add a comment |
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.
– Marc van Leeuwen
Feb 22 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. ;)
– Quuxplusone
Feb 23 at 6:40
@Quuxplusone if the compiler rejects your program, your program doesn't have any function declarations in it :)
– Ben Millwood
Feb 24 at 6:04
1
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.
– Marc van Leeuwen
Feb 22 at 14:35
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.
– Marc van Leeuwen
Feb 22 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. ;)
– Quuxplusone
Feb 23 at 6:40
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. ;)
– Quuxplusone
Feb 23 at 6:40
@Quuxplusone if the compiler rejects your program, your program doesn't have any function declarations in it :)
– Ben Millwood
Feb 24 at 6:04
@Quuxplusone if the compiler rejects your program, your program doesn't have any function declarations in it :)
– Ben Millwood
Feb 24 at 6:04
add a comment |
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)
{
// ...
}
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 at 8:22
add a comment |
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)
{
// ...
}
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 at 8:22
add a comment |
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)
{
// ...
}
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)
{
// ...
}
edited Feb 21 at 13:37
L. F.
1,194418
1,194418
answered Feb 21 at 7:31
Rocx En RuffRocx En Ruff
892
892
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 at 8:22
add a comment |
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 at 8:22
10
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 at 8:22
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 at 8:22
add a comment |
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 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..
3
Nice. Ending that sentence with an extra period. I see what you did there.
– David S
Feb 22 at 13:43
2
int func() int a=42;
doesn't compile. You need a comma, not anotherint
. 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.
– Peter Cordes
Feb 22 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.
– Nick Gammon
Feb 22 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...
– Eric Lippert
Feb 25 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!
– Eric Lippert
Feb 25 at 22:58
add a comment |
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 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..
3
Nice. Ending that sentence with an extra period. I see what you did there.
– David S
Feb 22 at 13:43
2
int func() int a=42;
doesn't compile. You need a comma, not anotherint
. 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.
– Peter Cordes
Feb 22 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.
– Nick Gammon
Feb 22 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...
– Eric Lippert
Feb 25 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!
– Eric Lippert
Feb 25 at 22:58
add a comment |
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 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..
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 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..
answered Feb 22 at 7:01
Nick GammonNick Gammon
862519
862519
3
Nice. Ending that sentence with an extra period. I see what you did there.
– David S
Feb 22 at 13:43
2
int func() int a=42;
doesn't compile. You need a comma, not anotherint
. 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.
– Peter Cordes
Feb 22 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.
– Nick Gammon
Feb 22 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...
– Eric Lippert
Feb 25 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!
– Eric Lippert
Feb 25 at 22:58
add a comment |
3
Nice. Ending that sentence with an extra period. I see what you did there.
– David S
Feb 22 at 13:43
2
int func() int a=42;
doesn't compile. You need a comma, not anotherint
. 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.
– Peter Cordes
Feb 22 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.
– Nick Gammon
Feb 22 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...
– Eric Lippert
Feb 25 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!
– Eric Lippert
Feb 25 at 22:58
3
3
Nice. Ending that sentence with an extra period. I see what you did there.
– David S
Feb 22 at 13:43
Nice. Ending that sentence with an extra period. I see what you did there.
– David S
Feb 22 at 13:43
2
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.– Peter Cordes
Feb 22 at 19:32
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.– Peter Cordes
Feb 22 at 19:32
1
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.
– Nick Gammon
Feb 22 at 19:44
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.
– Nick Gammon
Feb 22 at 19:44
1
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...
– Eric Lippert
Feb 25 at 22:56
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...
– Eric Lippert
Feb 25 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!
– Eric Lippert
Feb 25 at 22:58
... (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!
– Eric Lippert
Feb 25 at 22:58
add a comment |
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.
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 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 at 15:56
@DavidS: In C++, function declarations are always prototypes (or definitions), andvoid func();
is exactly equivalent tovoid func(void);
. This is very different from C, wherevoid func();
doesn't tell the compiler anything about the args, and isn't the same thing asvoid 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 toint
. Same rules as for args to variadic functions.)
– Peter Cordes
Feb 22 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 at 10:13
add a comment |
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.
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 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 at 15:56
@DavidS: In C++, function declarations are always prototypes (or definitions), andvoid func();
is exactly equivalent tovoid func(void);
. This is very different from C, wherevoid func();
doesn't tell the compiler anything about the args, and isn't the same thing asvoid 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 toint
. Same rules as for args to variadic functions.)
– Peter Cordes
Feb 22 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 at 10:13
add a comment |
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.
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.
edited Mar 18 at 19:32
Peter Mortensen
13.8k1987113
13.8k1987113
answered Feb 22 at 13:09
OpifexOpifex
772
772
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 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 at 15:56
@DavidS: In C++, function declarations are always prototypes (or definitions), andvoid func();
is exactly equivalent tovoid func(void);
. This is very different from C, wherevoid func();
doesn't tell the compiler anything about the args, and isn't the same thing asvoid 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 toint
. Same rules as for args to variadic functions.)
– Peter Cordes
Feb 22 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 at 10:13
add a comment |
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 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 at 15:56
@DavidS: In C++, function declarations are always prototypes (or definitions), andvoid func();
is exactly equivalent tovoid func(void);
. This is very different from C, wherevoid func();
doesn't tell the compiler anything about the args, and isn't the same thing asvoid 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 toint
. Same rules as for args to variadic functions.)
– Peter Cordes
Feb 22 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 at 10:13
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 at 13:41
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 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 at 15:56
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 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.)– Peter Cordes
Feb 22 at 19:36
@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.)– Peter Cordes
Feb 22 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 at 10:13
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 at 10:13
add a comment |
You can use ;
for prototypes only.
add a comment |
You can use ;
for prototypes only.
add a comment |
You can use ;
for prototypes only.
You can use ;
for prototypes only.
edited Mar 18 at 19:33
Peter Mortensen
13.8k1987113
13.8k1987113
answered Feb 25 at 14:52
Dev. MahmoodDev. Mahmood
678
678
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Mar 18 at 19:36
Peter Mortensen
13.8k1987113
13.8k1987113
answered Mar 5 at 15:19
dark_3nergydark_3nergy
628
628
add a comment |
add a comment |
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.
add a comment |
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.
add a comment |
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.
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.
edited Mar 21 at 1:39
DylanWoods
212
212
answered Mar 6 at 21:58
shiv shahshiv shah
515
515
add a comment |
add a comment |
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.
Declarations are not statements though.
– HolyBlackCat
18 hours ago
add a comment |
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.
Declarations are not statements though.
– HolyBlackCat
18 hours ago
add a comment |
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.
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.
edited Mar 19 at 11:20
answered Mar 3 at 17:08
JatinderJatinder
646
646
Declarations are not statements though.
– HolyBlackCat
18 hours ago
add a comment |
Declarations are not statements though.
– HolyBlackCat
18 hours ago
Declarations are not statements though.
– HolyBlackCat
18 hours ago
Declarations are not statements though.
– HolyBlackCat
18 hours ago
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54790795%2fisnt-a-semicolon-needed-after-a-function-declaration-in-c%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
40
A definition is also a declaration. But I would say your answer was correct.
– Neil Butterworth
Feb 20 at 16:16
213
It's a tricky nitpicking question and has no bearing on anyone's ability to program well.
– phonetagger
Feb 20 at 16:18
40
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."? :/
– Algirdas Preidžius
Feb 20 at 16:20
18
@phonetagger All this confusion goes to show how badly worded the question is.
– François Andrieux
Feb 20 at 16:20
34
Hanlon's Razor suggests that the author of the test mixed up "declaration" and "definition".
– Sneftel
Feb 20 at 16:21