The purpose of a function prototype in C++ is to tell the compiler
- what the name of the function is,
- what the types of its arguments are, and
- what type (if any) it returns.
An important detail here is that the above list does not include the names of the parameters. For example, all of the following pieces of code declare the same function:
int myFunction(int a, double b);
int myFunction(int apples, double bananas);
int myFunction(int pizkwat, double zyzzyzyplyz);
Moreover, the actual implementation of myFunction
wouldn't be required to use any of the names given here. For example, we could write
int myFunction(int snickerdoodle, double alfajores) {
// ... do something ... //
}
and the compiler would be perfectly happy with it, even though none of the above prototypes use those names.
Now, for a weird bit of C++ trivia: in C++, it's possible to declare functions that have parameters that don't have names. For example, this code is perfectly legal:
void doSomething(int) {
// This function takes in an integer. You can't call it without
// passing in that integer. However, this function has no way of
// referencing its argument, because it doesn't have a name!
}
You do see this used in practice. For example, overloading the postfix ++
operator requires declaring a function named operator++
that takes in an int
whose value is essentially never used. Or, you might be overriding a function in a derived class and not need to use the arguments provided.
This means that we have three rules in C++:
Rule One: The names of parameters in function prototypes are ignored.
Rule Two: A function implementation doesn't have to pick the same names for its parameters as its prototype.
Rule Three: Parameters don't even need to have names at all.
By combining these three rules together, you sometimes see things like this:
int myFunction(int, double); // Prototype gives no names to arguments
int myFunction(int a, double b) {
// .. use parameters a and b ... //
}
Here, the function prototype doesn't give names to its parameters, but that's okay! C++ still learns everything it needs to about the function (name, return type, and argument types). This is legal because of Rule One and Rule Three. In the implementation, which does actually need to reference the parameters, those parameters are given names a
and b
. The fact that these parameters now have names is fine due to Rule Two.
So, returning to your example, the function prototype
string ltrim(const string &);
means "I'm declaring a function named ltrim
. It takes in a reference to a const string
, and it returns a string
." It's completely equivalent to saying
string ltrim(const string& toTrim);
or
string ltrim(const string& oompaLoompa);
The actual implementation of ltrim
, by the looks of it, almost certainly names its parameter so that it can reference it and trim the string.
Hope this helps!