answersLogoWhite

0

Functions are declared by specifying the function return type (which includes any use of the const qualifier), the name of the function, and the function's argument types enclosed in parentheses (round brackets), which also includes any use of the const qualifier. The declaration ends with a semi-colon. Arguments may also be given default values but if any argument has a default value, all arguments that follow must also have default values.

[const] type name([[const] type [=value][, ...]]);

Declarations may also contain definitions in which case you must include the formal argument names that will be used by the definition. The definition (or implementation) must be enclosed in braces (curly brackets) and the semi-colon must be omitted.

[const] type name([[const] type arg1 [=value][, ...]])

{

statement;

}

Functions must be declared before they can be used. In most cases it is necessary to forward declare functions in a header. In these cases, default values must be omitted from the definition.

Functions that do not return a value must return void. Functions that do not accept arguments must still include the argument parentheses. Use of the void keyword to indicate no arguments is optional. Thus the following declarations are exactly the same (although declaring both in the same program would be invalid):

void f();

void f(void);

As well as the return type and argument types, class member functions (methods) can also be modified with the const qualifier:

struct obj

{

void f(void) const;

};

The const keyword assures the caller that the object's immutable members will not be modified by the function (that is, the internal state of the object's immutable data will remain unaltered). Also, when working with constant objects, only constant methods can be called.

Functions can also be declared static. In the case of external functions, the static keyword limits the visibility of the function to its translation unit. In the case of class methods, static member functions are local to the class as opposed to instances of the class. Static member functions do not have access to an implicit this pointer since they are not associated with any instance of the class but are accessible even when no instances of the class exist.

User Avatar

Wiki User

11y ago

What else can I help you with?