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)
13
  • 1
    what variable are you trying to increase and what should the output be? Commented Jun 15, 2018 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... Commented Jun 15, 2018 at 15:24
  • 2
    @MOROZILnic - This code makes so sense at all Commented Jun 15, 2018 at 15:25
  • 1
    @Nayuki Unfortunately you can't, this is python;) Commented Jun 15, 2018 at 15:26
  • 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); Commented Jun 15, 2018 at 15:30

2 Answers 2

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)
Sign up to request clarification or add additional context in comments.

1 Comment

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.
-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)

6 Comments

You should probably note that your "solution" is very much not advisable. There's a reason python chose to make numbers immutable.
I'll add the talk Facts and Myths about Python names and values. Really recommend it for anyone struggling with "pass by name/value/reference" in python
If Python variables were passed by reference then you wouldn't need to pass an immutable type. Python never passes by reference.
@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 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 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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.