I'm new to programming and right now I'm learning about functions and everything related to them. I've been making some example of my own, and I ran into a problem.
def test(x):
number = x + 1
return (number)
def test2(y):
number2 = test(y) + 2
print (number2)
test2(1)
If I print this function, it would work. However I don't understand something. Why is it that when I put number 1 into test2(y), it also goes into test(x)? Why does this happen?
You defined def test2(y):
so when you run test2(1)
then it creates local variable y
inside test2
and it assigns y = 1
- and then it starts to execute commands inside test2
.
Inside test2
you execute test(y)
so it gets value from y
and executes test(1)
. And again: you defined def test(x)
so it creates local variable x
inside test
, and it assigns x = 1
and then it starts to executes commands inside test
. So now 1
is inside test
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.