Function Declarations
A function declaration tells the compiler about a function name and how to call the function. The actual body of the function can be defined separately (described later in this chapter). A function declaration has the following parts:
type
name
(parameters
)cv-qualifiers
except-spec
;
The parameters
,
cv-qualifiers
, and
except-spec
are optional. The
type
is required, except for constructors,
destructors, and type conversion operators. The
name
is the function name. (Each of these
parts is described later in this chapter.) Example 5-1 shows a variety of
function declarations.
// Function named "add", which returns type int, and takes two parameters, each // of type int. The names a and b are optional.int add(int a, int b);
// Function named "print", which takes a const string reference, and does not // return anything. The function is expanded inline.inline void print(const std::string& str)
{ std::cout << str; } // Function named "test", which takes two floating-point arguments and returns an // enumeration. This function does not throw any exceptions. enum fptest { less=-1, equal, greater };fptest test(double, double) throw( );
class demo { public: // Member function named "value", which returns type int, takes no arguments, // and is const, which means it can be called for a const object.int value( ) const;
// Function named "~demo", that is, a destructor, that is virtual. Constructors // and destructors do not have return ...
Get C++ In a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.