Formal Parameter Declarations

From Appmethod Topics
Jump to: navigation, search

Go Up to Functions Index

The formal parameter declaration list follows a syntax similar to that of the declarators found in normal identifier declarations. Here are a few examples:

int func(void) {                   // no args
int func(T1 t1, T2 t2, T3 t3=1) {  // three simple parameters, one
                                   // with default argument
int func(T1* ptr1, T2& tref) {     // A pointer and a reference arg
int func(register int i) {         // Request register for arg
int func(char *str,...) {          /* One string arg with a variable number of other
                               args, or with a fixed number of args with varying types */

In C++, you can give default arguments as shown. Parameters with default values must be the last arguments in the parameter list. The arguments' types can be scalars, structures, unions, or enumerations; pointers or references to structures and unions; or pointers to functions, classes, or arrays.

The ellipsis (...) indicates that the function will be called with different sets of arguments on different occasions. The ellipsis can follow a sublist of known argument declarations. This form of prototype reduces the amount of checking the compiler can make.

The parameters declared all have automatic scope and duration for the duration of the function. The only legal storage class specifier is register.

The const and volatile modifiers can be used with formal parameter declarators