Skip to main content
GameDev.net gamedev.net
🔒 Locked

The ;

Started by Tree Penguin Jan 9, 2004 at 4:36 AM 1 replies 400+ views
Original Post
Tree Penguin
Tree Penguin
Hi, as i haven''t had any real education in c++ i wondered what''s the point about the ; after functions, as they don''t seem to behave any different if you just don''t put them there. Has it something to do with automatically deleting memory allocated? I really wouldn''t know. Same question for classes (however i know you can do something i think is equal to a typedef after the class end (the }) and before the ; after that). Thanks, TP
Oluseyi
Oluseyi
quote:
Original post by Tree Penguin
Hi, as i haven't had any real education in c++ i wondered what's the point about the ; after functions, as they don't seem to behave any different if you just don't put them there. Has it something to do with automatically deleting memory allocated? I really wouldn't know.
There is no semicolon (; ) after functions, only after function declarations:
int myfunction(int); /* prototype */
 
int myfunction(int i) /* implementation */
{
return i * 42; /* multiples of "the answer" */
}

Note that I didn't have to name the parameter in the prototype; the compiler uses prototypes to match function invocations on the basis of their formal parameter lists and overall signatures (signature = formal parameter list + function name + return type). This is necessary because C requires forward declaration:
/* Forward declaration introduces a function whose definition may be supplied later */
int max(int, int);
 
int my2ndfunction(int i, int j)
{
return max(i, j);
}

In the above example, I call a function whose body has not been supplied. You may recognize max as being a Standard Library function; forward declarations are what header files provide.

[Extra point: C is a block-structured language, and the single statement (a valid expression terminated by a semicolon), certain complex expressions such as if and do...while, and the statement block (a sequence of statements enclosed in curly braces - {}) fulfill this block structure. This is why braces are optional on single-statement-bodied if and while statements.]

Note that a forward declaration does not mean that you must have a separate prototype. If your entire function body is defined (be aware of the distinction between declaration and definition/initialization: declaration introduces a variable - function or simple/aggregate type - without a value while definition - for functions - and initialization - for simple types - provides the value to match) then you can skip the prototype:
/* Function is prototyped and declared in one */
double py_distance(double v1x, double v1y, double v2x, double v2y)
{
return sqrt(v1x * v2x + v1y * v2y);
}
 
double max_py_distance(double * vert_pair1, double * vert_pair2)
{
double v11x = vert_pair1[0],
v11y = vert_pair1[1],
v12x = vert_pair1[2],
v12y = vert_pair1[3];
double v21x = vert_pair2[0],
v21y = vert_pair2[1],
v22x = vert_pair2[2],
v22y = vert_pair2[3];
double d1 = py_distance(v11x, v11y, v12x, v12y),
d2 = py_distance(v21x, v21y, v22x, v22y);
if(d1 > d2)
return d1;
return d2;
}

max_py_distance calls py_distance, though there is no prototype for it. It's unnecessary, since the entire function definition is right there.

quote:
Same question for classes (however i know you can do something i think is equal to a typedef after the class end (the }) and before the ; after that).
It's a hold-over from C. A type (struct) definition in C has a rather involved but powerful syntax, roughly:
struct [struct_tag] struct_id
{
struct_member_list
} [struct_instance];

...if I recall correctly. typedef can be used to alias this to a simpler name, making the use of the struct keyword on every instance declaration unnecessary. This syntax allows for the easy creation of single-instance types:
struct X_type
{
members...
} x;
...
x.member;

However, to safely determine the end of the type declaration, the semicolon is necessary. Since C++ retains C syntax, and makes structs a variant of class (defaults to public inheritance and visibility), it also retains these features.

[NOTE: Java doesn't require a closing semicolon on a class declaration because it doesn't support this syntax. Being a strictly OO language - as much as that is possible; anything can be circumvented - it requires that objects be created using the keyword new while C and C++ allow for static declaration of objects.]

I think that should do it!

[Edit: unintentional smiley.]

[edited by - Oluseyi on January 9, 2004 6:07:54 AM]
Tree Penguin
Tree Penguin
Thanks for the information, most of it i already new but the info on structs was handy!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.