Variable changes value when calling function

Asked

Viewed 143 times

1

I created a variable v, who receives n random numbers. when create another variable by calling a function and passing v by parameter, the v mute.

n = int(input("Entre com o tamanho de vetor: "))
v = [0]*n
for i in range(0,n):
    v[i] = randint(0,100)
print(v)


bS=bubbleSort(v)
print(bS)
cS=countingSort(v)
print(cS)
iS=insertionSort(v)
print(iS)
mS=mergeSort(v)
print(mS)
qS=quickSort(v,0,n-1)
print(qS)
sS=selectionSort(v)
print(sS)

print(v)
  • 2

    It only changes because one (or more) of these functions you called is changing the variable. You should not change the value?

  • Are all functions changing the value? Enter the code of one of these that is changing the value.

1 answer

4


The type of data you’re using is called type by reference, so you don’t have the object directly, you have an indirect to the object. The variable has a reference to the object and does not have these values that you are placing on it directly (I will not go into specific details like Python handles this which is not the case here).

When you call the function by passing that list as argument you are passing that reference and you are not passing the object itself. So whatever is done inside the function will do on the object pointed by the reference, then a change there is reflected in the variable that was used as argument because a single object has been changed.

To pass the object it would have to be copied completely and in general is not what you want to do. Unless in this case, by the described question does not want the object to be changed, so you need to copy it manually to get the desired result.

A way to copy the list would be like this:

bS = v[:]
bS = bubbleSort(v)

I put in the Github for future reference.

You can do this as many times as you want, each one will be a different copy and each new variable will have a reference to a different object. The function will not change the behavior, it will change its object, but now each of them is independent of the other, you no longer point to the same object always.

You don’t even need to create more variables than you’ve already created, you just have to do it before by copying.

Just be clear that the copy is a costly operation if the list is too large, which is not even the most common case of the example (will be if you put a n very big, but if only does it will not be such a big problem), but it is good to know not to abuse it.

The only way this doesn’t happen is if the functions make the copy internally, which would explain having a return. If you did these functions, the problem could be there, you’re not making the copy inside. If you make the copy it would be good to document it because it will be a very "expensive" function, make a copy and a rating, and it’s not always obvious that you’re doing this, being obvious is important. Then you wouldn’t need to make the copy out. If you don’t make this copy then you have no reason to return anything.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.