Question
Code and run the swap function with main as given on page 2 in the Instructor's Notes on Functions - Pass By Reference. Submit the
Code and run the swap function with main as given on page 2 in the Instructor's Notes on Functions - Pass By Reference. Submit the output with explanations about:
What happened and why?
How can you fix the function to also swap a and b in main?
Explain how pass-by-value works
Explain how pass-by-reference works
Here is the code!
#include
using namespace std;
void swap (int x, int y)
{
int hold ;
hold = x ;
x = y ;
y = hold ;
cout << At end of swap method x is << x << endl;
cout << At end of swap method y is << y << endl;
}
int main ()
{
int a = 10, b= 20;
cout << a is << a << endl;
cout << b is << b << endl;
swap (a,b);
cout << In main after swap a is << a << endl;
cout << In main after swap b is << b << endl;
}
This is in C++
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started