6.1 Defining functions
float()
and input()
. Line 3 calls print()
for a total of five function calls.
"User info:"
and then calls print_phone_num()
, which outputs the phone number.
calc_distance()
10 times is equivalent to 60 statements in the original program.
6.2 Control flow
park_greet()
is called on line 6 and executed.
extra_lot()
, so control flow returns to line 12.
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.
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.
print_conf()
. Then print_book_club()
finishes, and control flow returns to line 16, which called print_book_club()
.
6.3 Variable scope
num
is created outside of a function and is global. num_sq
is created in print_square()
and is not global.
num
is global and can be accessed anywhere in the program, including both print_double()
and print_square()
.
out_str
is accessed in print_greeting()
but created in statements outside of print_greeting()
.
print_time()
creates out_str
as a local variable. out_str
is only in scope in print_time()
and cannot be accessed elsewhere.
update_hour()
. tmp
is the local variable edited. tmp
is discarded after update_hour()
finishes.
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.
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
username
is the argument passed to print_welcome()
. name
is print_welcome()
's parameter that is assigned with username
's value.
name
is the only parameter defined with print_welcome()
. username
is the argument passed on line 5's function call.
name
would still be created in print_welcome()
. Scope can be confusing with variables of the same name.
p1_x
is the first argument used in line 9's function call. x1
is defined as the first parameter of calc_distance()
.
y1
is defined as calc_distance()
's second parameter. p1_y
is the second argument on line 9's function call.
wknd_temps
and temps
is modified without needing a local copy.
6.5 Return values
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
.
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
"Welcome"
is unnamed and assigned correctly to count. Positional arguments must come before keyword arguments.
"Hi"
and
"Bea"
, follow the keyword argument count=1
, which produces an error.
name
's default value is
"Friend"
, and count
's default value is
1
. msg
does not have default value.
msg
is a required argument assigned with
"Greetings"
. name
and count
use the default values.
"Greetings Friend"
is printed.
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
.