473,395 Members | 1,677 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

a little syntax goes a long way??

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
6 1516
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
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

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

"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Istvan Albert | last post by:
Paul McGuire wrote: > Please reconsider the "def f() :" construct. Instead of > invoking a special punctuation character, it uses context and placement, > with familiar old 's, to infuse the...
7
by: Steven Bethard | last post by:
So here's the state of the decorator debate as I see it: *** Location GvR pretty strongly wants decorators before the function: ...
3
by: mheydman | last post by:
I apologize if this has been asked before- I searched google but could not find a concrete answer. I recently inherited a database whose t-sql code is written in a format that I find difficult...
8
by: Usman | last post by:
Huy everyone , Well I am not a big C++ programmer , I am just a little young kid on it tryint to learn . Actually I was given an assignment last week by my teacher which I solved ...
4
by: ben | last post by:
getting a bit confused with the details of how c's grammar is specified, especially when you get self-reference like in this: postfix-expression: primary-expression postfix-expression ...
23
by: Marcin Grzębski | last post by:
I red MSDN article of C# 2.0 this week... and i found very strange syntax for properties e.g.: public int MyIntValue { get { // ... } protected set { // ... }
2
by: Daniel | last post by:
I'm new to .Net and all of its abilities so I hope this makes sense. Basically I'm confused on when is the appropriate time to use web forms controls vs. regular HTML. For example in ASP...
44
by: petermichaux | last post by:
Hi, I have been using the following line of code to create an object called "Serious" if it doesn't already exist. if (Serious == null) {var Serious = {};} This works in the scripts I use...
7
by: Guillaume Dargaud | last post by:
Hello all, I have an example of working code under my eyes that goes as follow: unsigned long address=0x400000; (void (*)(void)address)(); It's supposed to jump start a kernel loaded at that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.