[Tutor] using a variable inside function, provided the variable is assigned a value after the function definition

Manprit Singh manpritsinghece at gmail.com
Sun Sep 19 03:11:05 EDT 2021


Dear Sir ,

Consider an example given below :
def sumofnumber(x):
    return x+b

b = 3
a = 4
ans = sumofnumber(a)
print(ans)   gives the answer = 7 which is the sum of 4 & 3 .
the formal parameter x receives the value of  variable a which is 4 and the
function returns x+ b , which is 4 +3  = 7 , because 3 is the value of
variable b .
Here the variable b which is assigned a value 3 after or below the function
definition sumofnumber, is used inside the same function.

My question is :
A variable which is assigned a value below or after  the function
definition , can be used inside that function definition or not ? is it a
good practise ?

Regards
Manprit Singh

Regards
Manprit Singh


More information about the Tutor mailing list