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{\longvect}{\overrightarrow}\)
\( \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
- c. The
print()function is called to output the variable,offset_num. - b. Lines 1, 4, and 5 each call
print()once. Lines 2 and 3 call a different function,input(). - b. Line 1 is a comment that is not executed. Lines 2 and 3 each call
float()andinput(). Line 3 callsprint()for a total of five function calls. - b. The corrected line is
def water_plant():. - c. The main program outputs
"User info:"and then callsprint_phone_num(), which outputs the phone number. - b. A function call includes the function name and parentheses.
- a.
calc_taxis concise, in snake case, and indicates the function's task. - 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.
- 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
- c. Line 5 is the start of the main program, which is not indented.
park_greet()is called on line 6 and executed. - b. Control flow moves to where
park_greet()is defined, at line 4. - 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. - c. The program has an indentation mistake.
print("Open ...")is unindented, so the statement is not a part ofpark_greet()and is executed first. Then,park_greet()is called, andprint("Welcome ...")is executed. - c. The
forloop has three iterations. Each iteration callsprint_book_club(), which callsprint_conf(). So the program callsprint_book_club()three times andprint_conf()three times. - b. Control flow returns to line 11, which called
print_conf(). Thenprint_book_club()finishes, and control flow returns to line 16, which calledprint_book_club().
6.3 Variable scope
- c. Both
numandnum_sqare created outside of functions and are global variables. - a.
numis created outside of a function and is global.num_sqis created inprint_square()and is not global. - c.
numis global and can be accessed anywhere in the program, including bothprint_double()andprint_square(). - b.
out_stris the only variable declared inprint_time()and is a local variable. - c.
out_stris accessed inprint_greeting()but created in statements outside ofprint_greeting(). - b.
print_time()createsout_stras a local variable.out_stris only in scope inprint_time()and cannot be accessed elsewhere. - a. The global variable hour is not edited in
update_hour().tmpis the local variable edited.tmpis discarded afterupdate_hour()finishes. - c.
new_houris a local variable that the print statement tries to access afterupdate_hour()finishes.new_houris out of scope, so an error is produced. - b. The
globalkeyword can be used to create a global variable from within a function.new_hourhas global scope and can be accessed outsideupdate_hour().
6.4 Parameters
- b.
usernameis the argument passed toprint_welcome().nameisprint_welcome()'s parameter that is assigned withusername's value. - a.
nameis the only parameter defined withprint_welcome().usernameis the argument passed on line 5's function call. - a.
nameis a parameter that exists only within a call toprint_welcome(). - a. The program would run correctly. Arguments and parameters can have the same name, and a local variable called
namewould still be created inprint_welcome(). Scope can be confusing with variables of the same name. - a.
p1_xis the first argument used in line 9's function call.x1is defined as the first parameter ofcalc_distance(). - b.
y1is defined ascalc_distance()'s second parameter.p1_yis the second argument on line 9's function call. - c.
3is a literal passed as the third argument, which corresponds tox2, the third parameter. - a. A list is mutable, so the object referred to by
wknd_tempsandtempsis modified without needing a local copy. - b. A string is immutable, so
convert_temps()makes a new object forunitto refer to. - a. An immutable object's value cannot be changed.
- b.
unitno longer exists afterconvert_temps()finishes and returns to line 14.
6.5 Return values
- a.
calc_mpg()specifiesmpgto be returned with thereturnstatement. - b.
returnis a valid statement to end the function and return to the calling code without a specified value.calc_sqft()should returnsqftwith the linereturn sqft. - c. If the return value or
returnstatement is not specified, the function returnsNone. - b.
num == 3isTrue, so thereturn "dk"statement executes. - a. For
inc_volume(9,10),return levelreturns9, andlevel += 1is never reached. - b.
bill += tax(bill) + auto_tip(bill)evaluates tobill += 6.0 + 20.0tobill = 100.0 + 6.0 + 20.0to 126.0. - a.
val2 = sq(offset(5))evaluates toval2 = sq(3)to 9.
6.6 Keyword arguments
- d. The function call uses all keyword arguments to assign values to the named parameters.
- b.
"Hiya"is the value ofmsgand"Ash"is the value ofnamein the function. - a.
"Welcome"is unnamed and assigned correctly to count. Positional arguments must come before keyword arguments. - b. The positional arguments,
"Hi"and"Bea", follow the keyword argumentcount=1, which produces an error. - b.
name's default value is"Friend", andcount's default value is1.msgdoes not have default value. - c.
msgis a required argument assigned with"Greetings".nameandcountuse the default values."Greetings Friend"is printed. - b.
msg="Hello",name="Friend", andcount=0, so the call is valid. The function executes but doesn't enter theforloop becausecount=0.


