Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write the definition of a function named swapints that is passed two int variabl

ID: 3641294 • Letter: W

Question

Write the definition of a function named swapints that is passed two int variables. The function returns nothing but exchanges the values of the two variables. So, if j and k have (respectively) the values 15 and 23, and the invocation swapint(j,k) is made, then upon return, the values of j and k will be 23 and 15 respectively.

I know that the beginning of the definition goes like this:
void swapints (int j, int k)
{

}

But I don't understand how to do what the question is asking. I missed my first class the other day and this is my fault.

Explanation / Answer

please rate - thanks

when you use reference parameters, which are indicated by the &. you pass the address of a variable, not the value of the variable. when you change a value of a reference parameter in a function, you change the value of the actual variable, not the copy

#include <iostream>
using namespace std;
void swapint(int&,int&);
int main()
{ int j=15,k=23;
swapint(j,k);
cout<<j<<" "<<k<<endl;
system("pause");
return 0;
}
void swapint(int& i,int& j)
{int t;
t=j;
j=i;
i=t;
}