Function/Variable Scope   «Prev  Next»
Lesson 2 Function prototypes
Objective Re-code C program which uses traditional C function syntax

C++ Function Prototypes

The syntax of functions in C++ inspired the function prototype syntax found in ANSI C. A C++ function lists the types of the function's parameters inside the header parentheses. Explicitly listing the type and number of arguments makes strong type checking and assignment-compatible conversions possible.
This list of a function's arguments in the header parentheses is called the function's signature.

Example

Let us consider a function min that determines the minimum of two numbers. It's prototype would be:
int min(int, int);



Both the function return type and the argument list types are explicitly mentioned.
The definition of min() that occurs in the file must match this declaration.
The function prototype can also include the identifier names of the arguments. In the case of min(), this would be:
int min(int x, int y);

C++ also allows unspecified argument lists.

Function Prototypes - Exercise

Click the Exercise link below to re-code a C program, which uses traditional C function syntax, as a C++ program that uses a proper function prototype.
Function Prototypes - Exercise