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.
- C Program 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.
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.
An important distinction between C and C++ when dealing with functions that have "empty argument lists". Here's a detailed explanation:
In C: When you declare a function like this:
int func2();
It means that `func2` is a function that:
- Returns an
int
.
- Can take any number and type of arguments.
- Does not enforce type-checking for the arguments during compilation.
Example in C:
#include <stdio.h>
int func2(); // No information about arguments.
int main() {
printf("%d\n", func2(5, 10)); // Compiles fine, even if func2 takes no arguments.
return 0;
}
int func2() {
return 42;
}
This compiles without error in C, even though `func2` is called with arguments but defined without any. This behavior can lead to runtime errors because no type-checking is performed.
In C++:
When you declare a function like this:
int func2();
It means that `func2`:
- Returns an `int`.
- **Takes no arguments**.
The compiler enforces **type-checking**, and calling this function with arguments results in a compilation error.
# Example in C++:
#include
using namespace std;
int func2(); // No arguments allowed.
int main() {
cout << func2(5, 10); // Compilation error: Too many arguments.
return 0;
}
int func2() {
return 42;
}
This will result in a compilation error like:
error: too many arguments to function ‘int func2()’
Correct Way to Declare Functions in C:
To explicitly indicate that a function takes no arguments in C, you should use the `void` keyword:
int func2(void);
This ensures that the function cannot accept any arguments, and the compiler will enforce this restriction.
Aspect |
C |
C++ |
Declaration |
int func2(); |
int func2(); |
Meaning |
Function can take any number and type of arguments. |
Function takes no arguments. |
Type-checking |
No type-checking for arguments. |
Enforces type-checking for arguments. |
Explicit No Arguments Syntax |
int func2(void); |
Same as int func2(); |
Why This Matters:
- The C behavior (`int func2();`) is retained for compatibility but is generally considered dangerous because it can lead to subtle bugs due to the lack of type-checking.
- C++ enforces stricter rules, making code safer and easier to debug.
Using `void` explicitly in C is a good practice to ensure clarity and compatibility with C++ rules.
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).