+ets go ahea% an% !reate o&r get_at_content f&n!tion. (efore e start typing, e nee% to fig&re o&t hat the inp&ts the n&mber an% types of the function
arguments= an% o&tp&ts the type of the return value= are going to be. For this f&n!tion, that seems pretty obvio&s 5 the inp&t is going to be a singe D;4
se<&en!e, an% the o&tp&t is going to be a %e!ima n&mber. $o transate these into #ython terms: the f&n!tion i ta-e a singe arg&ment of type string , an% i ret&rn a va&e of type number 1. 7eres the !o%e:
def get/at/content(dna#! length B len(dna#
a/count B dna.count(A# t/count B dna.count()#
at/content B (a/count C t/count# length return at/content
Reminder: if yo&re &sing #ython 2 rather than #ython 3, in!&%e this ine at the top of yo&r program:
from //future// import diision
$he first ine of the f&n!tion %efinition !ontains a severa %ifferent eements. e start ith the or% def, hi!h is short for "efine riting a f&n!tion is !ae% "efining it=. Fooing that e rite the name of the f&n!tion, fooe% by the names of the arg&ment variabes in parentheses. J&st i-e e sa before ith
1 'n fa!t, e !an be a itte bit more spe!ifi!: e !an say that the ret&rn va&e i be of type float 5 a foating point n&mber i.e. one ith a %e!ima point=.
1
10011 CChhaapptteer r : : rriittiinng g oo&&r r oon n ff&&nn!!ttiioonnss
norma variabes, the f&n!tion name an% the
norma variabes, the f&n!tion name an% the arg&ment names are arg&ment names are arbitrary 5 arbitrary 5 ee !an &se hatever e i-e.
!an &se hatever e i-e. $he first ine en%s
$he first ine en%s ith a !oon, 9&st i-ith a !oon, 9&st i-e the first e the first ine of the oops ine of the oops that e erethat e ere oo-ing at in the previo&s !hapter. 4n% 9&st i-e oops, this ine is fooe% by a oo-ing at in the previo&s !hapter. 4n% 9&st i-e oops, this ine is fooe% by a block
block of in%ente% of in%ente% ines 5 theines 5 the function bo"y function bo"y . $he f&n!tion bo%y !an have as . $he f&n!tion bo%y !an have as manymany ines of !o%e as
ines of !o%e as e i-e, as ong as they a e i-e, as ong as they a have the same have the same in%entation. in%entation. ithin theithin the f&n!tion bo%y
f&n!tion bo%y, e !an refer to the arg&ments by &sing the variabe names , e !an refer to the arg&ments by &sing the variabe names from thefrom the first ine. 'n this
first ine. 'n this !ase, the variabe!ase, the variabe dnadna refers to the se<&en!e that as passe% in as refers to the se<&en!e that as passe% in as the arg&ment to
the arg&ment to the f&n!tion.the f&n!tion. $he ast ine of
$he ast ine of the f&n!tion !a&ses it to ret&rn the 4$ !onthe f&n!tion !a&ses it to ret&rn the 4$ !ontent that as !a!&ate%tent that as !a!&ate% in the f&n!tion bo%y. $o
in the f&n!tion bo%y. $o returnreturn from a from a f&n!tion, f&n!tion, e simpy rite e simpy rite ret&rn fooe%ret&rn fooe% by the va&e that
by the va&e that the f&n!tion sho&% o&tp&t.the f&n!tion sho&% o&tp&t. $here are a
$here are a !o&pe of important things to be aare of hen riting f&n!tions.!o&pe of important things to be aare of hen riting f&n!tions. Firsty,
Firsty, e nee% to e nee% to ma-e a !ear ma-e a !ear %istin!tion beteen%istin!tion beteen "efining "efining a f&n!tion, an% a f&n!tion, an% running
running it e refer to r&nning a f&n!tion as it e refer to r&nning a f&n!tion as calling calling it=. it=. $he !o%e $he !o%e eve eve rittenritten
above i not !a&se anything to happen hen e r&n it, be!a&se eve not a!t&ay above i not !a&se anything to happen hen e r&n it, be!a&se eve not a!t&ay as-e% #ython to ee!&te the
as-e% #ython to ee!&te the get_at_contentget_at_content f&n!tion 5 e have simpy %efine% f&n!tion 5 e have simpy %efine% hat it is.
hat it is. $he !o%e in the f&n!$he !o%e in the f&n!tion i notion i not be ee!&te% &nt be ee!&te% &nti e !a the f&n!ti e !a the f&n!tiontion i-e this:
i-e this:
get/at/content(*A)%A&)%%A&&A*# get/at/content(*A)%A&)%%A&&A*#
'f e simpy !a the f&n!tion i-e that, hoever, then the 4$ !ontent i vanish 'f e simpy !a the f&n!tion i-e that, hoever, then the 4$ !ontent i vanish on!e it
on!e its been s been !a!&ate%. '!a!&ate%. 'n or%er to n or%er to &se the &se the f&n!tion to %o something &sef&, ef&n!tion to %o something &sef&, e m&st either store the res&t in a variabe:
m&st either store the res&t in a variabe:
at/content B get/at/content(*A)%A&)%%A&&A*# at/content B get/at/content(*A)%A&)%%A&&A*#
?r &se it %ire!ty: ?r &se it %ire!ty:
1
10022 CChhaapptteer r : : rriittiinng g oo&&r r oon n ff&&nn!!ttiioonnss
print(*A) content is * C str(get/at/content(*A)%A&)%%A&&A*### print(*A) content is * C str(get/at/content(*A)%A&)%%A&&A*###
"e!on%y, i
"e!on%y, its important to &n%erstan% that the ts important to &n%erstan% that the arg&ment variabearg&ment variabe dnadna %oes not %oes not ho% any parti!&ar va&e hen the f&n!tion is %efine%
ho% any parti!&ar va&e hen the f&n!tion is %efine%11. 'nstea%, its 9ob is to ho%. 'nstea%, its 9ob is to ho% hatever va
hatever va&e is given as the arg&ment &e is given as the arg&ment hen the f&n!tion is !ahen the f&n!tion is !ae%. 'n e%. 'n this ay itthis ay itss anaogo&s to the oop variabes e sa in the pr
anaogo&s to the oop variabes e sa in the previo&s !hapter: oop variabes ho%evio&s !hapter: oop variabes ho% a %ifferent va&e ea!h time ro&n% the
a %ifferent va&e ea!h time ro&n% the oop, anoop, an% f&n!tion arg&ment variabes ho% a% f&n!tion arg&ment variabes ho% a %ifferent va&e ea!h time
%ifferent va&e ea!h time the f&n!tion is !ae%.the f&n!tion is !ae%. Finay
Finay, be aare that the same s!oping r&es that appie% t, be aare that the same s!oping r&es that appie% to oops aso appy too oops aso appy to f&n!tions 5
f&n!tions 5 any variabes that e !reate as part of any variabes that e !reate as part of the f&n!tion ony eist insi%e thethe f&n!tion ony eist insi%e the f&n!tion, an
f&n!tion, an% !annot be a!!esse% o&tsi%e. 'f e tr% !annot be a!!esse% o&tsi%e. 'f e try to &se y to &se a variabe thats !reate%a variabe thats !reate% insi%e the f&n!tion from o&tsi%e:
insi%e the f&n!tion from o&tsi%e:
def get/at/content(dna#! def get/at/content(dna#! length B len(dna# length B len(dna# a/count B dna.count(A# a/count B dna.count(A# t/count B dna.count()# t/count B dna.count()#
at/content B (a/count C t/count# length at/content B (a/count C t/count# length return at/content
return at/content print(a/count#
print(a/count#
e get an error: e get an error:
<ame9rror! name a/count is not defined <ame9rror! name a/count is not defined
1
1
10033 CChhaapptteer r : : rriittiinng g oo&&r r oon n ff&&nn!!ttiioonnss