1

I really don't have a clue how to increase variables by using function with an argument. Code down below prints 0 and 1 and i understand why but i don't know how to fix it. It's just a general 'shape' of a code, so there can be any number of variables. Will appreciate if you take a look!

i=0
b=0
def increase(a):
    print(a)
    a+=1
    print(a)
for e in range (3):
    increase(i)
    increase(b)
FHTMitchell
  • 11,793
  • 2
  • 35
  • 47
Clayn
  • 17
  • 1
  • 7
  • 1
    what variable are you trying to increase and what should the output be? –  Jun 15 '18 at 15:24
  • 1
    In order to be able to modify global variable in function add `global a` at the beginning of `increase` function. Using global variables is bad for your code, bla-bla... – Andrew Morozko Jun 15 '18 at 15:24
  • 2
    @MOROZILnic - This code makes so sense at all –  Jun 15 '18 at 15:25
  • This is where you need to pass by reference – Nayuki Jun 15 '18 at 15:25
  • 1
    @Nayuki Unfortunately you can't, this is python;) – Andrew Morozko Jun 15 '18 at 15:26
  • https://stackoverflow.com/questions/986006/how-do-i-pass-a-variable-by-reference see this for an explanation of pass by value/ reference. What you're trying to do isn't possible in Python, so you'll need to restructure your code – c2huc2hu Jun 15 '18 at 15:27
  • Why was this question upvoted? –  Jun 15 '18 at 15:30
  • @Sveta Yep, didn't look thoroughly enough. – Andrew Morozko Jun 15 '18 at 15:30
  • 1
    @MOROZILnic I think you've misinterpreted what OP wants: they're looking for something like (in C): `void increase(int* a) { (*a)++; } increase(&i);` – c2huc2hu Jun 15 '18 at 15:30
  • You can achieve what you want by defining a class. As others have mentioned you don't have control over pass by reference or value and using global values is just a terrible idea – anishtain4 Jun 15 '18 at 15:31
  • @Clayn, variable `a` is a local copy of whatever you pass to the function, you can't pass the references to i or b into functions in python – Andrew Morozko Jun 15 '18 at 15:31
  • @anishtain4 Possibly wouldn't even think of it! Don't know if it is the most optimized solution but it actually solves my issue, so thanks a lot! – Clayn Jun 15 '18 at 15:47
  • I don't know what is the big picture and how you're gonna use these variables, probably there's a better way to do the whole thing. But you can make a subclass of int and add the increase function there. Look at [this question](https://stackoverflow.com/questions/3238350/subclassing-int-in-python) – anishtain4 Jun 15 '18 at 16:13

2 Answers2

3

Python does not have parameters reference like c++ ( the & sign ), so you can use a returning function.

i=0
b=0
def increase(a):
  print(a)
  a+=1
  print(a)
  return a
for e in range (3):
    i = increase(i)
    b = increase(b)
Darius Buhai
  • 334
  • 1
  • 12
  • Even though this solves the problem for one variable, but it can get very messy when multiple variables are used as OP mentioned is the case. – anishtain4 Jun 15 '18 at 15:54
-1

There's a somewhat lengthy discussion surrounding why you can't do what you're trying to do the way you're trying to do it, but the accepted answer here should be useful to you. Essentially, all arguments in Python are passed by object reference, so if you reassign the value in your function using the name of the parameter you are not changing the value pointed to by the variable you passed to your function.

What you're trying to do is possible, you just need to pass your function a mutable type, like a list. So, if you have the following code, it should do what you expect/want from your example:

i=[0]
b=[0]
def increase(a):
    a[0]+=1

for e in range (3):
    increase(i)
    increase(b)

print(i)
print(b)
MoxieBall
  • 1,907
  • 7
  • 23
  • You should probably note that your "solution" is very much **not** advisable. There's a reason python chose to make numbers immutable. – FHTMitchell Jun 15 '18 at 15:33
  • I'll add the talk [Facts and Myths about Python names and values](https://www.youtube.com/watch?v=_AEJHKGk9ns). Really recommend it for anyone struggling with "pass by name/value/reference" in python – Andrew Morozko Jun 15 '18 at 15:35
  • 1
    If Python variables *were* passed by reference then you wouldn't need to pass an immutable type. Python *never* passes by reference. – juanpa.arrivillaga Jun 15 '18 at 15:35
  • @juanpa.arrivillaga Saying that Python _never_ passes by _reference_ is misleading because it does _always_ do the same thing. However, you are right, and I have updated my answer to reflect the fact that it passes arguments by object reference. Keeping the answer up because it's definitely not the most far-fetched thing anyone would want to do. – MoxieBall Jun 15 '18 at 15:49
  • @MoxieBall it is not misleading to state that Python *never passes by reference*. Pass-by-reference semantics is pretty well defined, and Python doesn't do it. If you want to get pedantic, Python uses [*call-by-sharing*](https://en.wikipedia.org/wiki/Evaluation_strategy#Call_by_sharing) or *call-by-object-sharing*, which is admittedly an obscure term, although it is common in modern languages. I didn't downvote for the record. – juanpa.arrivillaga Jun 15 '18 at 15:57
  • @juanpa.arrivillaga I love being pedantic, helps me understand things better. I only said it was misleading because someone else might read that and assume it passes by value, since call-by-sharing is less common to think about in this context. Thanks for the responses – MoxieBall Jun 15 '18 at 16:01