May you explain the code please? why it is giving 20 as output? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 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 Answers
+ 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