6.1 Defining functions
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.
5.
c.
The main program outputs
"User info:"
and then calls print_phone_num()
, which outputs the phone number.
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.
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
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()
.
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.
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.
8.
a.
A list is mutable, so the object referred to by
wknd_temps
and temps
is modified without needing a local copy.
6.5 Return values
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
.
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.
6.6 Keyword arguments
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
.