Answer to Question #237667 in Python for Wolski

Question #237667
Define a function that takes an argument. Call the function. Identify what code is the argument and what code is the parameter.

Example 2: Call your function from Example 1 three times with different kinds of arguments: a value, a variable, and an expression. Identify which kind of argument is which.

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

Example 4: Create a function that takes an argument. Give the function parameter a unique name. Show what happens when you try to use that parameter name outside the function. Explain the results.

Example 5: Show what happens when a variable defined outside a function has the same name as a local variable inside a function. Explain what happens to the value of each variable as the program runs.
1
Expert's answer
2021-09-15T17:18:27-0400
# Example 1:
def my_func(num, power=2):  # 'num', 'power' - parameters
    return num ** power


print(my_func(2, 3))  # 2, 3 - arguments

# Example 2:
print(my_func(2))  # value

var = 12
print(my_func(var))  # variable

print(my_func(var * 2 - 15))  # expression

# Example 3:
def my_func(num):
    power = 2  # local variable
    return num ** power


print(my_func(5))  # 25
print(power)  # NameError: name 'power' is not defined
# variable 'power' is not exist outside my_func

# Example 4:
def my_func(num, power=2):
    return num ** power

print(my_func(2, 3)) # 8
print(num)  # NameError: name 'num' is not defined
# variable 'num' is not exist outside my_func

# Example 5:
def my_func():
    var = 20  # inner variable
    return var

var = 10  # outer variable
print(var)
print(my_func())
print(var)

# Output:
# 10
# 20
# 10
# In fact - nothing will happen. Each variable will live it's own life in it's scope.
# Any change of inner variable var will not affect outer variable var and vice versa.

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