OOPortal 





Function/Variable Scope   «Prev 

Unspecified Argument Lists

C++ uses the ellipsis symbol ( ... ) to show that a function's argument list is unspecified. For example, the stdio.h function printf() is declared as the prototype:

int printf(const char* cntrl_str, ...);
Such a function can be invoked on an arbitrary list of actual parameters.
This practice should be avoided, however, because it can cause type-safety issues.



Difference between C and C++ Functions

There is a significant difference between C and C++ for functions with empty argument lists. In C, the declaration:
int func2();

This means a function with any number and type of argument.
This prevents type-checking, so in C++ it means "a function with no arguments."

Function definitions

Function definitions look like function declarations except that they have bodies. A body is a collection of statements enclosed in braces. Braces denote the beginning and ending of a block of code. To give func1( ) a definition that is an empty body (a body containing no code), write:
int func1(int length, int width) { }

Notice that in the function definition, the braces replace the semicolon. Since braces surround a statement or group of statements, you do not need a semicolon. Notice also that the arguments in the function definition must have names if you want to use the arguments in the function body (since they are never used here, they are optional).