Unit 1

Function Calls

Code Tracing

[4.0] Please answer the following questions:

  1. How many arguments are there in this function call: msg_to_santa(name, age, wish)
  2. What keyword denotes the start of a function?
  3. What would happen when calling a function that has no return statement?
  4. What’s the difference between print and return statements inside a function?

[4.1] What does print mean? What does the statement print(3) do? What does it return?

[4.2] What is the output from print(1/3)? Why do we get this output?

[4.3] What do each of these functions evaluate to? What does it return (write None if nothing)? Does it have any side effects (write No if not)?

Function call: Returns: Side effects:
abs(-2)
pow(2, 3)
round(12.4567, 2)
int("4")
float(3)
str(98.9)
bool(0)
type(4 + 3.0)

[4.4] What will happen when you execute the following code?

num = input("Enter a number to be doubled:") 
doubled_num = num * 2 
print(doubled_num)

[4.5] What is going to be printed once each of the following code blocks is executed?

#code block 1
a = -4
b = 2
c = abs(a)
x = a * c
print(x)

#code block 2
a = 3
b = 2

x = pow(a, b)
print(x)

x = pow(b, a)
print(x)

[4.6] What will the following evaluate to?

  • round(pow(abs(-35), 1/2), 2)
  • pow(round(34/4,0), abs(3))

[4.7] Trace through the following code. What are the values of a and b at the end, once all the code has executed? What is going to be printed at the end, once all the code has executed?

a = 4
b = 15

a = b // a
a = b / a
b = b % 4
print(a + b)

[4.8] Consider this function definition

def mystery(num):
    return abs(num) + num
Imagine we ran mystery(-5).
  • Does this call have any argument(s)? If yes, list it/them.
  • Does this call have any returned value(s)? If yes, list it/them.
  • Does this call have any side effect(s)? If yes, list it/them.

[4.9] Now, imagine we ran the y = mystery(x) where type(x) == int . Which of the expressions below are true?

  1. y>0
  2. not (y < 0)
  3. x <= y
  4. y == 0
  5. type(y) == int
  6. type(y) == float

[4.10] Consider the following function:

def hypotenuse(a, b, rightAngle):
    if not rightAngle: 
        print(“I can’t calculate the hypotenuse of a triangle without a right angle!”) 
    else:
        c = (a**2 + b**2)**(0.5) 
        print(“The triangle’s hypotenuse is”, c)
If you call hypotenuse(3, 4, True):
  • Does this call have any argument(s)? If yes, list it/them.
  • Does this call have any returned value(s)? If yes, list it/them.
  • Does this call have any side effect(s)? If yes, list it/them.

[4.11] Consider the following function:

def funkyMath(x, y): 
    newX = int(x) + 8 
    newY = (y - 2) * 5 
    return 12 + newY - newX 
If you call funkyMath(3.8, 7):
  • Does this call have any argument(s)? If yes, list it/them.
  • Does this call have any returned value(s)? If yes, list it/them.
  • Does this call have any side effect(s)? If yes, list it/them.

[4.12] Answer the following questions regarding the block of code below:

  • Are there any global variables? If yes, what are they?
  • Are there any local variables? If yes, what are they?
  • Does the function call to fun1 have any argument(s)? If yes, list it/them.
  • Does the function call to fun1have any returned value(s)? If yes, list it/them.
x = 3
z = 5

def fun1(x, y):
    w = x + y
    print("x:", x)
    return w

fun1(1, z)

[4.13] Identify the number of arguments and their type, function name, side effects and return value type of the function below:

import random
import math

def magicFunction(x, y, s):
    a = random.randint(1, 5)
    b = math.ceil(x/y)
    print(s - a)
    print(b)

magicFunction(5, 3, 10)

[4.14] You do: what are the arguments and returned value of this function call, given the definition? What will it print?

        
def addTip(cost, percent):
    tip = cost * percent
    print("Tip:", tip)
    return cost + tip

total = addTip(25, 0.2)
        
    

[4.15] For the following code snippet, what is the function name, argument value(s), and returned value. If there is no name / argument / returned value, leave this blank

        
a = 4
b = 4
c = pow(a, b)
        
    

[4.16] For the following code snippet, what is the function name, argument value(s), and returned value. If there is no name / argument / returned value, leave this blank

        
a = -14
c = abs(a)
        
    

[4.17] For the following code snippet, what is the function name, argument value(s), and returned value. If there is no name / argument / returned value, leave this blank

        
s = "Mira"
print("My name is: ", s, " and I am: ", 11 + 10, " years old.")
        
    

Code Writing

[4.18] Given two variables x and y below, write code to calculate their sum, absolute value of their difference, x raised to the power of y and round the result of x divided by y:

x = 5
y = 2

#Your code below
sum = 
diff = 
power = 
roundDiv = 

[4.19] A student wants to calculate the volume of any sphere. Help this student by writing code that asks the user to enter in a radius value via the input function in Python and then compute the volume using the volume formula (V = 4/3 * r * 3):

[4.20] Write a line of code that generates a random number between 1 and 10 and then raises that number to power 2 and prints the result:

[4.21] Write a line of code that takes a variable x which holds a number as a string, turns the number into a float, and then divides the number by 13:

[4.22] Write a line of code that takes a variable x that stores an integer, and calculates the log of that number with base 10. It then multiples the result by pi and prints the result:

[4.23] Write some code that asks the user for two numbers (using the input function twice), multiplies the given numbers together, and then prints the result. For example, if the user enters 2 and then 4, your program should print the following text: “The product of 2 and 4 is 8”.

[4.24] Write some code that asks the user for a float and an integer (using the input function twice). Then, it prints the first number raised to the power of the second number. A sample run of the program should look like this:

Enter a base: 1.2
Enter an exponent: 3
1.2 to the power of 3 is: 1.72799999999998

[4.25] A 15110 student is trying to determine their grade for the class. To do that, they need to calculate their approximate score for their programming assignment. They know that:

  • for their 999 lines of code, between 5-15% of them will have syntax errors, and between 2-5% of the lines will have logical errors (they ask you generate this randomly)
  • for every buggy line they lose 0.1 point and for every infinite loop line they lose 0.5 points. Since lines can only be integers, round the number of lines down to the nearest integer.
Given that their homework assignment is out of 100 points, and using some libraries you have learnt about in class, help this student calculate their homework score.