Python Checkpoint Chapter 5

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

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 is a function?

A function is a group of statements that exist within a program for the pur- pose of performing a specific task.

5.33 What is a Boolean function?

A function that returns either True or False

5.20 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.

What is meant by the phrase "divide and conquer"?

A large task is divided into several smaller tasks that are easily performed.

5.10 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.

5.15 What is a parameter variable's scope?

A parameter variable's scope is the entire function in which the parameter is declared.

5.22 What is a library function?

A prewritten function that performs some commonly needed task

What are the pieces of data that are passed into a function called?

Arguments

5.9 Why must you indent the statements in a block?

Because the Python interpreter uses the indentation to determine where a block begins and ends

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.

5.19 Give one good reason that you should not use global variables in a program.

Here are three: • Global variables make debugging difficult. Any statement in a program can change the value of a global variable. If you find that the wrong value is being stored in a global variable, you have to track down every statement that accesses it to determine where the bad value is coming from. In a program with thousands of lines of code, this can be difficult. • 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. • Global variables make a program hard to understand. A global variable can be modified by any statement in the program. If you are to understand any part of the program that uses a global variable, you have to be aware of all the other parts of the program that access the global variable.

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

5.30 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.

5.24 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 .

5.26 What does the following statement do? print(random.randrange(10, 20))

It prints a random floating-point number in the range of 0.0 up to, but not including, 1.0.

5.27 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.

5.28 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.

5.25 What does the following statement do? print(random.randint(1, 20))

It prints a random integer in the range of 1 through 20.

5.31 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

5.29 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.

5.16 When a parameter is changed, does this affect the argument that was passed into the parameter?

No, it does not.

5.14 What are the variables that receive pieces of data in a function called?

Parameters

5.21 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.

5.18 What is the scope of a global variable?

The entire program

5.11 What is a variable's scope?

The part of a program in which a variable may be accessed

5.23 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.

5.7 What does the phrase "calling a function" mean?

To call a function means to execute the function.

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.

5.8 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.

5.12 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.

5.32 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? print(do_something(10))

a) do_something b) It returns a value that is twice the argument passed to it. c) 20

5.17 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. passes by keyword argument b. passes by position

5.36 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)

5.34 What import statement do you need to write in a program that uses the math module?

import math

5.35 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)


Kaugnay na mga set ng pag-aaral

Elsevier Nutrition/ DM Quiz- Final

View Set

unit 2 sentence structure and run-ons and fragment

View Set

Life Policy Provisions, Riders and Options

View Set