Test 2 Flashcards
Chapter 5 and 6 - Checkpoint questions
What is a function?
A function is a group of statements that exist within a program for the purpose of performing a specific task.
What is meant by the phrase “divide and conquer”?
A large task is divided into several smaller tasks that are easily performed.
How do functions help you reuse code in a program?
If a specific operation is performed in several places in a program, a function can be written once to perform that operation, and then be executed any time it is needed.
How can functions make the development of multiple programs faster?
Functions can be written for the common tasks that are needed by the different programs. Those functions can then be incorporated into each program that needs them.
How can functions make it easier for programs to be developed by teams of programmers?
When a program is developed as a set of functions in which each performs an individual task, then different programmers can be assigned the job of writing different functions.
A function definition has what two parts?
A function definition has two parts: a header and a block. The header indicates the starting point of the function, and the block is a list of statements that belong to the function.
What does the phrase “calling a function” mean?
To call a function means to execute the function.
When a function is executing, what happens when the end of the function’s block is reached?
When the end of the function is reached, the computer returns back to the part of the program that called the function, and the program resumes execution at that point.
Why must you indent the statements in a block?
Because the Python interpreter uses the indentation to determine where a block begins and ends
What is a local variable? How is access to a local variable restricted?
A local variable is a variable that is declared inside a function.
It belongs to the function in which it is declared, and only statements in the same function can access it.
What is a variable’s scope?
The part of a program in which a variable may be accessed
Is it permissible for a local variable in one function to have the same name as a local variable in a different function?
Yes
What are the pieces of data that are passed into a function called?
Arguments
What are the variables that receive pieces of data in a function called?
Parameters
What is a parameter variable’s scope?
A parameter variable’s scope is the entire function in which the parameter is declared.
When a parameter is changed, does this affect the argument that was passed into the parameter?
No
The following statements call a function named show_data.
Which of the statements passes arguments by position, and which passes keyword arguments?
a. show_data(name=’Kathryn’, age=25)
b. show_data(‘Kathryn’, 25)
A. Keyword
B. Passes by Position
What is the scope of a global variable?
The entire Program
Give one good reason that you should not use global variables in a program.
- They make debugging difficult
- Makes a program hard to understand
What is a global constant?
Is it permissible to use global constants in a program?
A global constant is a name that is available to every function in the program.
It is permissible to use global constants. Because their value cannot be changed during the program’s execution, you do not have to worry about its value being altered.
How does a value-returning function differ from the void functions?
The difference is that a value returning function returns a value back to the statement that called it. A simple function does not return a value.
What is a library function?
A prewritten function that performs some commonly needed task
Why are library functions like “black boxes”?
The term “black box” is used to describe any mechanism that accepts input, performs some operation (that cannot be seen) using the input, and produces output.
What does the following statement do?
x = random.randint(1, 100)
It assigns a random integer in the range of 1 through 100 to the variable x.
What does the following statement do?
print(random.randint(1, 20))
It prints a random integer in the range of 1 through 20.
What does the following statement do?
print(random.randrange(10, 20))
It prints a random integer in the range of 10 through 19.
What does the following statement do?
print(random.random())
It prints a random floating-point number in the range of 0.0 up to, but not including, 1.0.
What does the following statement do?
print(random.uniform(0.1, 0.5))
It prints a random floating-point number in the range of 0.1 through 0.5.
When the random module is imported, what does it use as a seed value for random number generation?
It uses the system time, retrieved from the computer’s internal clock.
What happens if the same seed value is always used for generating random numbers?
If the same seed value were always used, the random number functions would always generate the same series of pseudorandom numbers.
What is the purpose of the return statement in a function?
It returns a value back to the part of the program that called it.
Look at the following function definition:
def do_something(number):
return number * 2
a. What is the name of the function?
b. What does the function do?
c. Given the function definition, what will the following statement display?
a . do_something
b. It returns a value that is twice the argument passed to it.
c. 20
What is a Boolean function?
A function that returns either True or False
What import statement do you need to write in a program that uses the math module?
import math
Write a statement that uses a math module function to get the square root of 100 and assigns it to a variable.
square_root = math.sqrt(100)
Write a statement that uses a math module function to convert 45 degrees to radians and assigns the value to a variable.
angle = math.radians(45)
6.1 What is an output file?
A file to which a program writes data. It is called an output file because the program sends output to it.