By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,134 Members | 1,796 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,134 IT Pros & Developers. It's quick & easy.

a little syntax goes a long way??

P: n/a
It struck me as odd today that while class definitions require a
semi-colon at the end, function definitions do not. I was curious if
this small syntactic difference represents a larger conceptual
difference between these two language constructs.

I know that a semi-colon is used to "end a statement," but why would a
function definition not be considered a statement, while a class def
is?

If there is a larger conceptual difference (other than the
unilluminating fact that one is a function and one a class), can
anyone explain what it is?

Thanks,
cpp
Jul 22 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
On Tue, 01 Jun 2004 00:51:15 GMT, cppaddict <he***@hello.com> wrote:
It struck me as odd today that while class definitions require a
semi-colon at the end, function definitions do not. I was curious if
this small syntactic difference represents a larger conceptual
difference between these two language constructs.

I know that a semi-colon is used to "end a statement," but why would a
function definition not be considered a statement, while a class def
is?

If there is a larger conceptual difference (other than the
unilluminating fact that one is a function and one a class), can
anyone explain what it is?


Because you can immediately declare an instance of a class, even while
leaving the class anonymous:

class
{
int x;
} instance;

--
Be seeing you.
Jul 22 '05 #2

P: n/a
On Tue, 01 Jun 2004 00:51:15 GMT, cppaddict <he***@hello.com> wrote:
It struck me as odd today that while class definitions require a
semi-colon at the end, function definitions do not. I was curious if
this small syntactic difference represents a larger conceptual
difference between these two language constructs.

I know that a semi-colon is used to "end a statement," but why would a
function definition not be considered a statement, while a class def
is?

If there is a larger conceptual difference (other than the
unilluminating fact that one is a function and one a class), can
anyone explain what it is?

Thanks,
cpp


Thore's reason works for me...but there's also this: The C++ Standard
defines the syntax of a function definition (8.4/1) as follows:

Function definitions have the form
function-definition:
decl-specifier-seqopt declarator ctor-initializeropt function-body
decl-specifier-seqopt declarator function-try-block

function-body:
compound-statement

Note the final "compound-statement". Compound statements don't end in
semi-colons! And the compound statement "part" of a function definition
(the "function-body") isn't the entire function definition; that's why a
function definition is "not considered a statement".
-leor
--
Leor Zolman --- BD Software --- www.bdsoft.com
On-Site Training in C/C++, Java, Perl and Unix
C++ users: download BD Software's free STL Error Message Decryptor at:
www.bdsoft.com/tools/stlfilt.html
Jul 22 '05 #3

P: n/a

"cppaddict" <he***@hello.com> wrote in message
news:le********************************@4ax.com...
It struck me as odd today that while class definitions require a
semi-colon at the end, function definitions do not. I was curious if
this small syntactic difference represents a larger conceptual
difference between these two language constructs.


I think the "conceptual difference" lies in the meaning of "definition".
Classes are kind of funny when it comes to the meaning of "definition".
When you declare a function, you use a semicolon at the end. When you
define a function, you don't. But the "definition" of a function is really
an "implementation". Note that when you actually implement the class, you
need no semicolon. So a class "definition" is really more of a
"declaration", hence the need for the semicolon.
Jul 22 '05 #4

P: n/a
cppaddict wrote:
It struck me as odd today that while class definitions require a
semi-colon at the end, function definitions do not. I was curious if
this small syntactic difference represents a larger conceptual
difference between these two language constructs.
Maybe yes, maybe no. Most likely - no.
I know that a semi-colon is used to "end a statement," but why would a
function definition not be considered a statement, while a class def
is?
From the purely syntactical point of view, the fact that a semicolon is
used to "end a statement" is not really reflected in C++ grammar.
Moreover, as Leor already pointed out, this is not always the case.

If you take a look at the relevant portion of the grammar (see 6.7/1 and
next 7/1) you'll see that no semicolons are mentioned at this
grammatical level. Even simple statements don't have "their own"
semicolons. Instead, they "inherit" them from lower levels of the
grammar. For example, semicolons after declarations come with the last
rule in 7/1 - the rule for 'simple declaration'. Note that function
definition is not a 'simple declaration', while class definition is a
'simple declaration'. That's why there are no semicolons after function
declarations.

BTW, member function definitions can be optionally followed by
semicolons, but this is again their own "personal" semicolons introduced
by a separate grammatical rule in 9.2.
If there is a larger conceptual difference (other than the
unilluminating fact that one is a function and one a class), can
anyone explain what it is?


The rationale is rather well-known. But a "larger conceptual
difference"... I don't think it exists.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #5

P: n/a

"Andrey Tarasevich" <an**************@hotmail.com> wrote in message
news:10*************@news.supernews.com...
Note that function
definition is not a 'simple declaration', while class definition is a
'simple declaration'. That's why there are no semicolons after function
declarations.


Function definition? Declaration?
Jul 22 '05 #6

P: n/a
jeffc wrote:
"Andrey Tarasevich" <an**************@hotmail.com> wrote in message
news:10*************@news.supernews.com...
Note that function
definition is not a 'simple declaration', while class definition is a
'simple declaration'. That's why there are no semicolons after function
declarations.


Function definition? Declaration?
...


In the last quoted sentence I meant to say "function definition", of
course, i.e. "function declaration, which is a definition". Hopefully,
it is clear from the context.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.