CECS 100 - Chapter 5: Functions

Réussis tes devoirs et examens dès maintenant avec Quizwiz!

What is a function?

a group of statements that exist within a program for the purpose of performing a specific task

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

c. sqrt

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

When the random module is imported, it retrieves the system time from the computer's internal clock and uses that as the ________ __________.

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

YES because a function's local variables are hidden from other functions, other functions may have their own local variables with the same name

What is a Boolean function?

a function which returns either True or False.

What is a global constant? Is it permissible to use global constants in program?

a global name that references a value that can not be changed. It is permissible to use in a program (whereas a global VARIABLE is highly discouraged)

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

a parameter

What is a library function?

a prewritten function that performs commonly needed tasks.

Modularized program

a program that has been written with each task in its own function

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

a. argument

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

a. code reuse

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. returns a value that is twice the argument passed to it c. 20

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

a. hierarchy chart

The following statements calcall 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 arguments b. passes by position

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

a. random

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

a. top-down design

What is the scope of a global variable?

accessible to all functions in a program file

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

an argument

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

b. IPO chart

4. You __________ a function to execute it. a. define b. call c. import d. export

b. call

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

b. function

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

b. global

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

b. library function

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

b. local variable

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

b. parameter

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

b. randint

Why are library functions like "black boxes"?

because you do not see the internal workings of library functions. The term is used to describe any mechanism that accepts input, performs some operation (that cannot be seen) using the input, and produces output

What is a local variable? How is access to a local variable restricted?

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

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

breaking down a large task into several subtasks that are easily performed

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

c. Boolean

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

c. scope

How do functions help you reuse code in a program?

can be written once, use in whichever areas need it/whenever you need it

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

d. function header

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

d. global variable

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

d. return

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

d. uniform

How can functions make it easier for programs to be developed by teams of programmers?

different programmers can be assigned the job of writing different functions

Value-returning function

executes the statements it contains, and then it returns a value back to the statement that called it (e.g input function, int and float functions)

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

false

A function definition has what two parts?

function header; block

How can functions make the development of a program faster?

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

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 convert 45 degrees to radians and assigns the value to a variable.

import math angle = math.radians(45)

Write a statement that uses a math module function to get the square root of 100 and assigns it to a variable.

import math square_root = math.sqrt(100)

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

interpreter jumps back to part of program that called the function, and the program resumes execution at that point

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

it is like a void function in that 1) it's a group of statements that performs a specific task and 2) when you want to execute the function, you call it 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's a variable's scope?

part of a program in which the variable may be accessed. A local variable's ______ is the function in which the variable is created.

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

returns a value back to the part of the program that called it (of a value-returning function)

Void function

simply executes the statements it contains and then terminates

What does the following statement do? print(random.uniform(0.1, 0.5))

the _____ function prints a random floating point number from 0.1 up to 0.5 and assigns it to the number variable

T/F: 2. Functions make it easier for programmers to work in teams.

true

control

when a program calls a function, programmers commonly say that the _______ of the program transfers to that function (i.e "____" of the program's execution)

What does the following statement do? x = random.randint(1, 100)

when you call a ______ function, the function will return a randomly selected integer from 1 to 100 to the variable x

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

when you call a ______ function, the function will return a randomly selected integer from 1 to 20

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

when you call a ______ function, the function will return a randomly selected number from the sequence of values up to, but not including, the ending limit (10 though 19)

What does the following statement do? print(random.random())

when you call a ________ function, it returns a random floating point number in the range of 0.0 up to 1.0 (but not including 1.0)

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

- hard to debug - functions that use global variables are usually dependent on those variables - make a program hard to understand. it can be modified by any statement in the program

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

False

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

False

T/F: 3. Function names should be as short as possible.

False

T/F: 4. Calling a function and defining a function mean the same thing.

False

T/F: 5. A flowchart shows the hierarchical relationships between functions in a program.

False

T/F: 7. A statement in one function can access a local variable in another function.

False

T/F: 8. In Python, you cannot write functions that accept multiple arguments.

False

What happens if the same seed value is always used for generating random numbers?

If we start a new session and repeat the same seed value, we get the same sequence of pseudorandom numbers

Why must you indent statements in a block?

Indentation is required because the Python interpreter uses it to tell where the block begins and ends. ---> tab ---> (commonly used) 4 spaces

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.

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

No, it does not

What does the phrase "calling a function" mean?

To execute a function, a function call is required. The interpreter jumps to that function and executes the statements in that block. When end of block is reached, the interpreter jumps back to part of program that called the function, program resumes execution at that point ('returning'). ex. main() message()

T/F: 11. Some library functions are built into the Python interpreter.

True

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

True

T/F: 14. A function in Python can return more than one value.

True

T/F: 15. 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.

True

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

True

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

True


Ensembles d'études connexes

Chapter 10 - Piping Arrangement Drawing, Section, and Elevation

View Set

Understanding Nutrition Chapter 5

View Set

French History and Culture (PRAXIS)

View Set

Chapter 6, Lesson 2- The Crusades Study Guide

View Set

CHAPTER2 AUTOMATION::USING PYTHON TO INTERACT WITH OS WEEK1

View Set