1

I have seen other questions related to this topic, but haven't really found an answer to the following simple problem:

VB Code:

Function f_x(ByRef x As Integer)

    x = x + 1

End Function

Sub test()

    Dim w As Integer
    w = 2

    Call f_x(w)

    MsgBox w

End Sub

The output above is 3, whereby the variable "w" is modified through the pointer "x" inside the function "F_x()" (i.e. "by reference").

Can I write a similar function in Python, which modifies a single numerical variable through a pointer (i.e. "by reference")? I understand that a list or a Numpy array will be modified (automatically) by reference when passed to a function, but what about a single numerical variable?

EDIT: as per suggestion below, I am adding my attempt to code this in Python (which obviously doesn't work):

def test_function(y):
    y = y + 1

x = 2 
test_function(x) 
print(x)

The output above is 2, not 3.

Edit 2: why on earth would anyone bother with choosing whether to pass a numerical variable by reference (through a pointer) or by value? What if the task is to write a computationally efficient code and one is dealing with large floating point numbers: here, a pointer ("by reference") will only need to store the memory address, whilst "by value" approach will have to "copy" the entire variable inside the function.

Jan Stuller
  • 177
  • 3
  • 8
  • 1
    You should try to write it in Python and come here and ask when it doesn't work. Not post a VB code tagged as Python... – Tomerikoo Jun 15 '20 at 09:59
  • No, **python does not support call by reference**. – juanpa.arrivillaga Jun 15 '20 at 10:02
  • @Tomerikoo: ok I will do an edit to show my attempt. – Jan Stuller Jun 15 '20 at 10:03
  • I appreciate the question has been marked as duplicate: I have seen the other thread to which my question has been linked, but the thread suggests passing a tuple and other ways to go about the problem: it doesn't really give a solution to modifying a single variable by reference. That's why I asked a "new" question... – Jan Stuller Jun 15 '20 at 10:09
  • 2
    @JanStuller The simple answer is just "No, it's not possile". Python does not support something like VB's "ByRef ". – sloth Jun 15 '20 at 11:25
  • 1
    Because someone removed it, here is the duplicate: https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference – Tomerikoo Jun 15 '20 at 13:50
  • 1
    Python doesn't support call by ref, but you could have a wrapper object for your variable. You can then pass the object to your function and the function has to modify your value. E.G. test_function(obj) where test_function executes: obj.y += 1 – DerHamm Jun 15 '20 at 14:13
  • Why was this re-opened? "whilst "by value" approach will have to "copy" the entire variable inside the function." **Python isn't call by value**. Did you **read the linked duplicate when I closed it originally**? Python does not copy an object when you pass it. – juanpa.arrivillaga Jun 15 '20 at 17:12

1 Answers1

4

You could put your variable in a mutable object like a dict:

def test_function(y):
    y['x'] = y['x'] + 1

d = {'x': 2} 
test_function(d) 
print(d['x'])

Primitive types are immutable.

JoKing
  • 430
  • 3
  • 11