5

I saw this quiz. This is Python, right?

I ran this and got '3 44' although I'm not completely sure why the 44. I can see the obvious correlation between 'values [0]' and 'v[0]'. Aside from that I'm not sure.

Can anyone explain this to me?

Comments
  • 4
    In python , lists are passed by reference. So , if you add/update/delete elements inside the function using that pointer, it will reflect outside.

    On the other hand, if you 'assign' a value inside the function, it won't update the pointer but instead it will create a new local variable.
  • 6
    Basically, it's showing that array types are passed by reference, not value, at least how it works in the C language, and what I can remember from Python. "values" in this case, passed into the function 'f' as the variable 'v' is being modified in the function f at index 0 to have the value 44. That's why when the output is shown, v[0] is 44. Sorry if this is kind of crap, doing it on my phone, and can't see the code...

    And right, my Python is rusty AF. Array can be replaced by list. Updated after reading vish's post.
  • 1
    @Lahsen2016 C++, different language.
  • 0
    @Lahsen2016 thought u were talking about & operator in function declaration. Still when you use & operator you get the address of the variable so you pass the value of the pointer, reference, but for me it still doesn't count as pass by reference.
  • 2
    Yes, I understand what you mean, but what I mean is that you pass the VALUE of the pointer, you don't pass the variable by reference.
Add Comment