+ 2

Arguments and returnings

I need to know if we can modify the variable that I give to function, like this: i=0 function(i) print(i) and the output is something else. There's a way to do that?

21st Aug 2017, 10:15 PM
LuĂ­s Afonso
LuĂ­s Afonso - avatar
3 Answers
+ 13
You could have the function return the new value, and then reassign the variable with it. Ex: def function(x) #Your code return x i = 0 i = function(i) print(i)
21st Aug 2017, 10:44 PM
Tamra
Tamra - avatar
+ 13
No, Python doesn't have means of passing basic/primitive types (int, string, etc) by reference. Although it's not the best practice, returning a list of values you want and picking them apart afterward is a workaround.
22nd Aug 2017, 8:27 PM
Tamra
Tamra - avatar
+ 1
That is a possible solution but in c++ we can use pointers and in python we can do something like it?
22nd Aug 2017, 1:46 PM
LuĂ­s Afonso
LuĂ­s Afonso - avatar