Answer to Question #181873 in Python for Asumang Ebenezer

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.


1
Expert's answer
2021-04-15T23:21:13-0400
#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!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS