16.5: Chapter 6
( \newcommand{\kernel}{\mathrm{null}\,}\)
6.1 Defining functions
1.
c. The
print()
function is called to output the variable, offset_num
.
2.
b. Lines 1, 4, and 5 each call
print()
once. Lines 2 and 3 call a different function, input()
.
3.
b. Line 1 is a comment that is not executed. Lines 2 and 3 each call
float()
and input()
. Line 3 calls print()
for a total of five function calls.
4.
b. The corrected line is
def water_plant():
.
5.
c. The main program outputs
"User info:"
and then calls print_phone_num()
, which outputs the phone number.
6.
b. A function call includes the function name and parentheses.
7.
a.
calc_tax
is concise, in snake case, and indicates the function's task.
8.
c. Each calculation uses four statements to read in the x,y values of a pair of points. The original program performs three calculations by repeating code for a total of 12 input statements. The revised program performs three calculations by executing the four input statements multiple times with function calls.
9.
c. The benefits of a function's reusability is highlighted by many function calls that drastically reduce the amount of cluttered, redundant code. Calling
calc_distance()
10 times is equivalent to 60 statements in the original program.
6.2 Control flow
1.
c. Line 5 is the start of the main program, which is not indented.
park_greet()
is called on line 6 and executed.
2.
b. Control flow moves to where
park_greet()
is defined, at line 4.
3.
c. Control flow returns to the line that called the function. Line 12 contains the function call to
extra_lot()
, so control flow returns to line 12.
4.
c. The program has an indentation mistake.
print("Open ...")
is unindented, so the statement is not a part of park_greet()
and is executed first. Then, park_greet()
is called, and print("Welcome ...")
is executed.
5.
c. The
for
loop has three iterations. Each iteration calls print_book_club()
, which calls print_conf()
. So the program calls print_book_club()
three times and print_conf()
three times.
6.
b. Control flow returns to line 11, which called
print_conf()
. Then print_book_club()
finishes, and control flow returns to line 16, which called print_book_club()
.
6.3 Variable scope
1.
c. Both
num
and num_sq
are created outside of functions and are global variables.
2.
a.
num
is created outside of a function and is global. num_sq
is created in print_square()
and is not global.
3.
c.
num
is global and can be accessed anywhere in the program, including both print_double()
and print_square()
.
4.
b.
out_str
is the only variable declared in print_time()
and is a local variable.
5.
c.
out_str
is accessed in print_greeting()
but created in statements outside of print_greeting()
.
6.
b.
print_time()
creates out_str
as a local variable. out_str
is only in scope in print_time()
and cannot be accessed elsewhere.
7.
a. The global variable hour is not edited in
update_hour()
. tmp
is the local variable edited. tmp
is discarded after update_hour()
finishes.
8.
c.
new_hour
is a local variable that the print statement tries to access after update_hour()
finishes. new_hour
is out of scope, so an error is produced.
9.
b. The
global
keyword can be used to create a global variable from within a function. new_hour
has global scope and can be accessed outside update_hour()
.
6.4 Parameters
1.
b.
username
is the argument passed to print_welcome()
. name
is print_welcome()
's parameter that is assigned with username
's value.
2.
a.
name
is the only parameter defined with print_welcome()
. username
is the argument passed on line 5's function call.
3.
a.
name
is a parameter that exists only within a call to print_welcome()
.
4.
a. The program would run correctly. Arguments and parameters can have the same name, and a local variable called
name
would still be created in print_welcome()
. Scope can be confusing with variables of the same name.
5.
a.
p1_x
is the first argument used in line 9's function call. x1
is defined as the first parameter of calc_distance()
.
6.
b.
y1
is defined as calc_distance()
's second parameter. p1_y
is the second argument on line 9's function call.
7.
c.
3
is a literal passed as the third argument, which corresponds to x2
, the third parameter.
8.
a. A list is mutable, so the object referred to by
wknd_temps
and temps
is modified without needing a local copy.
9.
b. A string is immutable, so
convert_temps()
makes a new object for unit
to refer to.
10.
a. An immutable object's value cannot be changed.
11.
b.
unit
no longer exists after convert_temps()
finishes and returns to line 14.
6.5 Return values
1.
a.
calc_mpg()
specifies mpg
to be returned with the return
statement.
2.
b.
return
is a valid statement to end the function and return to the calling code without a specified value. calc_sqft()
should return sqft
with the line return sqft
.
3.
c. If the return value or
return
statement is not specified, the function returns None
.
4.
b.
num == 3
is True
, so the return "dk"
statement executes.
5.
a. For
inc_volume(9,10)
, return level
returns 9
, and level += 1
is never reached.
6.
b.
bill += tax(bill) + auto_tip(bill)
evaluates to bill += 6.0 + 20.0
to bill = 100.0 + 6.0 + 20.0
to 126.0.
7.
a.
val2 = sq(offset(5))
evaluates to val2 = sq(3)
to 9.
6.6 Keyword arguments
1.
d. The function call uses all keyword arguments to assign values to the named parameters.
2.
b.
"Hiya"
is the value of msg
and "Ash"
is the value of name
in the function.
3.
a.
"Welcome"
is unnamed and assigned correctly to count. Positional arguments must come before keyword arguments.
4.
b. The positional arguments,
"Hi"
and "Bea"
, follow the keyword argument count=1
, which produces an error.
5.
b.
name
's default value is "Friend"
, and count
's default value is 1
. msg
does not have default value.
6.
c.
msg
is a required argument assigned with "Greetings"
. name
and count
use the default values. "Greetings Friend"
is printed.
7.
b.
msg="Hello"
, name="Friend"
, and count=0
, so the call is valid. The function executes but doesn't enter the for
loop because count=0
.