4. Functions

Lecture 4.

Functions

91 minutes video lecture, 10 minutes coding practice time.

Slides

Video Lectures

Review Questions

1: What is the value of integer?

def myInteger (num1, num2):
    return num2 - num1
integer = myInteger (5, 14)
Click here to see the answer.

9

2: What is the value of integer?

def myInteger (num2, num1):
    return num2 % num1
integer = myInteger (5, 14)
Click here to see the answer.

5

3: What is the value of integer?

def myInteger (num1, num2):
    return num2 / num1
integer = myInteger (5, 14)
Click here to see the answer.

2.8

4: What is the value of integer?

def myInteger (num1, num2):
    return num2 // num1
integer = myInteger (5, 14)
Click here to see the answer.

2

5: What is the value of integer?

def myInteger (num1, num2):
    print(num2 / num1)
integer = myInteger (5, 14)
Click here to see the answer.

None. myInteger function does not return a value.

6: What does the following program print on the console?

def myInteger (string, number):
    print(string + number)
myPrint(5, 14)
Click here to see the answer.

19

7: What does the following program print on the console?

def myInteger (string, number):
    print(string, number)
myPrint(5, 14)
Click here to see the answer.

5 14

8: What does the following program print on the console?

def myInteger (string, number):
    print(string + number)
myPrint("5", 14)
Click here to see the answer.

Error. “5” + 14 is a syntax error, since “5” is text (i.e. string) whereas 14 is a number

9: Is the following code OK (i.e., runs without error) ?

def mySum (num1):
    num1 += 2
    num2 += 5
    return num1 + num2
value = mySum (10, 2)
Click here to see the answer.

Error, num 2 is not defined in the function header.

10: Is the following code OK (i.e., runs without error) ?

def mySum (num1):
    num1 += 2
    num2 += 5
    return num1 + num2
    num1 += 10
value = mySum (10, 2)
Click here to see the answer.

It is OK, runs without error. However, there is an unreachable code after return. The function terminates after return, so please do not write any statement after return.

11: Is the following code OK (i.e., runs without error) ?

def mySum (num1, num2):
    num1 += 2
    num2 += 5
    return num1 + num2
    num1 += 10
    return num1 + num2
value = mySum (10, 2)
Click here to see the answer.

It is OK, runs without error. The second return statement is not executed, but Python does not terminate due to a syntax error.

12: Is the following code OK (i.e., runs without error) ?

value = mySum (10, 2)
def mySum (num1, num2):
    num1 += 2
    num2 += 5
    return num1 + num2
    num1 += 10
    return num1 + num2
Click here to see the answer.

No, there is a syntax error in this program. The function was not defined before it was invoked. Always define the function before invoking it.

13: What does the following code print?

def myInteger (num1, num2):
    num1 //= 5
    print(num1)
    return num2 % num1
num1 = 15
num2 = myInteger(33, num1)
print (myInteger (num1 * num2, num2) )
print (num2) 
Click here to see the answer.

First, it prints 6, then 9, then 3, and finally 3.

In-class Programming Exercises

1: Write a program that asks the user to enter two integers: num1 and num2. Then, the program invokes a function that takes two integers. This function returns true if the first number is greater than the second one, else it returns false. After the function returns the value, the program prints whether num1 is greater than num2, or not.

Click to see the answer.

# In-class Exercise 1

def myComparison (num1, num2):
    result = False
    if num1 > num2:
        result = True
    return result
    
num1 = int ( input ( "Please input an integer and press Enter") )
num2 = int ( input ( "Please input an integer and press Enter") )

if myComparison (num1, num2):
    print("The first number is greater!")
else:
    print("The second number is greater or equal!")

Answers to Programming Exercises

Feedback

Previous