Assessment: Functions, local and global variables

Oct 2, 2014 • Ramalingam Saravanan

Based on the SWC lesson Creating Functions

Question 1

Consider the following piece of Python code:

x = 2
n = 3
def poly(x):
    # Compute simple polynomial value for x
    n = 2
    return x**n
print poly(4), x

What two pairs of numbers will be printed out by the above piece of code?

a) 4 2
b) 16 2
c) 4 4
d) 16 4
e) None of the above

Question 2

A student was assigned the following task: Write a function named square that accepts a single argument and returns the square of its argument. Test it by calling it with an argument value of 3, i.e., square(3) and ensure that it returns 9

The student produced the following code:

def square(x):
    n = 2
    x = 3
    return x**n
print square(3)

However, the above function definition is not correct, e.g., square(2) produces the wrong answer. Correct the function definition by making minimal changes to the code.