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.