In Fortran, a subroutine
can modify whatever variable(s) is fed into it (as long as it has intent(INOUT)
). Consider the code below:
program example
implicit none
integer:: i, j, k, l
i = 1
j = 2
k = 3
l = 4
call swap(i,j)
print*, i, j
call swap(k,l)
print*, k, l
contains
subroutine swap(a,b)
integer, intent(INOUT) :: a, b
integer :: aux
aux = a
a = b
b = aux
end subroutine
end program example
It outputs:
2 1
4 3
So my question is how one would accomplish the same in Python? In Python, the local variables inside functions do not affect the global ones, and defining a global my_variable
inside a function would hardcode a single variable to that function.