¢
Po
o
~
2.
Ss
2
Subroutines4.01 Subroutines
huibreutie sa sequence program cosets pears
reprezantneentes/iam
nrsobrautnesinPythonrequve2 sameand the keyword af whichis short fr define.
nen aubrotreicactvated hist releedo as alle, thecalng program shat
en ie refered otresvrouine. Ate te sbrounehas competed ection
ao gonchts ecalng program. This moduaried appreseh 9 TT
Frngewthit advantages ove asinplesequerced Power
Conder a Gul rogram that mantaisis running stats while wating fr various
ern ae becaleby acteoton ol exe ngpers Thesubrovies vee coe
nd pass contol backta he main proeiem™. ,
“Fo tons the ograave ogee te coplte cago fom acre of adi
aaa seated when the cps ded, other elements when ca
aa ef ona poet uteri cade are activated when tet ischanget
st i ce coplentyfteproeram cove any ase sequence code
vas oralabietocheroganee
specie tasbovedoes not
‘Advantages of Using Subroutines
erty cal sibratines om ain code oes number acvonapes:
an becaledivhen reedeo:Asingl block ede con bese nant
“jor repeagcetial code sequences
coge,ratesiveaserto undead and
Thembieu
‘esinthe ene progam, avoiding ene
throughout Thisjmproves thera lth
telpsintheidentiteaton of £70,
Theeisenyone ection of codetodebug fan er sactedinasubroutn ene
roe vpoutine needs tobe debigaes. ad he code beenepeated hyougrout ME
Man program cath crcuvence ould eadto bears,
“Thee sory one sation of code to update: improverens and exter
‘owvatbleeverynere te sobrovtne scaled.
jnsof te code
Typesof Subroutine
“wo main types ofsuboutine ext
«procedures ae smal sections of code that canbe reused, They donoh tur 8a
Poeucocode, = procedueis named atakesthefor
PROCEDURE... ENDPROCEDURE
They recalled by usngine ALL statement.
> fade sites ees sal ndenshocreomoe
epee oth ac oneor movevalies ae retuned tothe mam progam ar
they havecomplctd runing h pseudocode afueion aes thefome “
areca aera
Tica semen sea eae ft
‘passed to the function at the same time: “tonmareseiasrautednastin
po
Catt my forties reply he ftir}
4,02 Programming a Function
Tegan fr dng functonn Python shown:
eat circtete Progamming
cissiete) Fiction seuide
cle yous here twesylabus
Toda acicect as teninthemaiapatothe progam wewouldwite:
otreie (201
enon eras has bon ase othe
seta telucion sal tan
Housea CALL keyword as isused in Cambridge KICSE and Oe peasant
pesicdeshotnconrunesfecinnan eee
Pliers Pane
oe est epee mernane pantera ogc
cee
repens foes wine se eps
Multiples
uncon egied thet wl be pasted
Meco be pate anitegandctput tne ste lesSs
Tre pseudocode fr ths function anit cl em the main program ae shown belo
shown nee
she peeudecade forth hnetin 9 ts al rom thecaln program areas
“isuses FOR aap FOR oopsareinvoduced mee flyin Chapt.
Herel aPython implementation
oe ence teh
yadius = irsCopucC What ig the radius of your etzele? *))
clront - cirountorence (eaaiue)
priss|('The cizcunterence of your circle ia', ctzcuné)
ge soc tthe Multiples procedure the
ME creates, Je algorithm for an amended version of the Multiples procedure that
Foor ure of rufplatonsreqeee ete the fuetonis nat ectvted ty us ofthe yer CALL in Python, Thename ot
thefuncton's ws as varabein an asigment statement. Eachtimethenamesuse,
thefunction's eect and the return vali placed intne viable os output inde te.
Functions That Return Values to the Calling Routing -
ben prorenmnesswte uncon that re reured fo produce answerer epee sts
i eee rere ove valves tothe main prog. Fx example, imightbe necessary fo 2
ropa cal the ccumfence of sere cles fom tera,
Task
ete padocode algrthin faa arended version of his unconthat wht passed
thera ets the ten ofa eile
Test youralastam works programming arruriegthe code in Python,
Circumference
reteand tu neice
‘uncon srequtec that ilbeposea theradusofa ce and
Returning Two Values from a Function
Wiseasyo atu twovalueenpudacodee
RETURN och vale?
InPython, sis accomplished inthe sare way Lock atts interactive sassonoseehaw
thisworkEy Task3
unos yeudente an
fenton Wenge ere
stat yur algostn wonsby xousning ed nringthecogela Pron
oychart agorthe fran amd vein ofthc
theta etaasthe we onthe cca ot
4.03 Programming a Procedure
“ThePyton cacefora proceso tha used fra functon. nhs case ery ranean
Fee etn tharnopantersarerequed bytheswbutne Seehowthis procures ouside
taoisininelneractvesesionshowntelow: thesylabis
otic how ihegreeting() function contains the bitin incon, anes)
Task4
a ene a peicace sigh ora procedural dand_end() hatin 27
Sr oe rerio Tsien be calla smasegareweeneer
Spe esrestne erat apassage
bb ettneyour alent wots by pogtamming te procedueinPython and press
calito thaprocedue
‘Stine ae windopndssco eole at cn ca anne
another route white the pragram is running, tats way subroutines can be ae
‘commen asks within program. “= " a!
+ sree meno arin taro gaa matin rapt
Imeeiierogeithinthemsin prom.
{Subordo otro. One ashe
Spe sation shave camped wciton thy st
Subratnescanbe pesed ates nea apres
( Ayccekee ise teseante out repestvecdeton themaingopan.
| Aten ape ot suvoutne ben eanecoe mule pametesand een ans