[Tutor] passing variables to functions

steve10brink1 at comcast.net steve10brink1 at comcast.net
Mon Nov 16 10:46:50 EST 2020


Hi,

I always assumed that variables could only be passed to functions via function calls unless they are designated as global.  So I was suprised that this code works without any errors.  How does the variable 'b' get indirectly assigned in the function testVar(a)?  I expected an error.  I am using python3 version 3.5.3.

Code:
-----------------------------------------------------------
def testVar(a):
print(b,a)    #b is from main and not passed thru function call, expect error
return a

print("Test variables in function.....")
b = 23.4
c = testVar(b)
print("Final: ",c)

-----------------------------------------------------------
Output:

Test variables in function.....
23.4 23.4
Final: 23.4


More information about the Tutor mailing list