+ 2

May you explain the code please? why it is giving 20 as output?

#include<bits/stdc++.h> using namespace std; void func(int&x) { x=20; } int main() { int x=30; func(x); cout<<x; }

10th Aug 2018, 12:03 PM
Ruhul Amin
Ruhul Amin - avatar
2 Antworten
+ 4
thank you.
10th Aug 2018, 12:19 PM
Ruhul Amin
Ruhul Amin - avatar
+ 3
Because you pass by reference. Meaning that you manipulate the variable x at the same memory address and don't create another local x in the function.
10th Aug 2018, 12:11 PM
Matthias
Matthias - avatar