Functions Flashcards
What is a function?
A function is a collection of statements grouped together that performs an operation.
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. a heap B. storage area C. a stack D. an array
C. a stack
What is a void function?
A function that does not return a value.
Which of the following should be defined as a None function?
A. Write a function that prints integers from 1 to 100.
B. Write a function that returns a random integer from 1 to 100.
C. Write a function that checks whether a number is from 1 to 100.
D. Write a function that converts an uppercase letter to lowercase.
A. Write a function that prints integers from 1 to 100.
Consider the following incomplete code:
def f(number): # Missing function body # return the value number
print(f(5))
The missing function body should be \_\_\_\_\_\_\_\_. A. return "number" B. print(number) C. print("number") D. return number
D. return number
Given the following function header, which of the following is correct to invoke it?
def f(p1, p2, p3, p4) A. f(1, 2, 3, 4) B. f(p1 = 1, 2, 3, 4) C. f(p1 = 1, p2 = 2, p3 = 3, 4) D. f(p1 = 1, p2 = 2, p3 = 3, p4 = 4) E. f(1, 2, 3, p4 = 4)
A. f(1, 2, 3, 4)
D. f(p1 = 1, p2 = 2, p3 = 3, p4 = 4)
E. f(1, 2, 3, p4 = 4)
Given the following function, what will be displayed by the call nPrint(‘a’, 4)?
def nPrint(message, n): while n > 0: print(message, end = '') n -= 1 A. aaaaa B. aaaa C. aaa D. invalid call E. infinite loop
B. aaaa
Given the following function, what will be displayed by the call nPrint(‘a’, 4)?
def nPrint(message, n): while n > 0: print(message, end = '') n -= 1 A. aaaaa B. aaaa C. aaa D. invalid call E. infinite loop
E. infinite loop
Hint: Note that n -= 1 is outside of the loop.
What is an argument?
An argument is an object that is passed to a function when the function is invoked.
What is a parameter?
A parameter is a variable that receives an argument that is passed to a function.
What is an immutable object?
An object whose contents cannot be changed.
Numbers and strings are ________ objects.
immutable
What is k after running the following code?
def nPrint(message, n):
while n > 0:
print(message)
n -= 1
k = 2 nPrint(n = k, message = "A message") A. 0 B. 1 C. 2 D. 3
C. 2
Whenever possible, you should avoid using \_\_\_\_\_\_\_\_\_\_. A. global variables B. function parameters C. global constants D. local variables
A. global variables
A variable defined inside a function is referred to as \_\_\_\_\_\_\_\_\_\_. A. a global variable B. a function variable C. a block variable D. a local variable
D. a local variable