- [Narrator] In C+ plus it's possible to have
different functions with the same name.
The compiler selects which function to call,
based on the entire function signature.
Not just the function name.
This is sometimes called function name overloading.
And this behavior is different
than in C in many other languages,
where each function must have a unique name.
Here is func-overload.cpp from chapter 5
of the exercise files.
In C+ plus, the function signature,
consists of the return type,
the name
and a list of parameter types.
For example, this function is volume returning double
and taking one double argument.
Whereas the second one is volume returning double
taking, two double arguments.
And this third one, is volume returning double
and taking three double arguments.
And so you'll notice
when we call them down here in main,
I'm calling them with one double argument,
with two double arguments
and, with three double arguments.
For the volume of a cube,
the volume of a cylinder,
and the volume of a cuboid.
And so, when I build and run this,
you'll notice that each of these functions is called,
this one says cube of, cylinder of, cuboid of.
And so you can see where each of them is called,
and, the compiler figures out which one to call,
based on, the number and types of arguments,
because that's really the only place
where these three functions are different.
So, there are some rules to this,
the return type, cannot be the only distinguishing factor,
but the constantness of the return type can be,
and often is.
And there's rules about
how types are promoted to distinguished parameters.
So, it's a good idea to test
your overloaded functions thoroughly.
This is one area where C+ plus differs
from C significantly.
In C, a function is distinguished only by its name,
and this example would not compile.
In C+ plus, functions are distinguished by their signatures.
So, it's possible to have multiple functions
with the same name,
that operate with different parameters
and with different return types.