Unit 1

Function definitions

[5.1] What is the difference between defining a function and calling a function? Feel free to give an example.

[5.2] What does the statement def someFunction(x, y): do?

Code Writing

[5.3] Write a function that takes text input from the user and displays the input in lower case.

[5.4] Write a function that takes a person’s name as a parameter and prints it on the screen. Call the function with your name.

[5.5] Write the function diff21(n) which, given an integer n, returns the absolute difference between n and 21. Call the function with n = 10 and print the returned value.

[5.6] Write a function that takes as a parameter a number n and prints that number rounded to two decimal places. Call the function with the value n = 3.145678

[5.7] Write a function that takes as a parameter the integer n and raises this number to the power of itself. Call the function with n = 7.

[5.8] Write a function that takes a string parameter, e.g., "Bob", and returns a greeting of the form "Hello Bob!".

            
hello_name('Bob') # this should print 'Hello Bob!'
hello_name('Alice') # this should print 'Hello Alice!'
hello_name('X') # this should print 'Hello X!'
            
        

[5.9] Write a function named right_justify that takes a string named s as a parameter and prints the string with enough leading spaces so that the last letter of the string is in column 70 of the display.

            
>>> right_justify("monty")
                                                                 monty
            
        

[5.10] Write a function that takes two numbers a and b as a parameter and returns their sum.

[5.11] Write a function that takes two numbers a and b as a parameter and returns their difference.

[5.12] Write a function that takes two numbers a and b as a parameter and returns their product.

[5.13] Write a function that takes two numbers a and b as parameters, divides them, and prints the result.

[5.14] Write a function that takes as a parameter the side of a square and returns the perimeter of the square.

[5.15] Write a function that takes as a parameter the side of a square and prints the area of the square.

[5.16] Write a function that takes as a parameter the radius of a circle and prints the perimeter of the circle.

[5.17] Write a function that takes as a parameter the radius of a circle and returns the area of the circle.

[5.18] Write a function that takes as a parameter the two sides of a rectangle, prints the perimeter of the rectangle, and returns the area of the rectangle.

[5.19] Write a function dice(n) which simulates a dice roll for a dice with n sides and prints the result. Hint: Using the random library is helpful here! Call the function with 3 different values of n and see the results!

[5.20] Write a function that takes input from the user and displays that input back by printing it.

Function Call Tracing

[5.21] What is the output of the following block of code?

        
def mystery1(str1, str2):
    return str1 + str2

mystery1("hello", "world")
        
    

[5.22] What is the output of the following blocks of code?

        
def mystery2(number):
    return abs(number)

mystery2(34)
mystery2(-3)
        
    

[5.23] What will the following code print once it is executed? Explain in a few sentences why.

        
1. def outer(x):
2.     y = x + 2
3.     print("outer y:", y)
4.     return inner(y) * 2
5.
6. def inner(x):
7.     y = x * 2
8.     print("inner y:", y)
9.     return y
10.
11. print(outer(4))
        
    

[5.24] You do: given the code, trace through the execution of the code and the function calls. It can be helpful to jot down the current variable values as well, so you don't have to hold them all in your head. What will be printed at the end?

        
def calculateTip(cost):
    tipRate = 0.2
    return cost * tipRate

def payForMeal(cash, cost):
    cost = cost + calculateTip(cost)
    cash = cash - cost
    print("Thanks!")
    return cash

wallet = 20.00
wallet = payForMeal(wallet, 8.00)
print("Money remaining:", wallet)
        
    

[5.25] What will be printed when we run the following code?

        
def test(x):
    print("A:", x)
    return x + 5

y = 2
print("B:", y)
z = test(y + 1)
        
    

[5.26] For the following code, answer the questions below:

  1. What will this block of code print once it stops running?
  2. List all the function calls that occur in this code block with their name, argument value(s), and returned value. If there is no name / argument / returned value, leave the space blank. Do not include any calls to built-in functions (like print) in the table.
  3. List all the global and local variables in the code, and determine to which function the local variables belong.

        
def fun1(x):
    tmp = x * 2
    x = 3
    print("fun1:", x)
    x = "WOW"
    return x

def fun2(x):
    x = x * 3
    y = int(x) + 2
    print("fun2:", fun1(x))
    return y

x = "5"
result = fun2(x)
print("End: ", x, result)
        
    

[5.27] Provided the following Python code, trace the function calls. What will be printed after line 6 is executed?


1. def foo(x,y,z):
2.       return x*y+z

3. def bar(x):
4.       a = foo(x, x+2, x*2)
5.       return a**2

6. print(bar(3))

Scope

[5.28] What will the following code print? Explain in a few words what is happening

        
x = "Hello World"

def fun1():
    x = 2
    print("inside the function x has the value:", x)

fun1()
print("outside the function x has the value:", x)
        
    

[5.29] Provided the following Python code, please answer the following questions.

  1. What is the scope of the variable name?
  2. What is the scope of the variable date?

        
1. date = “January 28th”
2. def welcome(name):
3.      print(“Welcome, ” + name + “!”)
4.      print(“Today is” + date + ”.”)
5. print(“Goodbye, “ + name + “.”)
        
    

[5.30] Which variables in the following code snippet are global? Which are local? For the local variables, which function can see them?

        
name = "Farnam"

def greet(day):
    punctuation = "!"
    print("Hello, " + name + punctuation)
    print("Today is " + day + punctuation)

def leave():
    punctuation = "."
    print("Goodbye, " + name + punctuation)

greet("Friday")
leave()