I copied this function from a C guide but i don't know why the function type is void and not int or another one.
1 2 3 4 5 6 7 8 9
void swap(int *a, int *b) {
int tmp;
// tmp conterrĂ il contenuto della variabile intera puntata da a
tmp=*a;
// a conterrĂ il contenuto della variabile intera puntata da b
*a=*b;
// b conterrĂ il contenuto della variabile intera puntata da tmp
*b=tmp;
}
Because there is nothing for the subroutine to return.
You pass the address of the pointers to where the numbers are in memory.
You then swap the values in those memory locations.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
usingnamespace std;
void swap(int *a, int *b) {
int tmp;
tmp=*a;
*a=*b;
*b=tmp;
}
int main()
{
int x = 5;
int y = 7;
swap (&x,&y);
cout << x << " " << y << endl;
return 0;
}