FIT100 FIT100 FIT100 Concepts of Algorithmic Thinking Functions and Methods: Functions and Methods: Good Things Come in Small Packages D.A. Cle me nts
Functions FIT100 FIT100 FIT100 • A func tio n is a pa c ka g e fo r a n a lg o rithm; o nc e writte n, it c a n b e g ; , use d o ve r a nd o ve r. • Pro fe ssio na l de ve lo pe rs ha ve the ir Pro fe ssio na l de ve lo pe rs ha ve the ir o wn sc ript lib ra rie s the y b ring to e a c h jo b the y wo rk o n e a c h jo b the y wo rk o n.
Anatomy of a Function FIT100 FIT100 FIT100 • F unc tio ns a re pa c ka g e s fo r a lg o rithms g • T hre e pa rts ∗ Na me N ∗ Pa ra me te rs ∗ De finitio n D fi iti he se pa rts a re the func tio n • T p de c laratio n
Pick a Name FIT100 FIT100 FIT100 • Na me is the ide ntifie r fo r the func tio n ∗ Co mmo nly use d to de sc rib e wha t the func tio n do e s • F unc tio n de c la ra tio n fo rm: func tion <na me > ( < parame te r list > ) p ( ) { < state me nt list > }
Parameters FIT100 FIT100 FIT100 • Pa ra me te rs a re the input va lue s the func tio n will c o mpute o n • Pa ra me te rs a re g ive n na me s • I f mo re tha n o ne , the y a re se pa ra te d b y c o mma s • Pa ra me te r na me s fo llo w usua l rule s fo r ide ntifie rs func tion c onve rtC2F ( te mpInC ) { <sta te me nt list> }
Definition FIT100 FIT100 FIT100 • De finitio n is the a lg o rithm writte n in a pro g ra mming l la ng ua g e • T o sa y wha t the a nswe r/ re sult is, Ja va Sc ript use s the sta te me nt: re turn <e xpre ssio n> p func tio n c o nve rtC2F ( te mpI nC ) { re turn 9.0 / 5.0 * te mpI re turn 9 0 / 5 0 * te mpI nC + 32; nC + 32; } • "Ca lling " a func tio n is to run o r e xe c ute it ∗ Write the func tio n’ s na me , put the input va lue s ( argume nts ) W it th f ti ’ t th i t l ( t ) in the pa re nthe se s c o nve rtC2F ( 38 ); ( );
FIT100 FIT100 FIT100
Declaration vs. Call FIT100 FIT100 FIT100 • A func tio n’ s de c la ra tio n is diffe re nt fro m its c a ll (use ) ( ) • F unc tio ns a re de c la re d o nc e • F • F unc tio ns c a n b e c a lle d a s ma ny unc tio ns c a n b e c a lle d a s ma ny time s a s the ir a nswe rs a re ne e de d Declare once Declare once. Call many times.
Forms and Functions FIT100 FIT100 FIT100 • Co nstruc t a we b pa g e in whic h to run a func tio n • E ve nt ha ndle rs usua lly c a ll func tio ns • T he va lue in a n input windo w, o r te xt b o x, c a n b e use d a s a n argume nt to g , a func tio n
FIT100 FIT100 FIT100
Calling to Customize a Page FIT100 FIT100 FIT100 • T hre e wa ys to g e t the re sult o f a func tio n c a ll to print o n the mo nito r 1) Be fo re the pa g e is c re a te d ) f h i d F o r e xa mple , with the a le rt() c a ll (slide 7) 2) I 2) I nte ra c tive ly a fte r the pa g e is nte ra c tive ly a fte r the pa g e is displa ye d F o r e xa mple , the Co nve rsio n a pplic a tio n (slide 10) (slide 10) 3) While the pa g e is b e ing lo a de d F o r e xa mple , do c ume nt.write () b uilt-in p () func tio n • Ca lling func tio ns while the b ro wse r is ti th ll t
Calling to Customize a Page FIT100 FIT100 FIT100 • Ho w a b ro wse r b uilds a pa g e : ∗ Re a ds thro ug h HT ∗ Re a ds thro ug h HT ML ML file fig uring o ut a ll file , fig uring o ut a ll ta g s a nd pre pa ring to b uild pa g e ∗ Re mo ve s Ja va Sc ript ta g s a nd a ll te xt R J S i t t d ll t t b e twe e n the m, a nd do e s wha te ve r the Ja va Sc ript te lls it to do Ja va Sc ript te lls it to do • I t c o uld te ll the b ro wse r to put so me te xt b a c k in the file , a s in do c ume nt.write () ()
FIT100 FIT100 FIT100
Calling to Customize a Page FIT100 FIT100 FIT100 • Suppo se we wa nt a ta b le o f te mpe ra ture c o nve rsio ns fo r a we b p pa g e with a c o lumn fo r Ce lsius a nd a c o lumn fo r F a hre nhe it • Put do c ume nt.write () within the <sc ript> </ sc ript> ta g s to c re a te the <sc ript> </ sc ript> ta g s to c re a te the ro ws o f the ta b le • Put Ce lsius va lue s in first c o lumn c e lls, se c o nd c o lumn c e lls c a n c a ll
FIT100 FIT100 FIT100
Writing Functions Using Writing Functions, Using Functions FIT100 FIT100 FIT100 • F lipping E le c tro nic Co ins ∗ A c o in flip is a n unpre dic ta b le e ve nt ∗ A c o in flip is a n unpre dic ta b le e ve nt who se two o utc o me s a re “e q ua lly pro b a b le ” p ∗ Co mpute rs c a n g e ne ra te pse udo - ra ndo m numb e rs ra ndo m numb e rs • An a lg o rithm tha t pro duc e s a se q ue nc e o f numb e rs tha t pa sse s the sta tistic a l te sts fo r ra ndo mne ss • We c a n just c a ll the m ra ndo m numb e rs
Flipping Electronic Coins FIT100 FIT100 FIT100 • Math.rando m() is Ja va Sc ript’ s b uilt- in func tio n fo r g e ne ra ting ra ndo m g g numb e rs ∗ E ∗ E a c h time it is c a lle d it g e ne ra te s a a c h time it is c a lle d, it g e ne ra te s a ra ndo m numb e r b e twe e n 0 (inc lusive ) a nd 1 (e xc lusive ) ( ) • A func tio n to flip e le c tro nic c o ins: f func tio n c o inF ti i F li () { lip() { re turn Ma th.ro und( Ma th.ra ndo m() );
Flipping Electronic Coins (cont’d) FIT100 FIT100 FIT100 • c o inF lip() re turns with e q ua l pro b a b ility a 0 o r a 1 • Ne xt impro ve me nt is to re turn the te xt He a ds o r T te xt He a ds o r T a ils ra the r tha n a ils ra the r tha n numb e rs f nc tio n flipT func tio n flipT e xt() { e t() { if ( c o inF lip() == 0 ) re turn 'T a ils'; e lse re turn 'He a ds';
Flipping Electronic Coins (cont’d) FIT100 FIT100 FIT100 • E E ve n mo re ve n mo re use ful to g ive o utc o me in o utc o me in re spo nse to pre ssing a pre ssing a b utto n o n a we b pa g e we b pa g e
Scoping: Scoping: When to Use Names FIT100 FIT100 FIT100 • Sc o pe o f a na me de fine s ho w “fa r” fro m its de c la ra tio ns it c a n b e use d • Ge ne ra l rule fo r sc o ping : ∗ Va ria b le na me s de c la re d in a func tio n c a n b e use d o nly within tha t func tio n (the y a re lo c al to the func tio n ) l l t th f ti (th ) • Pa ra me te rs a re c o nside re d lo c a l va ria b le s ∗ Va ria b le na me s de c la re d o utside a ny func tio n c a n b e use d thro ug ho ut the
An Annotated Example FIT100 FIT100 FIT100
Scoping FIT100 FIT100 FIT100 • L o c al variable s c o me into e xiste nc e whe n a func tio n b e g ins, a nd whe n it g , e nds, the y va nish • Glo bal variable s a re a ro und a ll the Glo bal variable s a re a ro und a ll the time • I f info rma tio n must b e sa ve d fro m o ne func tio n c a ll to the ne xt, it must b e in a glo bal variable
Global/Local Scope Interaction FIT100 FIT100 FIT100 • Whe re a g lo b a l va ria b le a nd a lo c a l va ria b le ha ve the sa me na me : va r y=0; … func tio n tric ky (x) { va r y; va r y; y = x; … }
Global/Local Scope Interaction Global/Local Scope Interaction (cont'd) FIT100 FIT100 FIT100 • y is g lo b a lly de c la re d a nd c a n b e re fe re nc e d a nywhe re • y is a lso de c la re d a s a lo c a l va ria b le in the tric ky() func tio n in the tric ky() func tio n • T he y a re two diffe re nt va ria b le s • Whic h y is a ssig ne d the pa ra me te r x? ∗ T he lo c a l y, b e c a use it is de c la re d in the func tio n’ s sc o pe , ma king it the " l " d l i d hidi h
Recap: Two Reasons to Write Recap: Two Reasons to Write Functions FIT100 FIT100 FIT100 • Pa c ka g ing a lg o rithms into func tio ns ∗ Re use • Building b lo c ks o f future pro g ra mming • Ma ke the m a s g e ne ra l a s po ssib le ∗ Co mple xity ma na g e me nt C l it t • He lp us ke e p o ur sa nity while we 're so lving pro b le ms p
Built-in Ja va Sc ript func tio ns HODS HODS T T FIT100 FIT100 FIT100 ME ME
Methods FIT100 FIT100 FIT100 • Me tho ds a re b uilt-in Ja va Sc ript fo r c o mmo nly use d c o de : y ∗ windo w.o pe n() ∗ a le rt(); a le rt(); ∗ pro mpt(); ∗ c o nfirm(); ∗ c o nfirm(); ∗ do c ume nt.write ();
Methods—Write your own FIT100 FIT100 FIT100 • Yo u c a n e ve n write yo ur o wn me tho ds a nd “a tta c h” the m to o b je c ts
End papers… FIT100 FIT100 FIT100 Why is pro g ra mming fun? Why is pro g ra mming fun? • T hird is the fa sc ina tio n o f fa shio ning c o mple x puzzle -like o b je c ts o f c o mple x puzzle like o b je c ts o f inte rlo c king mo ving pa rts a nd wa tc hing the m wo rk in sub tle c yc le s, g pla ying o ut the c o nse q ue nc e s o f princ iple s b uilt in fro m the b e g inning . T T h he pro g ra mme d c o mpute r ha s a ll the d t h ll th fa sc ina tio n o f the pinb a ll ma c hine o r the juke b o x me c ha nism c a rrie d to the the juke b o x me c ha nism, c a rrie d to the ultima te .
Recommend
More recommend