67
APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6, 2012

Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

  • Upload
    others

  • View
    1

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

APS105Winter 2012

Jonathan Deberjdeber -at- cs -dot- toronto -dot- edu

Lecture 10February 6, 2012

Page 2: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Today

2

• More Arrays

• Functions

Page 3: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Array

3

• A connected group of variables

!"#$!%!$&$'(%

!

)* (+ '*,!"#$-.)/%-.0/$&$)*%-.'/$&$(+%-.*/$&$'*,%

-

0 ' *

-.0/ -.'/ -.*/

'( index

Page 4: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

4

What’s the type of -?What’s the type of -.0/?

What’s the type of -.'/?

What’s the type of -.*/?

array of !"#!"#

!"#$1$&$2%-.0/$&$1$3$*%!"#$456$&$-.0/$3$-.'/%-.0/33%

!7$8-.0/$9$:;<=>$$$???@

-$&$'+%

!"#$-.)/%-.0/$&$)*%-.'/$&$(+%-.*/$&$'*,%

)* (+ '*,-

-.0/ -.'/ -.*/

!"#

!"#

lvalues}

Error

Page 5: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

5

!"#$-.)/%-.0/$&$)*%-.'/$&$(+%-.*/$&$'*,%

)* (+ '*,-

-.0/ -.'/ -.*/

-.)/$&$*A(%

-.)/

*A(

Page 6: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

6

Initializing Arrays!"#$!%!$&$A%

!"#$!$&$A%

!"#$-.A/%-.0/$&$'%-.'/$&$*%-.*/$&$)%-.)/$&$+%-.+/$&$A%

!"#$-.A/$&$>'B$*B$)B$+B$A@%

!"#$-./$$&$>'B$*B$)B$+B$A@%

' * ) + A-

' * ) + A-

Page 7: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

7

Iterating Arrays

!"#$-.C/%$$$$7DE$8!"#$!$&$0%$!$F$C%$!33=>$$$-.!/$&$!%@$$$$7DE$8!"#$!$&$0%$!$F$C%$!33=>$$$GE!"#78HIJK"HB$-.!/=%@

LJM7!"M$C$'0???

Page 8: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

8

Copying Arrays

• This will not work:

!"#$-.+/$&$>'B$*B$)B$+@%!"#$N.+/%

N$&$-%

!"#$!$&$,%!"#$O%

O$&$!%Error

Page 9: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Copying Arrays

9

!"#$-.+/$&$>'B$*B$)B$+@%!"#$N.+/%

7DE$8!"#$!$&$0%$!$F$+%$!33=>$$$N.!/$&$-.!/%@

Page 10: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Other Types

10

NDDP$N.)/%N.0/$&$#E5M%N.'/$&$7-P4M%N.*/$&$N.0/$QQ$N.'/%$

JD5NPM$RM!ST#.)/%RM!ST#.0/$&$)?+%RM!ST#.'/$&$*%RM!ST#.*/$&$)?'$3$RM!ST#.0/%

UT-E$!"!#!-P4.)/%!"!#!-P4.0/$&$VWV%!"!#!-P4.'/$&$V;V%!"!#!-P4.*/$&$VXV%$

Page 11: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

11

'-based indexing• Arrays are 0-based

• If you need '-based indexing, just ignore -.0/

#!6M4.0/First place: 22.4 secondsSecond place: 24.9 secondsThird place: 27.8 seconds

#!6M4.'/#!6M4.*/

JD5NPM$#!6M4.)/$&$>**?+B$*+?2B$*Y?,@%

JD5NPM$#!6M4.+/$&$>0?0B$**?+B$*+?2B$*Y?,@%

#!6M4.'/

#!6M4.*/#!6M4.)/

ZZ$[S"DE!"S$#!6M4.0/$#D$GED\!JM$']N-4MJ$!"JM1!"S

Page 12: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Multi-Dimensional Arrays

12

0 ' ) A Y

'-dimensional

0 ' ) A Y

2 ') 'Y '2 *)

*Y )' )Y +' +)

+Y A) A2 (' (Y

*-dimensional

!"#$-.A/% !"#$-.+/.A/%

row

col

Page 13: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

13

!"#$^$&$-.)B*/%

!"#$-.+/.A/%???ZZ$;4456M$!#$SM#4$7!PPMJ$!"$TMEM???!"#$!$&$-.*/.0/%

0 ' ) A Y

2 ') 'Y '2 *)

*Y )' )Y +' +)

+Y A) A2 (' (Y

col 0 ' * ) +

0

'

*

)

row

Wrong

Wrong

!"#$O$&$-.'/%

row

col

*Y

Page 14: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

14

!"#$6.)/.+/$&$>$>*B$+B$(B$,@B$$$$$$$$$$$$$$$$>'B$)B$AB$Y@B$$$$$$$$$$$$$$$$>2B$,B$YB$(@$@%

!"#$".)/.+/$&$>$>*B$+B$(B$,@B$$$$$$$$$$$$$$$$>'B$)B$AB$Y@$@%

!"#$".)/.+/$&$>$>*B$+B$(B$,@B$$$$$$$$$$$$$$$$>'B$)B$AB$Y@B$$$$$$$$$$$$$$$$>0B$0B$0B$0@$@%

!"#$D.)/.+/$&$>$>*B$+B$(@B$$$$$$$$$$$$$$$$>'B$)B$A@B$$$$$$$$$$$$$$$$>2B$,B$Y@$@%

!"#$D.)/.+/$&$>$>*B$+B$(B$0@B$$$$$$$$$$$$$$$$>'B$)B$AB$0@B$$$$$$$$$$$$$$$$>2B$,B$YB$0@$@%

Page 15: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

15

$$$$!"#$6.C/.C/%$$$$$$$$7DE$8!"#$EDR$&$0%$EDR$F$C%$EDR33=$$$$>$$$$$$$$7DE$8!"#$UDP$&$0%$UDP$F$C%$UDP33=$$$$$$$$>$$$$$$$$$$$$!7$8EDR$&&$UDP=$$$$$$$$$$$$>$$$$$$$$$$$$$$$$6.EDR/.UDP/$&$'%$$$$$$$$$$$$@$$$$$$$$$$$$MP4M$$$$$$$$$$$$>$$$$$$$$$$$$$$$$6.EDR/.UDP/$&$0%$$$$$$$$$$$$@$$$$$$$$@$$$$@

Multi-Dimensional Iteration

Page 16: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

16

$$$$7DE$8!"#$EDR$&$0%$EDR$F$C%$EDR33=$$$$>$$$$$$$$7DE$8!"#$UDP$&$0%$UDP$F$C%$UDP33=$$$$$$$$>$$$$$$$$$$$$GE!"#78HIJ$HB$6.EDR/.UDP/=%$$$$$$$$@$$$$$$$$GE!"#78HK"H=%$$$$@

Multi-Dimensional Iteration

Page 17: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

17

__________

TM!ST#`$+TM!ST# rowsTM!ST#$]$!$H_H in row !

$$$$7DE$8!"#$!$&$0%$!$F$TM!ST#%$!33=$$$$>

$$$$$$GE!"#78HK"H=%$$$$@

$$$$$$$$7DE$8!"#$O$&$0%$O$F$TM!ST#$]$!%$O33=$$$$$$$$>$$$$$$$$$$$$GE!"#78H_H=%$$$$$$$$@

(0-based index)

Page 18: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

17

__________

TM!ST#`$+TM!ST# rowsTM!ST#$]$!$H_H in row !

$$$$7DE$8!"#$!$&$0%$!$F$TM!ST#%$!33=$$$$>

$$$$$$GE!"#78HK"H=%$$$$@

$$$$$$$$7DE$8!"#$O$&$0%$O$F$TM!ST#$]$!%$O33=$$$$$$$$>$$$$$$$$$$$$GE!"#78H_H=%$$$$$$$$@

(0-based index)

UT-E$#E!-"SPM.TM!ST#/.TM!ST#/%

Page 19: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

18

$$$$7DE$8!"#$!$&$0%$!$F$TM!ST#%$!33=$$$$>

$$$$$$GE!"#78HK"H=%$$$$@

$$$$$$$$7DE$8!"#$O$&$0%$O$F$TM!ST#$]$!%$O33=$$$$$$$$>$$$$$$$$$$$$#E!-"SPM.!/.O/$&$H_H%$$$$$$$$@

__________

TM!ST#`$+TM!ST# rowsTM!ST#$]$!$H_H in row !

(0-based index)

UT-E$#E!-"SPM.TM!ST#/.TM!ST#/%

Page 20: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

19

$$$$7DE$8!"#$!$&$0%$!$F$TM!ST#%$!33=$$$$>

$$$$$$GE!"#78HK"H=%$$$$@

$$$$$$$$7DE$8!"#$O$&$0%$O$F$TM!ST#$]$!%$O33=$$$$$$$$>$$$$$$$$$$$$#E!-"SPM.!/.O/$&$V_V%$$$$$$$$@

__________

TM!ST#`$+TM!ST# rowsTM!ST#$]$!$H_H in row !

(0-based index)

UT-E$#E!-"SPM.TM!ST#/.TM!ST#/%

Page 21: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

20

V_V V_V V_V V_V

V_V V_V V_V

V_V V_V

V_V

#E!-"SPM

Page 22: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

20

V_V V_V V_V V_V

V_V V_V V_V

V_V V_V

V_V

a

aa

aa a

#E!-"SPM

Page 23: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

20

V_V V_V V_V V_V

V_V V_V V_V

V_V V_V

V_V

#E!-"SPM

V$V

V$V

V$VV$VV$V

V$V

Page 24: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

21

$$$$7DE$8!"#$!$&$0%$!$F$TM!ST#%$!33=$$$$>

$$$$$$GE!"#78HK"H=%$$$$@

$$$$$$$$7DE$8!"#$O$&$0%$O$F$TM!ST#$]$!%$O33=$$$$$$$$>$$$$$$$$$$$$#E!-"SPM.!/.O/$&$V_V%$$$$$$$$@

__________

TM!ST#`$+TM!ST# rowsTM!ST#$]$!$H_H in row !

(0-based index)

UT-E$#E!-"SPM.TM!ST#/.TM!ST#/%

and !$H$H in row !

Page 25: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

22

$$$$

$$$$$$

$$$$$$$$7DE$8$$$$$$$$$$$$>$$$$$$$$$$$$#E!-"SPM.!/.O/$&$V_V%$$$$$$$$@

__________

TM!ST#`$+TM!ST# rowsTM!ST#$]$!$H_H in row !

(0-based index)

UT-E$#E!-"SPM.TM!ST#/.TM!ST#/%7DE$8!"#$!$&$0%$!$F$TM!ST#%$!33=>

$$GE!"#78HK"H=%@

!"#$OO$&$0%$O$F$TM!ST#$]$!%$O33=

7DE$8$%$O$F$TM!ST#%$O33=>$$$$#E!-"SPM.!/.O/$&$V$V%@

and !$H$H in row !

%

Page 26: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

23

$$$$7DE$8!"#$!$&$0%$!$F$TM!ST#%$!33=$$$$>$$$$$$$$7DE$8!"#$O$&$0%$O$F$TM!ST#%$O33=$$$$$$$$>$$$$$$$$$$$$GE!"#78HIUHB$#E!-"SPM.!/.O/=%$$$$$$$$@$$$$$$$$$$$$$$$$GE!"#78HK"H=%$$$$@

Page 27: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions

24

Page 28: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions

25

• Building blocks of programs

• Most code must be inside a function

• Programming involves writing and using functions

• C comes with a bunch of them

• Functions can receive and/or return information

Page 29: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

26

Input (optional) Output (optional)

Parameter(s) Return Value

Function

Page 30: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions

• We’ve seen three functions so far

• 6-!"8=

• GE!"#78=

• 4U-"78=

27

Page 31: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

28

GE!"#78HbMPPDK"H=%

name of function call it

with these parameters

Input (optional) Output (optional)

Parameter(s) Return Value

Function

}

Page 32: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Defining Functions

29

!"#$6-!"8\D!J=>$$$???@

!"#$!%&#'("$)*"%#)+)"!8(,!,-"#"!.=////.#,#"-"%#

Page 33: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Defining Functions

30

• !"#$!%&#'("

• Any type (e.g., !"#, JD5NPM, NDDP, etc.), but not arrays

• \D!J

• )*"%#)+)"!

• Same rules as variable naming, but must be unique

• (,!,-"#"!.

• List of variable types and names

• Any type, including arrays

c \D!J

Page 34: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Defining Functions• I’m defining a function called 6-!"

• It returns something of type !"#

• It takes \D!J parameters (i.e., no parameters)

31

!"#$6-!"8\D!J=>$$$???

$$$EM#5E"$0%@

Page 35: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Defining Functions• I’m defining a function called -\ME-SM

• It returns something of type JD5NPM

• It takes two parameters

• ') double named - and 2) JD5NPM named N

32

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=>$$$EM#5E"$8-$3$N=$Z$*%@

Page 36: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Defining Functions• I’m defining a function called -\ME-SM

• It returns something of type JD5NPM

• It takes two parameters

• ') double named - and 2) JD5NPM named N

32

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=>$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%{

Page 37: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Using Functions

33

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

\D!J$GE!"#de5-EMJ8!"#$"=

\D!J$GE!"#bMPPD8\D!J=

!"#$SM#dGMMJ8\D!J=

Page 38: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

Page 39: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

-\S`JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 40: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

A?0

JD5NPM$-%$JD5NPM$N%

-\S`JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 41: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

A?0 *?0

JD5NPM$-%$JD5NPM$N%

-\S`JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 42: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

A?0 *?0

-$&$A?0%N$&$*?0%

JD5NPM$-%$JD5NPM$N%

-\S`JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 43: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

A?0 *?0

-$&$A?0%N$&$*?0%

JD5NPM$-%$JD5NPM$N%

)?A

-\S`JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 44: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

)?A

-\S`JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 45: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

)?A

JD5NPM$-%$JD5NPM$N%

-\S`JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 46: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

)?A

JD5NPM$-%$JD5NPM$N%

-\S` )?AJD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 47: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

-\S` )?AJD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 48: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

'?0

-\S` )?AJD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 49: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

'?0 '2?0

-\S` )?AJD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 50: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

'?0 '2?0

-$&$$'?0%N$&$'2?0%

-\S` )?AJD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 51: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

'?0 '2?0

-$&$$'?0%N$&$'2?0%

'0?0

-\S` )?AJD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 52: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

'0?0

-\S` )?AJD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 53: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

'0?0

-\S` )?AJD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 54: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

'0?0

-\S` '0?0JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 55: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

-\S` '0?0JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 56: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

GE!"#78HISHB$-\ME-SM8A?0B$*A?0=$Z$)=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

-\S` '0?0JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

Page 57: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

GE!"#78HISHB$-\ME-SM8A?0B$*A?0=$Z$)=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

-\S` '0?0JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

A?0

Page 58: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

GE!"#78HISHB$-\ME-SM8A?0B$*A?0=$Z$)=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

-\S` '0?0JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

A?0 *A?0

Page 59: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

GE!"#78HISHB$-\ME-SM8A?0B$*A?0=$Z$)=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

-\S` '0?0JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

A?0 *A?0

-$&$$A?0%N$&$*A?0%

Page 60: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

GE!"#78HISHB$-\ME-SM8A?0B$*A?0=$Z$)=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

-\S` '0?0JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

A?0 *A?0

'A?0

-$&$$A?0%N$&$*A?0%

Page 61: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

GE!"#78HISHB$-\ME-SM8A?0B$*A?0=$Z$)=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

-\S` '0?0JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

'A?0

Page 62: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

GE!"#78HISHB$-\ME-SM8A?0B$*A?0=$Z$)=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

-\S` '0?0JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

'A?0

Page 63: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

GE!"#78HISHB$-\ME-SM8A?0B$*A?0=$Z$)=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

-\S` '0?0JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

'A?0A

Page 64: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

GE!"#78HISHB$-\ME-SM8A?0B$*A?0=$Z$)=%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

JD5NPM$-%$JD5NPM$N%

-\S` '0?0JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

A

Page 65: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Functions w/ Return Types

34

JD5NPM$-\S$&$-\ME-SM81B$f=%

-\S$&$-\ME-SM8'?0B$'2?0=%

GE!"#78HISHB$-\ME-SM8A?0B$*A?0=$Z$)=%

-\ME-SM81$3$'B$f$3$'=%

-\ME-SM%

JD5NPM$-\ME-SM8JD5NPM$-B$JD5NPM$N=

>

$$$$EM#5E"$8-$3$N=$Z$*%@

Legal, but useless 1$3$A%

Legal, but not what you expect

JD5NPM$-%$JD5NPM$N%

-\S` '0?0JD5NPM$1$&$A?0%JD5NPM$f$&$*?0%

A

Page 66: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Using \D!J Functions

35

\D!J$GE!"#de5-EMJ8!"#$"=>$$$$$GE!"#78HIJ$4e5-EMJ$!4$IJHB$"B$"$_$"=%@

GE!"#de5-EMJ8*=%!"#$!$&$A%GE!"#de5-EMJ8!=%

!$&$GE!"#de5-EMJ82=%GE!"#78HIJHB$GE!"#de5-EMJ8)==%

ErrorError

*$4e5-EMJ$!4$+

A$4e5-EMJ$!4$*A

Page 67: Lecture 10-2 - University of Torontojdeber/aps105h/201201/lectures/Lecture10/A… · APS105 Winter 2012 Jonathan Deber jdeber -at- cs -dot- toronto -dot- edu Lecture 10 February 6,

Side Effects

36

• \D!J functions need a side effect

• Functions with return types may have side effects

• This means you will sometime (intentionally) ignore a return value

!"#$"56gT-E4$&$GE!"#78H[$T-\M$-$4!JM$M77MU#?H=%

GE!"#78HIJHB$GE!"#78H[$T-\M$-$4!JM$M77MU#?H==%

[$T-\M$-$4!JM$M77MU#?*'

"56gT-E4` *'

[$T-\M$-$4!JM$M77MU#?