Need help understanding overloaded functions

I am a beginner and was using the 'Teach Yourself C++ in 24 Hours' Book. I couldnt understand how overloaded functions worked. Could someone explain by providing an answer to the following question:

Write a program that can calculate the average of two integers, two long integers
or two floating-point values using an overloaded function named
average().

Any additional help will also be very helpful..
Thanks
Last edited on
well, overloaded functions are functions with the same name, but different parameters (or types).
IE:
1
2
double calcAverage(int x, int y);
double calcAverage(float x, float y);


The compiler will choose the right function depending on the variables you pass.
Here, a template would be a better choice though.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <iostream>

int function1 (int);
int function1 (int,int); /* overloading a function means using the same name but 
different parameters (or types) like what xander said */

{
int variable1,variable2;
variable1 = 0; 
variable2 = 0;
function1(variable1); //the compiler will know you're calling the function with 1 parameter
function1(variable1,variable2); /* the compiler will know you're calling the function with 2 parameters */
return 0;
}

int function1(int variable1)
{
//code goes here
return 0;
}

int function1 (int variable1, int variable2)
{
//code goes here
return 0;
}
Last edited on
Jesus, please use code tags ;)
my apologies, I'm a bit of a newbie myself haha
Topic archived. No new replies allowed.