Chapter 6-Functions
3 -1
What will be displayed by the following code? def f1(x = 1, y = 2): return x + y, x - y x, y = f1(y = 2, x = 1) print(x, y)
3 3
What will be displayed by the following code? def f1(x = 1, y = 2): x = x + y y += 1 print(x, y) f1()
3 1
What will be displayed by the following code? x = 1 def f1(): x = 3 print(x) f1() print(x)
may have no parameters
A function ________.
None
A function with no return statement returns _____.
parentheses
Arguments to functions always appear within ________.
a stack
Each time a function is invoked, the system stores parameters and local variables in an area of memory, known as _______, which stores elements in last-in first-out fashion.
A stub
__________ is a simple but incomplete version of a function.
Top-down approach
__________ is to implement one function in the structure chart at a time from the top to the bottom.
2
def nPrint(message, n): while n > 0: print(message) n -= 1 What is k after invoking nPrint("A message", k)? k = 2 nPrint("A message", k)
global variables
Whenever possible, you should avoid using __________.
a local variable
A variable defined inside a function is referred to as __________.
a global variable
A variable defined outside a function is referred to as _______.
return number
Consider the following incomplete code: def f(number): # Missing function body print(f(5)) The missing function body should be ________.
No
Does the function call in the following function cause syntax errors? import math def main(): math.sin(math.pi) main()
2
Given the following function def nPrint(message, n): while n > 0: print(message) n -= 1 What is k after invoking nPrint("A message", k)? k = 2 nPrint(n = k, message = "A message")
aaaa
Given the following function def nPrint(message, n): while n > 0: print(message) n -= 1 What will be displayed by the call nPrint('a', 4)?
infinite loop
Given the following function def nPrint(message, n): while n > 0: print(message) n -= 1 What will be displayed by the call nPrint('a', 4)?
f(1, 2, 3, 4) f(p1 = 1, p2 = 2, p3 = 3, p4 = 4) f(1, 2, 3, p4 = 4)
Given the following function header: def f(p1, p2, p3, p4) Which of the following is correct to invoke it?
None
If a function does not return a value, by default, it returns ________.
function name and parameter list
The header of a function consists of _________.
3 2
What will be displayed by the following code? def f1(x = 1, y = 2): x = x + y y += 1 print(x, y) f1(2, 1)
3 3
What will be displayed by the following code? def f1(x = 1, y = 2): x = x + y y += 1 print(x, y) f1(y = 2, x = 1)
3 3
What will be displayed by the following code? x = 1 def f1(): global x x = x + 2 print(x) f1() print(x)
The program has a runtime error because x is not defined.
What will be displayed by the following code? x = 1 def f1(): x = x + 2 print(x) f1() print(x)
3 1
What will be displayed by the following code? x = 1 def f1(): y = x + 2 print(y) f1() print(x)
pass by value
When you invoke a function with a parameter, the value of the argument is passed to the parameter. This is referred to as _________.
def f(a = 1, b = 1, c = 2):
Which of the following function headers is correct?
Write a function that prints integers from 1 to 100.
Which of the following should be defined as a None function?