Declare and define a function that swaps two integer numbers:
void swap(int &n1, int &n2)
Declare and define an overloaded function that swaps two double values:
void swap(double &d1, double &d2)
In the main() function, call the first function by passing two integer varaibles and display the values of these two integer variables before and after calling the function. Then call the second function by passing two double-typed variables and display the values of these two double-typed variables before and after calling the function.
I'm confused on what this is adding me.
Here is some code I have so far, can someone help.
No, you can't have several variables with the same name.
double n1 would conflict with int n1
Because in the function the double values are n1, n2?
The names don't have to be the same. It copies the values/memory addresses into the function. Within the function, you'll use the new name.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
void m(double &n1) //Use the variable name that is here within the function
n1 = 2; //even though a different NAME, this has the memory address of "max"
double max = 100;
m(max); //Give the function "max"
std::cout << max; //outputs 2
Since it's "double &n1", the variable "n1" will have the same memory address as "max". So when you change one, you change the other. If the "&" was not there, "n1" would have a COPIED value of the variable "max". So changing one would NOT change the other.