� Topic IV � Block-structured procedural languages Algol and Pascal References: � Chapters 5 and 7, of Concepts in programming languages by J. C. Mitchell. CUP, 2003. � Chapters 10(§2) and 11(§1)of Programming languag es: Design and implementation(3 RD EDITION) by T.W. P !tt !"# M. $.%el&o'it(. P entice )all, *+++. � 74 Chapter 5of Programming languag es: Concepts & constructs by R.,ethi (2ND EDITION). -#di.on-Wesley, *++6. Chapter 7of Understanding program ming languages by M 0en-- i. Wiley, *++/. � 75 �� �� ��Parameters�� The e ! e t'o conce1t. that 23.t be cle! ly #isti"4ui.he#5 � - formal parameter i. ! #ecl! ation that !11ea . i" the #ecla !tio" of the .3bp o4 am. (The comput!tion in the body of the .ub1ro4 !2 i. ' itten in ter2. of fo mal 1! !mete ..) � -" actual parameter i. ! 6al3e that the c!lli"4 1 o4 !2 .en#. to the .3bp o4 !m. Example: Named 1! !mete !..ociatio".. No 2!lly the !ct3!l 1! !2eter. in ! .3b1 o4 !2 c!ll ! e 73.t listed !"# the matchi"4 'ith the formal 1! !meter. is do"e by � 76 1ositio"5 procedure Proc(First: Integer; Second: Character); Proc(24,’h’); I" -#! it i. 1os.ible to 3.e named association i" the c!ll: Proc(Second => ’h’, First => 24); ? What about in ML9 Can it be .i2ul!ted9 Thi. i. commo"ly 3.ed togethe 'ith default parameters5 procedure Proc(First: Integer := 0; Second: Character := ’*’); Proc(Second => ’o’); � 77 �� �� ��Parameter passing�� The '!y that !ct3al 1! !2eter. ! e e6al3ate# !"# 1!..e# to 1 oced3re. #epe"#. on the 1 o4 !2min4 la"43!ge !"# the &in# of parameter-passing mechanisms it 3.e.. The 2!i" #istinction bet'ee" #iffe ent 1! ameter-1!..in4 2echani.2. ! e: � the time that the act3!l 1! !mete is e6!l3!ted, !"# � the location 3.e# to .to e the 1! amete 6!lue. NB: The location of ! 6! i!ble (or e:1 e..ion) i. called it. L-value, !"# the value .to e# i" this loc!tio" i. c!lled the R-value of the 6! i!ble (o e:1 e..ion). � 78 �� �� ��Parameter passing�� Pass/Call-by-value � I" pass-by-value, the act3!l 1! amete i. e6!l3!ted. The 6!lue of the act3!l 1! !2ete i. then .to ed in ! "e' locatio" !lloc!te# fo the f3nctio" 1! amete . � Under call-by-value, ! fo 2!l 1! amete co e.1on#. to the 6!lue of !" act3!l 1! amete . That is, the fo 2!l of! 1 oced3reP t!&es o" the 6alue of t he !ct3al 1! !2eter. The ide! i. to e6!lu!te ! c!ll P(E) !. follo'.5 := !; e:ec3te the body of 1 oced3 eP; ifP i. ! f3nctio", et3 " ! e.3lt. � 79 �� �� ��Parameter passing�� Pass/Call-by-reference � I" pass-by-reference, the act3!l 1! amete 23.t ha6e !" 8-6al3e. The 8-6!lue of the !ct3al 1! !2eter i. the" bou"# to the for2!l 1! !2eter. � Under call-by-reference, ! fo 2!l 1! amete becomes ! .y"ony2 fo the loc!tio" of !" !ct3al 1! !2ete . -" !ct3!l efe ence 1! amete 23.t ha6e ! location. � 80 Example: program "ain; #egin $unction f( %ar x: integer; &: integer): integer; #egin := 2; & := 1; i$ = ' then $ := ' else $:= 2 end; %ar z: integer; ) := 0; *riteln( f(z,z) ) end � 81 The #iffere"ce bet'een call-by-6!lue !"# call-by- eference is i21ort!nt to the 1 o4 !2me i" .e6e al '!y.5 � Side effects -..i4"2e"t. in.i#e the f3"ctio" body 2!y h!6e different effect. 3"de 1!..-by-6!lue !"# 1!..-by- efe ence. � !liasin" -lia.i"4 occ3 . whe" t'o "!2e. efe to the .ame object or locatio". -lia.i"4 2!y occ3 whe" t'o 1! amete . ! e 1!..ed by efe ence o one 1! !2ete 1!..e# by efe ence h!. the .ame location !. the 4lobal 6! i!ble of the 1 oced3 e. � 82 Ef#cienc$ P!..-by-6al3e 2!y be inef<cie"t fo l! ge .t uct3 e. if the 6alue of the la ge .t uct3 e 23.t be copie#. P!..-by- efe e"ce 2!ybe les. ef<cie"t th!" 1!..-by-6!lue fo .mall .t uct3 e. that 'oul# <t di ectly on .t!c&, bec!3.e 'hen 1! amete . ! e 1!..ed by efere"ce 'e 23.t #e efe ence ! 1ointe to 4et thei 6al3e. � 83 �� �� ��Parameter passing�� Pass/Call-by-value/result Call-by-value/result i. al.o &no'" !. copy-in/copy-out bec!3.e the act3!l. ! e i"itially copied i"to the fo 2!l. !"# the formal. ! e e6e"t3!lly copied bac& out to the !ct3!ls. -ct3!ls that #o "ot h!6e loc!tio". ! e 1!..e# by 6al3e. -ct3!ls with loc!tio". ! e t eate# !. follo'.5 1. Copy-in phase. Both the 6alue. !"# the loc!tio". of the !ct3al 1! !2eter. ! e co21uted. The 6!lues !re !..ig"ed to the co es1o"#in4 formal., !. i" c!ll-by-6!lue, !"# the loc!tio". ! e .!6ed fo the co1y-out 1h!.e. 2. Copy-out phase. -fte the 1 oce#3 e body is e:ecuted, the <"!l 6!lue. of the fo mal. ! e copied bac& out to the locatio". compute# i" the copy-i" 1h!.e. � 84 Examples: � - 1! !2eter i" P!.cal i. "ormally 1!..e# by 6!lue. It i. 1!..e# by efe ence, ho'e6e , if the &ey'or# %ar !11ea . befo e the decla !tio" of the fo mal 1! !2eter. procedure proc(in: Integer; %ar out: +ea(); � The only 1! !mete -1!..i"4 2etho# i" C i. c!ll-by-6!lue; ho'e6e , the effect of c!ll-by- efe ence c!" be !chie6e# 3.i"4 pointe .. I" C+= t 3e call-by- efere"ce is !6ailable using reference parameters. � 85 -#! .31po t. three &in#. of 1! !mete .5 1. in 1! !mete ., cor e.1o"#in4 to 6!lue 1! amete .; 2. out 1! !mete ., co e.po"#in4 to 73.t the co1y-o3t ph!.e of call-by-6alue> e.3lt; !"# 3. in out 1! !mete ., co e.po"#in4 to eithe efe ence 1! !mete . o 6alue> e.ult 1! !2eter., at the #isc etio" of the imple2ent!tio". � 86 �� �� ��Parameter passing�� Pass/Call-by-name The -lgol /0 epo t #esc ibe. call-by-name !. follo'.5 1. -ct3!l 1! !2ete . !re te:t3!lly .3bstit3ted fo the formal.. Pos.ible con?ict. bet'ee" "ames in the act3!l. !"# local "ames in the 1 oced3 e body ! e !6oide# by e"amin4 the local. i" the body. 2. The es3lti"4 1 oce#3 e body is .3bstit3ted fo the c!ll. Pos.ible con?ict. bet'ee" "onlocal. i" the 1 oced3 e body !"# local. !t the 1oint of call ! e !6oide# by e"!2i"4 the local. !t the 1oint of call. � 87 Block structure I" ! block-structured language, each 1 og !2 o .ub1 o4 !2 i. o 4!ni.e# !. ! .et of ne.te# bloc&.. - block i. ! e4io" of 1 o4 !2 te:t, identi<e# by begi" !"# e"# 2! &e ., th!t 2!y contai" #ecl! ation. loc!l to this egion. n-line !or unnamed) blocks ! e 3.eful fo est icti"4 the .cope of 6! i!ble. by #ecl! in4 the2 only 'he" "eeded, i".tea# of at the begi""i"4 of ! .3bp og am. The t e"# i" 1 og !2mi"4 is to ed3ce the .i(e of .3bp o4 !2., .o the 3.e of 3""amed bloc&. i. les. 3.eful th!" it 3.e# to be. � 88 #ested procedures c!" be 3.ed to 4 ou1 .tate2e"t. th!t ! e e:ec3ted at 2o e th!" one loc!tion 'ithi" ! .ub1 o4 !2, b3t efer to loc!l 6! iables !"# .o c!"not be e:te "!l to the .3bp og !2. 0efo e mo#3le. !"# object-oriented 1 o4 !22i"4 'ere int od3ced, "ested 1 oced3re. 'e e 3.e# to .t 3ct3 e l! 4e 1 og !2.. 0loc& .t uct3 e '!. < .t #e<"ed in -lgol. P!.cal contai". "ested 1 oced3 e. but "ot in-line bloc&.; C contai". in-line bloc&. but "ot ne.te# 1 oce#3 es; -#! .31po t. both. $lock-structured languages ! e cha !cte i.e# by the follo'i"4 1 ope tie.5 � Ne' 6! iables 2!y be #ecla e# !t 6! io3. poi"t. in ! 1 og !2. � 89 E!ch #ecl! ation is 6i.ible within ! certai" e4ion of 1 og !2 te:t, c!lle# ! bloc&. Whe" ! 1 o4 !2 begi". e:ecuti"4 the in.t uction. containe# i" ! bloc& at 3" time, memory is !lloc!ted for the 6! iable. #ecl! e# i" that bloc&. Whe" ! 1 o4 !2 e:it. ! bloc&, .ome o all of the 2e2o y !lloc!ted to 6! iable. #ecl! e# i" th!t bloc& 'ill be #ealloc!ted. -" i#entifie that is not #elc! e# i" the c3 ent bloc& i. con.i#ere# 4lobal to the bloc& !"# efe . to the e"tity 'ith thi. "!2e th!t i. #ecla e# i" the clo.e.t enclo.i"4 bloc&. � 90 Algol had a major effect on language design The -lgol-li&e 1 og !22i"4 l!"43!4es e6ol6e# i" 1! !llel 'ith the 8I,P family of l!"43!ge., begi"ni"4 'ith -lgol @A !"# -lgol /0 i" the late *+@0s.
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages37 Page
-
File Size-