In: Computer Science
Trace through the program and what the outputs will be. If there is an error explain what must be fixed and continue to trace through.
#include <iostream> using namespace std; void beans(int y, int& n, int size); void spam(int& n, int& y); int main(){ int m = 7; int n = 4; cout<<"m is "<<m<<" n is "<<n<<endl; beans(n, m, 3); cout<<"m is "<<m<<" n is "<<n<<endl; spam(m, n); cout<<"m is "<<m<<" n is "<<n<<endl; beans(m, 2, n); cout<<"m is "<<m<<" n is "<<n<<endl; return 0; } void beans(int y, int& n, int size){ y = n+size; n = 7+n; } void spam(int& n, int& y){ int m = n+y; y=n; n=y+n; }
When the function accepts a value using '&' , it is pointing to the address of variable whose value is passed. So changes will be directly reflected.
Therefore different value of m and n printed are
O/P | value of m | value of n |
1 | 7 | 4 |
2 | 2i | 14 |
3 | 28 | 14 |
If you have any questions comment down. Please don't simply downvote and leave. If you are satisfied with answer, please? upvote thanks