Starting Out with Python, 3e Ch 5

Ace your homework & exams now with Quizwiz!

You cannot have both keyword arguments and non-keyword arguments in a function call. T or F

False

You do not need to have an import statement in a program to use the functions in the random module. T or F

False

What statements are able to access a local variable?

Only statements inside that function can access the variable in the function.

What is a local variable's scope?

the variable can be used only locally, within the function in which it is created. No statement outside the function may access the variable.

A hierarchy chart does not show the steps that are taken inside a function. T or F

True

A function definition has what two parts?

Function Header; Block

When possible, you should avoid using _______ variables in a program. a. local b. global c. reference d. parameter

global

What is a global variable?

A variable that is created by an assignment statement that is written outside all the functions in a program file.

What is a local variable?

A variable that is declared within a function and cannot be accessed by statements that are outside of the function. In addition, a local variable cannot be accessed by code that appears inside the function at a point before the variable has been created.

Why do global variables make a program difficult to debug?

Because any statement in a program file 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.

Why are library functions like " black boxes" ?

Because you do not see the internal workings of library functions, 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.

This type of function returns either True or False. a. Binary b. true false c. Boolean d. logical

Boolean

What are the advantages of breaking a large program into modules?

Called "Modular programming". Large programs are easier to understand, debug and maintain when they are divided into modules. Modules also make it easier to reuse the same code in more than one program.

The phrase " divide and conquer" means that all of the programmers on a team should be divided and work in isolation. T or F

False

What is a parameter variable's scope?

It is the function in which the parameter is used. All of the statements inside the function can access the parameter variable, but no statement outside the function can access it.

A design technique that helps to reduce the duplication of code within a program and is a benefit of using functions is _______. a. code reuse b. divide and conquer c. debugging d. facilitation of teamwork

code reuse

How does a value-returning function differ from the void functions?

it returns a value back to the part of the program that called it.

What is the purpose of the return statement in a function?

it returns control back to the main function.

This is a design tool that describes the input, processing, and output of a function. a. hierarchy chart b. IPO chart c. datagram chart d. data processing chart

IPO chart

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.

Why must you indent the statements in a block?

Indentation is required because the Python interpreter uses it to tell where the block begins and ends.

In Python, you can specify which parameter an argument should be passed into a function call. T or F

True

Some library functions are built into the Python interpreter. T or F

True

What is a value-returning function?

a function that returns a value back to the part of the program that called it.

A _______ is a variable that is created inside a function. a. global variable b. local variable c. hidden variable d. none of the above; you cannot create a variable inside a function

local variable

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

parameters

A(n) _______ is the part of a program in which a variable may be accessed. a. declaration space b. area of visibility c. scope d. mode

scope

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

Dividing a large program or task into several smaller tasks that are easily managed and performed.

What three things are listed on an IPO chart?

Input Processing Output

Complex mathematical expressions can sometimes be simplified by breaking out part of the expression and putting it in a function. T or F

True

Is it permissible for a local variable in one function to have the same name as a local variable in a different function?

Yes; Different functions can have local variables with the same names because the functions cannot see each other's local variables.

What is a Boolean function?

a function which returns either True or False. You can use a Boolean function to test a condition, and then return either True or False to indicate whether the condition exists. Boolean functions are useful for simplifying complex conditions that are tested in decision and repetition structures.

A group of statements that exist within a program for the purpose of performing a specific task is a(n) _______. a. block b. parameter c. function d. expression

function

What is a void function?

A void function is a group of statements that exist within a program for the purpose of performing a specific task. When you need the function to perform its task, you call the function. This causes the statements inside the function to execute. When the function is finished, control of the program returns to the statement appearing immediately after the function call.

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

arguments

A function in Python can return more than one value. T or F

True

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

import math; In order to call a function that is stored in a module, you have to write an import statement at the top of your program. An import statement tells the interpreter the name of the module that contains the function.

This is math module function. a. derivative b. factor c. sqrt d. differentiate

sqrt

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.

When the random module is imported, what does it use as a seed value for random number generation?

the system time from the computer's internal clock

A design technique that programmers use to break down an algorithm into functions is known as _______. a. top-down design b. code simplification c. code refactoring d. hierarchical subtasking

top-down design

This standard library function returns a random floating-point number within a specified range of values. a. random b. randint c. random_integer d. uniform

uniform

How is access to a local variable restricted?

A local variable belongs to the function in which it is created, and only statements inside that function can access the variable.

How can functions make the development of multiple programs faster?

Functions can be written for the commonly needed tasks, and those functions can be incorporated into each program that needs them.

What is a variable's scope?

It is the part of a program in which the variable may be accessed.

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

No; the changes made to a parameter do not affect the arguments.

What is a function definition?

The definition (code) that is written to create a function. It specifies what a function does.

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

The reasons are as follows: • Global variables make debugging difficult. Any statement in a program file 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, most likely you will 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.

What is a seed value?

The value that initializes a formula. The seed value is used in the calculation that returns the next random number in the series.

When a function is executing, what happens when the end of the function's block is reached?

When the end of the block is reached, the interpreter jumps back to the part of the program that called the function, and the program resumes execution at that point. When this happens, we say that the function "returns".

What is a global constant?

a global name that references a value that cannot be changed.

What is a library function?

a prewritten function that performs commonly needed tasks. Python, as well as most other programming languages, comes with a standard library of functions that have already been written for you. These functions, known as library functions, make a programmer's job easier because they perform many of the tasks that programmers commonly need to perform.

You _______ the function to execute it. a. define b. call c. import d. export

call

A variable that is visible to every function in a program file is a _______. a. local variable b. universal variable c. program-wide variable d. global variable

global variable

The first line of a function definition is known as the _______. a. body b. introduction c. initialization d. header

header

A _______ is a diagram that g1ves a visual representation of the relationships between functions in a program. a. flowchart b. function relationship chart c. symbol chart d. hierarchy chart

hierarchy chart (also called a 'structure chart').

This is a prewritten function that is built into a programming language. a. standard function b. library function c. custom function d. cafeteria function

library function

A(n) _______ is a special variable that receives a piece of data when a function is called. a. argument b. parameter c. header d. packet

parameter

This standard library function returns a random floating-point number in the range of 0.0 up to 1.0 (but not including 1.0). a. random b. randint c. random_integer d. uniform

random

Suppose you want to select a random number from the following sequence: 0, 5, 10, 15, 20, 25, 30 What library function would you use?

randrange ex: num = random.randrange(0, 5, 10, 15, 20, 25, 30)

This statement causes a function to end and sends a value back to the part of the program that called the function. a. end b. send c. exit d. return

return

What statement do you have to have in a value-returning function?

return expression, where expression holds the value that is to be returned to the calling function. This can be any value, variable or expression that has a value.

A flowchart shows the hierarchical relationships between functions in a program. T or F

False

A statement in one function can access a local variable in another function. T or F

False

Calling a function and defining a function mean the same thing. T or F

False

Function names should be as short as possible. T or F

False

In Python you cannot write functions that accept multiple arguments. T or F

False

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 the scope of a global variable?

A global variable can be accessed by any statement in the program file, including the statements in any function.

Name and describe the two parts of a function definition.

Function Header - the first line of the function definition. It marks the beginning of the function definition. The function header begins with the key word def, followed by the name of the function, followed by a set of parentheses, followed by a colon. Block - is a set of statements that belong together as a group. These statements are performed any time the function is executed.

What does the phrase "calling a function" mean?

To execute a function, a function call is required. "Calling a function" is a statement that is used to invoke (execute) the function at required places in a program.

Functions make it easier for programmers to work in teams. T or F

True

IPO charts provide only brief descriptions of a function's input, processing, and output, but do not show the specific steps taken in a function. T or F

True

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 that each performs an individual task, then different programmers can be assigned the job of writing different functions.

A(n) _______ is a piece of data that is sent into a function. a. argument b. parameter c. header d. packet

argument

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 )

arguments by position: show_ data ( 'Kathryn' , 25 ) keyword arguments: show_data ( name= 'Kathryn' , age=25 )

This standard library function returns a random integer within a specified range of values. a. random b. randint c. random_integer d. uniform

randint


Related study sets

Delegation & Communication LEADERSHIP QUIZ 1

View Set

Chapter 7: Receiving, Storage, and Inventory

View Set

Chapter 17: Bandura: Social Cognitive Theory

View Set

Chapter 38: Nursing Care of the Child With an Alteration in Intracranial Regulation/Neurologic Disorder

View Set