#include "stdafx.h"
#include <iostream>
using namespace std;
void silly(int & a, int & b)
{
a=10;
b=20;
cout << a << "" << b << endl;
}
void main()
{
int z=30;
silly(z,z);
cout << z << endl;
system("Pause");
}
The output for this is:
2020 20
Why are there 3 outputs, and why does b override a?
I'm asking because this is part of homework and I don't exactly understand the passing of variables especially strings/arrays.
You are passing the reference of z to the silly() function.
When you change the value of a or b (both references to z) in silly(), it changes the value of z.
So, at this line:
cout << a << "" << b << endl;
the value of z is 20, hence the output.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.