Create a function with a local variable. Show what happens when you try to use that variable outside the function. Explain the results.
#Create a function with a local variable.
def add(number1,number2):
#(1) a local variable.
sumResult=number1+number2
return sumResult
#(2)use the same variable outside the function.
sumResult=45
print(f"sumResult={sumResult}")
def main():
print(f"5+6= {add(5,6)}")
main()
#Ouput:
#sumResult=45
#5+6= 11
#a local variable inside a function (1) and a variable
#outside the function (2) are two different variables.
#The values of these variables don't overlap each other.
Comments
Leave a comment