Sign in to follow this  
NUCLEAR RABBIT

Overloading Functions

Recommended Posts

Hello, In my book, im learning about function overloading, and I came across a paragragh which does not make sense too me. Can somebody please explain to me what it means? overloading functions: To implement function overloading, you need to write multiple definitions for the same functions with different parameter lists. Notice i didnt mention anything about return types. Thats because if you write two definitions in which only the return type is different, you'll generate a compiler error. For example, you cannot have both of the following prototypes in a program.
int bonus(int);

float bonus(int);
Please, can someone please explain what this means. I understand that to overload functions, you write a function with the same name, but with a different type, but I dont understand what its trying to say. [sad] Is it saying that if the function type is int, the arguments must be int's as well? For example, the argument in the second prototype should be a float, not an int?

Share this post


Link to post
Share on other sites
It's trying to say that if you two functions with the same name and the same parameters types, but different return types, then that's not legal.

int bonus(int); // named bonus, taking an int, returning an int
float bonus(int); // named bonus, taking an int, returning a float. No good


int bonus(int); // named bonus, taking an int, returning an int
float bonus(float); // named bonus, taking a float, returning a float. Just fine

Share this post


Link to post
Share on other sites
I'd just like to add, that this is also legal:

int bonus(int); // named bonus, taking an int, returning an int
int bonus(float); // named bonus, taking a float, returning a int. Just fine


Share this post


Link to post
Share on other sites
Yea generally if you want to overload a function, changing the return type is not enough. You have to change either one of the parameter types or the number of parameters.

As far as I can see, even this is good enough:

int bonus(int);
int bonus(int) const;

Share this post


Link to post
Share on other sites
Basically, what SiCrane said. But another way of putting it:
When your compiler has to do a function lookup (ie, you call a function), it looks for the function based on that functions name, and the parameters the function takes.
So, if you have
void foo(int) {}
void foo(float) {}

and call, somewhere in your code
float f;
foo(f);

The compiler will look for a function called foo which takes a float as a parameter.

With that being said, this name lookup does not include return types, so if you have:
void foo(int) {}
int foo(int) {}

And call:
int i, j;
i = foo(j);

The compiler will look for a function called foo which takes an int, find two such functions, become confused, and generate an error.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this