1/35
Checkpoints
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What is a function?
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 Header and a block.
Header: indicates the starting point of the function.
Block: a list of statements that belong to the function.
What does the phrase “calling a function” mean?
To execute the function.
When a function is executing, what happens when the end of the function’s block 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 variable that is declared inside a function
A variable 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, it is permissible.
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?
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, it does not.
The following statements call a function named show_data. Which of the statements passes arguments by position, and which passes keyword arguments?
show_data(name= ‘Kathryn’, age=25)
show_data(‘Kathryn', 25)
passes by keyword argument
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.
(*hay dos más si quieres repasarlos en la página 692)
Functions that use global variables are usually dependent on those variables. If you want to use such a function in a different program, you will most likely have to redesign it so it does not rely on the global variable.
What is a global constant? Is it permissible to use global constants in a program? Why?
A name that is available to every function in the program.
It is permissible to use them.
bc 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?
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
What is the name of the function?
What does the function do?
Given the function definition, what will the following statement display?
print(do_something(10))
do_something
It returns a value that is twice the argument passed to it.
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)