+ 1
Weird code
Can someone please explain the code attached? I donât understand why the id changes when reassigning list1 a literal. https://sololearn.com/compiler-playground/cbVN27aHZ9GH/?ref=app
6 Answers
+ 2
Edward Finkelstein
Just see the result of first 2 print statement. Ids are same because references are same
In 3rd print, result is different because list1 is assigned by new value.
https://sololearn.com/compiler-playground/ckFvUKJEFD9Q/?ref=app
+ 4
On line 8 list1 will be the local variable it will not affect on reference variable list1 which is passed as a parameter in function. Reference variable id will not change after assigning new variable. That's why sum is 13 but if you return that local variable and assign to list1 then print list1 output would be 18.
+ 3
If you want to dig a bit deeper into the intricacies of the reference topic, you might read this:
https://sololearn.com/compiler-playground/c89ejW97QsTN/?ref=app
+ 1
@AJ makes sense thanks
+ 1
HonFu wow, yeah this is great, thanks!
0
Yeah, I donât get it⌠Could you post an example explaining the last bit plz? I donât get what the difference between list1 and list1 đ