Question #181873

Create a function with a local variable. Show what happens when you try to use that variable outside the function. Explain the results.


Expert's answer

#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.

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

LATEST TUTORIALS
APPROVED BY CLIENTS