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.