16.5: Chapter 6
- Page ID
- 117501
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\( \newcommand{\dsum}{\displaystyle\sum\limits} \)
\( \newcommand{\dint}{\displaystyle\int\limits} \)
\( \newcommand{\dlim}{\displaystyle\lim\limits} \)
\( \newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\)
( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\id}{\mathrm{id}}\)
\( \newcommand{\Span}{\mathrm{span}}\)
\( \newcommand{\kernel}{\mathrm{null}\,}\)
\( \newcommand{\range}{\mathrm{range}\,}\)
\( \newcommand{\RealPart}{\mathrm{Re}}\)
\( \newcommand{\ImaginaryPart}{\mathrm{Im}}\)
\( \newcommand{\Argument}{\mathrm{Arg}}\)
\( \newcommand{\norm}[1]{\| #1 \|}\)
\( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\)
\( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\AA}{\unicode[.8,0]{x212B}}\)
\( \newcommand{\vectorA}[1]{\vec{#1}} % arrow\)
\( \newcommand{\vectorAt}[1]{\vec{\text{#1}}} % arrow\)
\( \newcommand{\vectorB}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vectorC}[1]{\textbf{#1}} \)
\( \newcommand{\vectorD}[1]{\overrightarrow{#1}} \)
\( \newcommand{\vectorDt}[1]{\overrightarrow{\text{#1}}} \)
\( \newcommand{\vectE}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash{\mathbf {#1}}}} \)
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \)
\( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)
\(\newcommand{\avec}{\mathbf a}\) \(\newcommand{\bvec}{\mathbf b}\) \(\newcommand{\cvec}{\mathbf c}\) \(\newcommand{\dvec}{\mathbf d}\) \(\newcommand{\dtil}{\widetilde{\mathbf d}}\) \(\newcommand{\evec}{\mathbf e}\) \(\newcommand{\fvec}{\mathbf f}\) \(\newcommand{\nvec}{\mathbf n}\) \(\newcommand{\pvec}{\mathbf p}\) \(\newcommand{\qvec}{\mathbf q}\) \(\newcommand{\svec}{\mathbf s}\) \(\newcommand{\tvec}{\mathbf t}\) \(\newcommand{\uvec}{\mathbf u}\) \(\newcommand{\vvec}{\mathbf v}\) \(\newcommand{\wvec}{\mathbf w}\) \(\newcommand{\xvec}{\mathbf x}\) \(\newcommand{\yvec}{\mathbf y}\) \(\newcommand{\zvec}{\mathbf z}\) \(\newcommand{\rvec}{\mathbf r}\) \(\newcommand{\mvec}{\mathbf m}\) \(\newcommand{\zerovec}{\mathbf 0}\) \(\newcommand{\onevec}{\mathbf 1}\) \(\newcommand{\real}{\mathbb R}\) \(\newcommand{\twovec}[2]{\left[\begin{array}{r}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\ctwovec}[2]{\left[\begin{array}{c}#1 \\ #2 \end{array}\right]}\) \(\newcommand{\threevec}[3]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\cthreevec}[3]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \end{array}\right]}\) \(\newcommand{\fourvec}[4]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\cfourvec}[4]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \end{array}\right]}\) \(\newcommand{\fivevec}[5]{\left[\begin{array}{r}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\cfivevec}[5]{\left[\begin{array}{c}#1 \\ #2 \\ #3 \\ #4 \\ #5 \\ \end{array}\right]}\) \(\newcommand{\mattwo}[4]{\left[\begin{array}{rr}#1 \amp #2 \\ #3 \amp #4 \\ \end{array}\right]}\) \(\newcommand{\laspan}[1]{\text{Span}\{#1\}}\) \(\newcommand{\bcal}{\cal B}\) \(\newcommand{\ccal}{\cal C}\) \(\newcommand{\scal}{\cal S}\) \(\newcommand{\wcal}{\cal W}\) \(\newcommand{\ecal}{\cal E}\) \(\newcommand{\coords}[2]{\left\{#1\right\}_{#2}}\) \(\newcommand{\gray}[1]{\color{gray}{#1}}\) \(\newcommand{\lgray}[1]{\color{lightgray}{#1}}\) \(\newcommand{\rank}{\operatorname{rank}}\) \(\newcommand{\row}{\text{Row}}\) \(\newcommand{\col}{\text{Col}}\) \(\renewcommand{\row}{\text{Row}}\) \(\newcommand{\nul}{\text{Nul}}\) \(\newcommand{\var}{\text{Var}}\) \(\newcommand{\corr}{\text{corr}}\) \(\newcommand{\len}[1]{\left|#1\right|}\) \(\newcommand{\bbar}{\overline{\bvec}}\) \(\newcommand{\bhat}{\widehat{\bvec}}\) \(\newcommand{\bperp}{\bvec^\perp}\) \(\newcommand{\xhat}{\widehat{\xvec}}\) \(\newcommand{\vhat}{\widehat{\vvec}}\) \(\newcommand{\uhat}{\widehat{\uvec}}\) \(\newcommand{\what}{\widehat{\wvec}}\) \(\newcommand{\Sighat}{\widehat{\Sigma}}\) \(\newcommand{\lt}{<}\) \(\newcommand{\gt}{>}\) \(\newcommand{\amp}{&}\) \(\definecolor{fillinmathshade}{gray}{0.9}\)6.1 Defining functions
print() function is called to output the variable, offset_num.
print() once. Lines 2 and 3 call a different function, input().
float() and input(). Line 3 calls print() for a total of five function calls.
def water_plant(): .
"User info:" and then calls print_phone_num(), which outputs the phone number.
calc_tax is concise, in snake case, and indicates the function's task.
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.
park_greet() is defined, at line 4.
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 and num_sq are created outside of functions and are global variables.
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 the only variable declared in print_time() and is a local variable.
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 is a parameter that exists only within a call to print_welcome().
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.
3 is a literal passed as the third argument, which corresponds to x2, the third parameter.
wknd_temps and temps is modified without needing a local copy.
convert_temps() makes a new object for unit to refer to.
unit no longer exists after convert_temps() finishes and returns to line 14.
6.5 Return values
calc_mpg() specifies mpg to be returned with the return statement.
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.
return statement is not specified, the function returns None .
num == 3 is True , so the return "dk" statement executes.
inc_volume(9,10), return level returns 9 , and level += 1 is never reached.
bill += tax(bill) + auto_tip(bill) evaluates to bill += 6.0 + 20.0 to bill = 100.0 + 6.0 + 20.0 to 126.0.
val2 = sq(offset(5)) evaluates to val2 = sq(3) to 9.
6.6 Keyword arguments
"Hiya" is the value of msg and "Ash" is the value of name in the function.
"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.


