What happens:
-
num = 6
A variable num is created and assigned the value 6. -
decrement(num)
This calls the decrement function and passes the value of num (which is 6) into the function. -
Inside the decrement function:
-
Here, a new local variable num is created inside the function scope (it’s a copy of the one passed in).
num -= 2 changes this local copy to 4.
-
But the original num outside the function is not changed because integers are immutable and passed by value (in effect).
-
-
print(num)
This prints the original num, which is still 6.
Output:
Summary:
Even though the function decreases num by 2, it only does so inside the function. The original variable remains unchanged because integers are immutable and passed by value in Python functions.
0 Comments:
Post a Comment