-2

I am learning C++ and I am a beginner. I am creating a function to receive two acquisitions and to alter and return them. Function overloading shall be used. However, an error is generated when calling a function. What should I do? And what are int* argument and int &argument? here's my script.

#include <iostream>


int swap(int val1, int val2)
{
    return val2, val1;
}

char swap(char val1, char val2)
{
    return val2, val1;
}

double swap(double val1, double val2)
{
    return val2, val1;
}

int main(void)
{
    int num1=20, num2 = 30;
    swap(&num1, &num2);
    std::cout<<num1<<' '<<num2<<std::endl;

    char ch1='A', ch2='Z';
    swap(&ch1, &ch2);
    std::cout<<ch1<<' '<<ch2<<std::endl;

    double dbl1=1.111, dbl2=5.555;
    swap(&dbl1, &dbl2);
    std::cout<<dbl1<<' '<dbl2<<std::endl;
    return 0;
}
Iłya Bursov
  • 23,342
  • 4
  • 33
  • 57
dr_coding
  • 13
  • 2
  • Please read http://en.cppreference.com/w/cpp/algorithm/swap as those functions are not happening – Ed Heal Feb 25 '18 at 07:08
  • 1
    Possible duplicate of [The Definitive C++ Book Guide and List](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list) –  Feb 25 '18 at 07:09

1 Answers1

0

Return values don't make sense here. You want to modify the actual variables that have been passed to the functions. In order to do that, the functions need to take the parameters by reference.

Declaring a reference is done by using the & character. For example:

int num = 0;
int &ref = num;

ref is reference to num. Modifying ref will modify num:

ref = 1;
cout << num; // This will print "1"

So your swap function needs to take references as parameters:

void swap(int &val1, int &val2)
{
    int tmp = val1;
    val1 = val2;
    val2 = tmp;
}

It does not need to return anything.

I know you're just practicing and learning here, but keep in mind that swapping two values is such a very common thing that the standard C++ library has a function for it already. So you should be using that at some point. The function is swap() and it's in the <utility> header:

#include <utility>
// ...

int i1 = 0, i2 = 1;
char c1 = 'a', c2 = 'b';
double d1 = 0.0, d2 = 1.0;

std::swap(i1, i2);
std::swap(c1, c2);
std::swap(d1, d2);

It's a template function so it can swap the values of variables of all built-in types.

Nikos C.
  • 50,738
  • 9
  • 71
  • 96
  • I definitely understood, but it still make error. "error : no mach function for call to 'swap(int *, int *)' – dr_coding Feb 26 '18 at 08:55
  • @dr_coding Don't pass pointers to the function. Note in the code above, it says `std::swap(i1, i2)`, **not** `std::swap(&1, &2)`. When you are **declaring** a variable, using a `&` before the name of the variable you are declaring, makes that variable a reference. When you are **evaluating** a variable, the `&` takes the **address** of the variable, meaning the result in a pointer. You should not pass pointers to a function that takes references. You just pass the variables themselves. – Nikos C. Feb 26 '18 at 08:59
  • thanks for your kindness. However, I found an answer in blog of my learning material. – dr_coding Feb 27 '18 at 10:41