Functions and Methods: Functions and Methods: Good Things Come in - - PowerPoint PPT Presentation

functions and methods functions and methods
SMART_READER_LITE
LIVE PREVIEW

Functions and Methods: Functions and Methods: Good Things Come in - - PowerPoint PPT Presentation

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


slide-1
SLIDE 1

FIT100 FIT100 FIT100

Concepts of Algorithmic Thinking

Functions and Methods: Functions and Methods:

Good Things Come in Small Packages

D.A. Cle me nts

slide-2
SLIDE 2

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

  • 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.

slide-3
SLIDE 3

FIT100 FIT100 FIT100

Anatomy of a Function

  • F

unc tio ns a re pa c ka g e s fo r a lg o rithms g

  • T

hre e pa rts

N

∗ Na me ∗ Pa ra me te rs

D fi iti

∗ De finitio n

  • T

he se pa rts a re the func tio n p

de c laratio n

slide-4
SLIDE 4

FIT100 FIT100 FIT100

Pick a Name

  • 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> }

slide-5
SLIDE 5

FIT100 FIT100 FIT100

Parameters

  • 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> }

slide-6
SLIDE 6

FIT100 FIT100 FIT100

  • De finitio n is the a lg o rithm writte n in a pro g ra mming

l

Definition

la ng ua g e

  • T
  • 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 nC + 32; re turn 9.0 / 5.0 * te mpI nC + 32; }

  • "Ca lling " a func tio n is to run o r e xe c ute it

W it th f ti ’ t th i t l (

t ) ∗ Write the func tio n’ s na me , put the input va lue s (argume nts)

in the pa re nthe se s c o nve rtC2F ( 38 ); ( );

slide-7
SLIDE 7

FIT100 FIT100 FIT100

slide-8
SLIDE 8

FIT100 FIT100 FIT100

Declaration vs. Call

  • 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

unc tio ns c a n b e c a lle d a s ma ny

  • F

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.

slide-9
SLIDE 9

FIT100 FIT100 FIT100

Forms and Functions

  • 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

slide-10
SLIDE 10

FIT100 FIT100 FIT100

slide-11
SLIDE 11

FIT100 FIT100 FIT100

Calling to Customize a Page

  • 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

) f h i d 1) Be fo re the pa g e is c re a te d

F

  • r e xa mple , with the a le rt() c a ll (slide 7)

2) I nte ra c tive ly a fte r the pa g e is 2) I nte ra c tive ly a fte r the pa g e is displa ye d

F

  • 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

  • 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

slide-12
SLIDE 12

FIT100 FIT100 FIT100

Calling to Customize a Page

  • Ho w a b ro wse r b uilds a pa g e :

∗ Re a ds thro ug h HT

ML file fig uring o ut a ll

∗ Re a ds thro ug h HT

ML file , fig uring o ut a ll ta g s a nd pre pa ring to b uild pa g e R J S i t t d ll t t

∗ Re mo ve s Ja va Sc ript ta g s a nd a ll te xt

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 ()

()

slide-13
SLIDE 13

FIT100 FIT100 FIT100

slide-14
SLIDE 14

FIT100 FIT100 FIT100

Calling to Customize a Page

  • 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

slide-15
SLIDE 15

FIT100 FIT100 FIT100

slide-16
SLIDE 16

Writing Functions Using

FIT100 FIT100 FIT100

Writing Functions, Using Functions

  • 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
slide-17
SLIDE 17

FIT100 FIT100 FIT100

Flipping Electronic Coins

  • 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

a c h time it is c a lle d it g e ne ra te s a

∗ E

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 ti i F li () { func tio n c o inF lip() { re turn Ma th.ro und( Ma th.ra ndo m() );

slide-18
SLIDE 18

FIT100 FIT100 FIT100

Flipping Electronic Coins (cont’d)

  • 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 a ils ra the r tha n te xt He a ds o r T a ils ra the r tha n numb e rs

f nc tio n flipT e t() { func tio n flipT e xt() { if ( c o inF lip() == 0 )

re turn 'T a ils'; e lse re turn 'He a ds';

slide-19
SLIDE 19

FIT100 FIT100 FIT100

Flipping Electronic Coins (cont’d)

  • E

ve n mo re E ve n mo re use ful to g ive

  • utc o me in
  • 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

slide-20
SLIDE 20

Scoping:

FIT100 FIT100 FIT100

Scoping: When to Use Names

  • 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 (th l

l t th f ti

) (the y a re lo c al to the func tio n)

  • 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

slide-21
SLIDE 21

FIT100 FIT100 FIT100

An Annotated Example

slide-22
SLIDE 22

FIT100 FIT100 FIT100

Scoping

  • L
  • 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

  • ne func tio n c a ll to the ne xt, it must

b e in a glo bal variable

slide-23
SLIDE 23

FIT100 FIT100 FIT100

Global/Local Scope Interaction

  • 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; … }

slide-24
SLIDE 24

Global/Local Scope Interaction

FIT100 FIT100 FIT100

Global/Local Scope Interaction (cont'd)

  • 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

slide-25
SLIDE 25

Recap: Two Reasons to Write

FIT100 FIT100 FIT100

Recap: Two Reasons to Write Functions

  • 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

C l it t

∗ Co mple xity ma na g e me nt

  • He lp us ke e p o ur sa nity while we 're so lving

pro b le ms p

slide-26
SLIDE 26

FIT100 FIT100 FIT100

ME T HODS

Built-in Ja va Sc ript func tio ns

ME T HODS

slide-27
SLIDE 27

FIT100 FIT100 FIT100

Methods

  • 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 ();

slide-28
SLIDE 28

FIT100 FIT100 FIT100

Methods—Write your own

  • 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

  • b je c ts
slide-29
SLIDE 29

FIT100 FIT100 FIT100

End papers…

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 h d t h ll th T he pro g ra mme d c o mpute r ha s a ll the 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 .