程序設(shè)計語言復(fù)習(xí)-礦大-程序設(shè)計語言原理_第1頁
程序設(shè)計語言復(fù)習(xí)-礦大-程序設(shè)計語言原理_第2頁
程序設(shè)計語言復(fù)習(xí)-礦大-程序設(shè)計語言原理_第3頁
程序設(shè)計語言復(fù)習(xí)-礦大-程序設(shè)計語言原理_第4頁
程序設(shè)計語言復(fù)習(xí)-礦大-程序設(shè)計語言原理_第5頁
已閱讀5頁,還剩70頁未讀 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

1.3LanguageEvaluationCriteriaReadabilityThemostimportantcriteriumFactors:OverallsimplicityToomanyfeaturesisbadMultiplicityoffeaturesisbadOperatoroverloadingOrthogonalityMakesthelanguageeasytolearnandreadMeaningiscontextindependentArelativelysmallsetofprimitiveconstructscanbecombinedinarelativelysmallnumberofwaysEverypossiblecombinationislegalLackoforthogonalityleadstoexceptionstorules正交性:指只用該語言的一組相對少量的根本結(jié)構(gòu),經(jīng)過相對少的結(jié)合步驟,就可構(gòu)成該語言的控制結(jié)構(gòu)和數(shù)據(jù)結(jié)構(gòu)。而且,它的根本結(jié)構(gòu)的任何組合都是合法和有意義的。count=count+1count+=1count++++countCopyright?2006ShujuanJiang.Allrightsreserved.*1.3LanguageEvaluationCriteriaReadabilityfactors(continued)Controlstatements(goto)DefiningdatatypesandstructuresTimeout=1×Timeout=trueSyntaxconsiderationsIdentifierformsSpecialwordsFormandmeaning(C:static;UNIX:grep)Pascal:begin…..endC:{}Ada:if……endifloop……endloopCopyright?2006ShujuanJiang.Allrightsreserved.*1.3LanguageEvaluationCriteriaWritabilityFactors:SimplicityandorthogonalitySupportforabstraction(subprogram,binarytree)Expressivity(C:count++,Ada:andthen(布爾表達式的短路求值))ReliabilityFactors:TypecheckingExceptionhandlingAliasingReadabilityandwritabilityCopyright?2006ShujuanJiang.Allrightsreserved.*1.3LanguageEvaluationCriteriaCostCategoriesTrainingprogrammerstouselanguageWritingprogramsCompilingprogramsExecutingprograms(優(yōu)化)LanguageimplementationsystemReliabilityMaintainingprogramsOthers:portability,generality,well-definedness

Copyright?2006ShujuanJiang.Allrightsreserved.*1.5LanguageCategoriesImperativeCentralfeaturesarevariables,assignmentstatements,anditerationC,PascalFunctionalMainmeansofmakingcomputationsisbyapplyingfunctionstogivenparametersLISP,SchemeCopyright?2006ShujuanJiang.Allrightsreserved.*1.5LanguageCategoriesLogicRule-basedRulesarespecifiedinnospecialorderPrologObject-orientedEncapsulatedataobjectswithprocessingInheritanceanddynamictypebindingGrewoutofimperativelanguagesC++,JavaCopyright?2006ShujuanJiang.Allrightsreserved.*1.7ImplementationMethodsCompilationTranslatehigh-levelprogramtomachinecodeCCOBOLAdaSlowtranslationFastexecutionBottleneck:thespeedoftheconnectionbetweenacomputer’smemoryanditsprocessor.Copyright?2006ShujuanJiang.Allrightsreserved.*Chapter2TopicsTheIBM704andFORTRANTheFirstStepTowardSophistication:ALGOL60TheBeginningsofDataAbstraction:SIMULA67Hisotry’sLargestDesignEffort:AdaObject-OrientedProgramming:SmalltalkCopyright?2006ShujuanJiang.Allrightsreserved.*5.2Names(cont.)SpecialwordsAnaidtoreadability;usedtodelimit(界定)orseparatestatementclausesDef:A

keywordisawordthatisspecialonlyincertaincontexts.i.e.inFortran:RealVarName(Realisdatatypefollowedwithaname,thereforeRealisakeyword)

Real=3.4(Realisavariable) Disadvantage:poorreadabilityDef:Areservedwordisaspecialwordthatcannotbeusedasauser-definednameCopyright?2006ShujuanJiang.Allrightsreserved.*5.2Names(cont.)SpecialwordsPredefinednames(預(yù)定義的名字)Betweenreservedwordanduser-definednamesTheyhavepredefinedmeaningbutcanberedefinedbytheuserForexample,printfscanfinCCopyright?2006ShujuanJiang.Allrightsreserved.*5.3VariablesAvariableisanabstractionofamemorycellVariablescanbecharacterizedasasextuple(六個局部)ofattributes:(name,address,value,type,lifetime,scope)Name-notallvariableshavethem(anonymous(匿名的))Copyright?2006ShujuanJiang.Allrightsreserved.*兩個變量具有按名等價的類型當(dāng)且僅當(dāng)它們一起被說明,或者用相同的類型標識符說明,即對應(yīng)的類型名相同。如:typeTisarray[1..100]ofINTEGER;(Ada)x,y:array[1..100]ofINTEGER;〔P198〕z:array[1..100]ofINTEGER;r,w:T;i,j:FLOAT;按名等價:r與w,i與jx與y、x與z按名不等價,z與r按名不等價.按名等價(Nametypecompatibility)Copyright?2006ShujuanJiang.Allrightsreserved.*兩個變量具有按定義等價的類型當(dāng)且僅當(dāng)兩個變量有完全相同的類型〔包括相同的域名〕。除類型名外的其他特征均相同者即視為按定義等價。如typecomplex=recordre:int;im:intend;Typerational=recordnominator:int;denominator:1..299999end;按定義等價(declarationequivalence)Copyright?2006ShujuanJiang.Allrightsreserved.*varx:complex;z:recordre:int;im:intend;u:rational;v:recordrepart:int;denominator:1..299999end;

x與z按名不等價,但按定義等價。

u與v按定義不等價按定義等價Copyright?2006ShujuanJiang.Allrightsreserved.*

兩個變量具有按結(jié)構(gòu)等價的類型當(dāng)且僅當(dāng)變量的所有分量有相同類型的值集。在上例中,u和v第二個分量的值集是相同的,所以按結(jié)構(gòu)等價。x和u呢?按結(jié)構(gòu)等價雖然第二個分量的值均為整數(shù),但其類型不同,所以不是按結(jié)構(gòu)等價的。Copyright?2006ShujuanJiang.Allrightsreserved.*programexample;vara,b:integer;……

proceduresubl;varx,y:integer;begin{subl}…..1end;{subl}

proceduresub2;varx:integer;……

proceduresub3;varx:integer;begin{sub3}……2end;{sub3}

begin{sub2}……..3

end;{sub2}begin{example}……4end.{example}Point

Referencing

Environmentxandyofsubl,aandb

ofexamplexofsub3,(xofsub2ishidden),aandbofexamplexofsub2,aandbofexample4aandbofexampleInastatic-scopedlanguageCopyright?2006ShujuanJiang.Allrightsreserved.*voidsubl(){inta,b;…….1}/*endofsubl*/voidsub2(){intb,c;……2

subl();}/*endofsub2*/voidmain(){intc,d;……3

sub2();}/*endofmain*/

Point

ReferencingEnvironment1aandbofsub1,cofsub2,dofmain,,(cofmainandbofsub2arehidden)2bandcofsub2,dofmain,(cofmainishidden)3canddofmainFunctioncall:Main--->sub2---->sub1Inadynamic-scopedlanguageCopyright?2006ShujuanJiang.Allrightsreserved.*6.10PointerTypesPointerProblemsDanglingPointers(懸掛指針):isapointerthatcontainstheaddressofaheap-dynamicvariablethathasbeendeallocated(解除分配).

Danglingpointersaredangerous

thelocationmayhavebeenreallocatedthevalueofthenewheap-dynamicvariablewillbedestroyedstoragemanagertofail

LostHeap-dynamicVariables:isanallocatedheap-dynamicvariablethatisnolongeraccessibletotheuserprogram.Suchvariablesarecalledgarbage(垃圾)Copyright?2006ShujuanJiang.Allrightsreserved.*OperandEvaluationOrder

Ifneitheroftheoperandsofanoperatorhassideeffects,thenoperandevaluationorderisirrelevant(不相關(guān)的)

Sideeffects

Def:

functional

side

effect,occurswhenthefunctionchangeseitheroneofitsparametersoraglobalvariable

Example

a=10;10+5=15(fromlefttoright)b=a+fun(a);20+5=25(fromrighttoleft)funreturnsthevalueofitsargumentdividedby2andchangesitsparametertohavethevalue207.2ArithmeticExpressions

Copyright?2006ShujuanJiang.Allrightsreserved.*7.2ArithmeticExpressions

Example

inta=5;

intfunI(){a=17;return3;}/*offun1*/

voidfun2(){a=a+funl();}/*offun2*/voidmain(){fun2();}/*ofmain*/Fromlefttoright:A=5,fun1=3,result=8Fromrighttoleft:Fun1=3,a=17,result=20Copyright?2006ShujuanJiang.Allrightsreserved.*在表達式中出現(xiàn)的函數(shù)調(diào)用可能出現(xiàn)“副作用〞,即可能存在同一個賦值語句中的同名量的值不相同,所以在串行流程中必須規(guī)定計算次序。計算次序如下:求位置求位置求位置(計算下標)(計算下標)(計算下標)左端1:=左端2:=……:=右端;賦值賦值賦值7.2ArithmeticExpressions

Copyright?2006ShujuanJiang.Allrightsreserved.*Examplei,j:integer;A,B:array[1..100]ofinteger;Functionf(x:integer):integer;begini:=i+1;j:=j+2;f:=x+1end7.2ArithmeticExpressions

Copyright?2006ShujuanJiang.Allrightsreserved.*例:i:=3;j:=0;A[i]:=B[f(j)+i]:=i+f(j)+i*j(1)求A[i]的位置,i=3;(2)求B[f(j)+i]的位置,j=0,f(0)=0+1=1,同時有,i=3+1=4,j=0+2=2(副作用),

即f(j)+i=5,B[f(j)+i]=B[5],(3)表達式的值為:4+f(2)+(4+1)*(2+2)=27,在求f(2)時i=4+1=5,j=2+2=4(副作用)

(4)將27賦給B[5](5)將27賦給A[3]A[i]:=B[i+f(j)]:=i+f(j)+i*j結(jié)果=?7.2ArithmeticExpressions

Copyright?2006ShujuanJiang.Allrightsreserved.*i:=3;j:=0;A[i]:=B[i+f(j)]:=i+f(j)+i*j(1)求A[i]的位置,i=3;(2)求B[i+f(j)]的位置,j=0,f(0)=0+1=1,即i+f(j)=3+1=4,B[i+f(j)]=B[4],同時有,i=3+1=4,j=0+2=2(副作用),

(3)表達式的值為:4+f(2)+(4+1)*(2+2)=27,在求f(2)時i=4+1=5,j=2+2=4(副作用)

(4)將27賦給B[4](5)將27賦給A[3]7.2ArithmeticExpressions

Copyright?2006ShujuanJiang.Allrightsreserved.*9.1IntroductionTwofundamentalabstractionfacilities(設(shè)施)canbeincludedinaprogramminglanguage:processabstractionanddataabstractionProcessabstractionhasbeenacentralconceptinallprogramminglanguages.Thefirstprogrammablecomputer,in1940,hadthecapability(能力)ofreusingcollectionsofinstructioncardsatseveraldifferentplacesinaprogramwhenthatwasconvenient(方便的)Thisreuseresultsinseveraldifferentkindsofsavings,frommemoryspacetocodingtime.Thisincreasesthereadabilityofaprogram.Copyright?2006ShujuanJiang.Allrightsreserved.*BasicDefinitions(con.)ForexampleFORTRAN:SUBROUTINEADDER(parameters)

Ada:procedureADDER(parameters)C:voidadder(parameters)Theparameter

profile(參數(shù)輪廓)

ofasubprogramisthenumber,order,andtypesofitsformalparameters(形式參數(shù)).Theprotocol(協(xié)議)

ofasubprogramisitsparameterprofileplus,ifitisafunction,itsreturntype.9.2FundamentalsofSubprogramsCopyright?2006ShujuanJiang.Allrightsreserved.*Parameters

Therearetwowaysthatasubprogramgains(得到)accesstothedata:Throughdirectaccesstononlocal(非局部的)variables

ThroughparameterpassingParameterpassingismoreflexible(靈活的)thandirectaccesstononlocalvariables.Extensive(大量)accesstononlocalscancausereducedreliability(可靠性)Theparametersinthesubprogramheaderarecalledformal

parameters.Theyareboundtostorageonlywhenthesubprogramiscalled.9.2FundamentalsofSubprogramsCopyright?2006ShujuanJiang.Allrightsreserved.*Parameters(Con.)

Subprogramcallstatementsmustincludethenameofthesubprogramandalistofparameterstobeboundtotheformalparametersofthesubprogram(actual

parameters.).positional

parameters:thecorrespondence(對應(yīng))betweenactualandformalparametersisdonebysimpleposition

keyword

parameters:thenameoftheformalparametertowhichanactualparameteristobeboundisspecifiedwiththeactualparameter.(Ada)9.2FundamentalsofSubprogramsCopyright?2006ShujuanJiang.Allrightsreserved.*Variablesthataredefinedinsidesubprogramsarecalledlocalvariables.(eitherstaticorstackdynamic)Def:Thenonlocal

variablesofasubprogramarethosethatarevisiblewithinthesubprogrambutarenotlocallydeclared.Def:

Global

variablesarethosethatarevisibleinallprogramunits.9.4LocalReferencingEnvironmentsCopyright?2006ShujuanJiang.Allrightsreserved.*9.5Parameter-PassingMethodsCopyright?2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassing

Pass-by-ValueThevalueoftheactualparameterisusedtoinitializethecorrespondingformalparameterActsasalocalvariableinthesubprogramPass-by-valueisnormallyimplementedbyactualdatatransfer.Thedisadvantage:AdditionalstoragefortheformalparameterThestorageandthemoveoperationscanbecostlyiftheparameterislargePass-by-valueisimplementedbytransmittinganaccesspathtothevalueoftheactualparameterrequirethatthevaluebeinawrite-protectedcell

9.5Parameter-PassingMethodsCopyright?2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassing

Pass-by-ResultPass-by-resultisanimplementationmodelforout-modeparameters.NovalueistransmittedtothesubprogramThecorrespondingformalparameteractsasalocalvariable,justbeforecontrolistransferredbacktothecaller,itsvalueispassedbacktothecaller'sactualparameter

AlsorequirestheextrastorageandthecopyoperationsBeingimplementedbydatatransfer,theproblemisinensuringthattheinitialvalueoftheactualparameterisnotusedinthecalledsubprogram.9.5Parameter-PassingMethodsCopyright?2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassingPass-by-Result(con.)Oneproblemwiththepass-by-resultmodelisthattherecanbeanactualparametercollisionsub(p1,p1)〔調(diào)用語句中〕AssumingthetwoformalparametershavedifferentnamesTheorderinwhichtheactualparametersareassigneddeterminestheirvalueBecausetheorderisusuallyimplementationdependent,portability(可移植性)problemscanoccurthataredifficulttodiagnose(診斷)Anotherproblemistochoosetwodifferenttimestoevaluatetheaddressesoftheactualparameters(atthetimeofthecalloratthetimeofthereturn)9.5Parameter-PassingMethodsForexample:

list[index]Ifindexischangedbythesubprogram,thentheaddressoflist[index]willchangebetweenthecallandthereturnCopyright?2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassing

Pass-by-Value-ResultPass-by-value-resultisanimplementationmodelforinout-modeparametersinwhichactualvaluesaremoved.Acombinationofpass-by-valueandpass-by-result.Pass-by-value-resultissometimescalledpass-by-copybecausetheactualparameteriscopiedtotheformalparameteratsubprogramentryandthencopiedbackatsubprogramtermination.Disadvantagesofrequiringmultiplestorageforparametersandtimeforcopyingvalues.Theproblemsassociatedwiththeorderinwhichactualparametersareassigned.9.5Parameter-PassingMethodsCopyright?2006ShujuanJiang.Allrightsreserved.*COPY機制:機制實參進入時出去時功能功能傳值方式表達式X:=實參

傳結(jié)果方式變量實參:=x傳值--結(jié)果方式變量X:=實參實參:=x傳值方式、傳結(jié)果方式、傳值--結(jié)果方式通稱為傳遞形式參數(shù)的COPY機制。形參x被指定為被調(diào)用過程的一個局部變量;進入時將實參值復(fù)制給x,出去時將x的值復(fù)制給實參。X如同一個局部變量一樣,它的值可以取出和更新,進入時創(chuàng)立出去時刪除。9.5Parameter-PassingMethodsCopyright?2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassing

Pass-by-ReferenceThepass-by-referencemethodtransmitsanaccesspath,justanaddresstothecalledsubprogram.Advantagesisthatthepassingprocessitselfisefficient,intermsofbothtimeandspace.Duplicatespaceisnotrequired,norisanycopying.Disadvantageslikelybeslowerbecauseindirectaddressingifonlyonewaycommunicationtothecalledsubprogramisrequired,Inadvertent(不注意的)anderroneous(錯誤的)changesmaybemadeAnotherseriousproblemofpass-by-referenceisthataliasescanbecreated.9.5Parameter-PassingMethodsCopyright?2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassing

Pass-by-ReferenceseveralwaysaliasescanbecreatedCollisions(沖突)canoccurbetweenactualparametersvoidfun(int*first,int*second)fun(&total,&total)Collisionsbetweenarrayelementscanalsocausealiasesfun(&list[i],&list[j])ifI==jCollisionsbetweenarray-elementparametersandelementsofarrayspassedasarray-nameparametersfun1(&list[i],&list)9.5Parameter-PassingMethodsCopyright?2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassing

Pass-by-Referenceseveralwaysaliasescanbecreated(con.)Collisionsbetweenformalparametersandnonlocalvariables9.5Parameter-PassingMethodsint*global;voidmain(){extern

int*global;……

sub(global);

}voidsub(int*local){externint*global;……}Insidesub,localandglobalarealiases.Copyright?2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassing

Pass-by-NamePass-by-nameisaninout-modeparametertransmissionmethod.Whenparametersarepassedbyname,theactualparameteristextuallysubstitutedforthecorrespondingformalparameterinallitsoccurrencesinthesubprogram.Apass-by-nameformalparameterisboundtoanaccessmethodatthetimeofthesubprogramcall,buttheactualbindingtoavalueoranaddressisdelayeduntiltheformalparameterisassignedorreferenced.9.5Parameter-PassingMethodsCopyright?2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassingPass-by-Name〔con.〕Iftheactualparameterisascalarvariable(標量變量),thenpass-by-nameisequivalenttopass-by-reference.Iftheactualparameterisaconstantexpression,thenpass-by-nameisequivalenttopass-by-valueIftheactualparameterisanarrayelement,pass-by-namemaybedifferentfromanyothermethodIftheactualparameterisanexpressionthatcontainsavariable,pass-by-nameisagaindifferentfromanyothermethod9.5Parameter-PassingMethodsCopyright?2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassingPass-by-Name〔con.〕9.5Parameter-PassingMethods

beginLIST[1]:=2;LIST[2]:=2;GLOBAL:=1;SUB(LIST[GLOBAL])

end;procedureBIGSUB;integerGLOBAL;integer

arrayLIST[1:2];procedureSUB(PARAM);integerPARAM;beginPARAM:=3;GLOBAL:=GLOBAL+1;PARAM:=5end;List[1]=3;List[2]=5;Copyright?2006ShujuanJiang.Allrightsreserved.*傳名方式〔callbyname〕用實參替換形參后的過程體來代替原過程體。必要時,變量必須重命名〔如果同名時〕。例:procedureroots(a:float)isd:float;begind:=discr(a,a,a)//假設(shè)discr已定義過endroots那么用傳名機制來調(diào)用roots(x),有一個分程序:d:float;begind:=discr(x,x,x)//假設(shè)discr已定義過endroots9.5Parameter-PassingMethodsCopyright?2006ShujuanJiang.Allrightsreserved.*ImplementationModelsofParameterPassingPass-by-Name〔con.〕Theprimaryadvantageofpass-by-nameistheflexibilityThemaindisadvantageistheslownessoftheprocess9.5Parameter-PassingMethodsCopyright?2006ShujuanJiang.Allrightsreserved.*再談參數(shù)傳遞(按引用調(diào)用)例如語句global:=1;PROC(global);

執(zhí)行時把指向global的指針傳遞給形式參數(shù)pp最后global=0ProcedurePROC(pp);begin

pp:=0; pp:=(pp+global)*1000;endPROC;

Copyright?2006ShujuanJiang.Allrightsreserved.*再談參數(shù)傳遞〔按值調(diào)用〕例如ProcedurePROC(pp);begin pp:=0; pp:=(pp+global)*1000;endPROC;

語句global:=1;PROC(global);

執(zhí)行時把global的值1的傳遞給形式參數(shù)pp最后global=1Copyright?2006ShujuanJiang.Allrightsreserved.*再談參數(shù)傳遞〔按值-結(jié)果調(diào)用〕例如ProcedurePROC(pp);begin pp:=0; pp:=(pp+global)*1000;endPROC;

語句global:=1;PROC(global);

執(zhí)行時:

global

ppppglobal最后global=1000Copyright?2006ShujuanJiang.Allrightsreserved.*再談參數(shù)傳遞〔例1〕簡單變量與常量(在C語言中,在子程序頭部前面帶有*的形式參數(shù)是按引用傳遞,而沒有*的是按值傳遞。以下圖中I是按值傳遞,j是按引用傳遞)兩個打印語句的結(jié)果是什么?結(jié)果是:1213213Copyright?2006ShujuanJiang.Allrightsreserved.*當(dāng)P調(diào)用Q時,實際參數(shù)表達式a和&b被計值,實際傳遞的參數(shù)是a的右值和b的左值。由于a是按值傳遞的,形式參數(shù)i被表示為Q中的局部整數(shù)變量,并且當(dāng)子程序Q開始執(zhí)行時,a的值在調(diào)用時被作為初值賦予i。之后,a和i不再有聯(lián)系。當(dāng)i被賦為新值12時,a不會改變。當(dāng)調(diào)用Q結(jié)束后,a的值仍為2再談參數(shù)傳遞〔例1〕參數(shù)b是按引用傳遞。這意味著j是Q中指向整數(shù)的指針類型的局部變量。當(dāng)Q開始執(zhí)行時,指向數(shù)據(jù)對象b的指針作為j的右值來存放。當(dāng)10加到j(luò)的值中時,j本身并不改變。每次對j的引用〔j的右值即b的左值〕緊跟著一個指針值的選取操作,使得對j的引用實際上是訪問數(shù)據(jù)對象b的地址。Copyright?2006ShujuanJiang.Allrightsreserved.*雖然看起來對j的賦值類似于對i的賦值,但是它意味著完全不同。實際參數(shù)b的值改變?yōu)?3,當(dāng)形式參數(shù)i和j的值在Q中打印時,結(jié)果是12和13,當(dāng)返回到P時,對應(yīng)于實際參數(shù)a和b的值被打印,僅有b的值被修改。當(dāng)Q終止時,在Q中賦予i的值12被喪失了,因為Q中的局部變量在結(jié)束時被刪除了。當(dāng)然j的值也被喪失了,但是這是指針而非值13.再談參數(shù)傳遞〔例1〕Copyright?2006ShujuanJiang.Allrightsreserved.*Pascal:

typevect=array[1..3]ofinteger;procedureQ(k:vect;varl:vect);varn:integer;begink[2]:=k[2]+10;l[2]:=l[2]+10;forn:=1to3dowrite(k[n]);forn:=1to3dowrite(l[n]);end;再談參數(shù)傳遞〔例2〕

procedureP;varc,d:vect;m:integer;beginc[1]:=6;c[2]:=7;c[3]:=8;d[1]:=6;d[2]:=7;d[3]:=8;Q(c,d);form:=1to3dowrite(c[m]);form:=1to3dowrite(d[m]);end;當(dāng)P執(zhí)行時,打印的結(jié)果是什么?617861786786178Copyright?2006ShujuanJiang.Allrightsreserved.*Def:An

overloaded

subprogramisasubprogramthathasthesamenameasanothersubprograminthesamereferencingenvironment.Everyversionofanoverloadedsubprogrammusthaveaunique(唯一的)protocol;Itmustbedifferentfromtheothersinthenumber,order,ortypesofitsparameters,orinitsreturntypeifitisafunction.C++,Java,andAdaincludepredefinedoverloadedsubprograms.InAda,thereturntypeofanoverloadedfunctionisusedtodisambiguate(消除…的歧義)calls.

9.7OverloadSubprogramsCopyright?2006ShujuanJiang.Allrightsreserved.*Thecapabilityofcompilingpartsofaprogramwithoutcompilingthewholeprogramisessentialtotheconstructionoflargesoftwaresystems.Thepartsofprogramthatcanbecompiledaresometimescalledcompilationunits(編譯單元).Thetermseparatecompilation(分別編譯)

meansthatcompilationunitscanbecompiledatdifferenttimes,buttheircompilationsarenotindependentofeachotherifeitheraccessesorusesanyentitiesoftheother.9.9SeparateandIndependentCompilationCopyright?2006ShujuanJiang.Allrightsreserved.*Usingwith,theprogrammerspecifiestheexternalunitsthatthecodeinthecompilationmustaccess

withGLOBALS,TEXT_IO;procedureEXAMPLEis……endEXAMPLE;FORTRAN90alsoallowsseparatecompilationofitssubprogramsandmodules.Withindependent

compilation,programunitscanbecompiledwithoutinformationaboutanyotherprogramunits.9.9SeparateandIndependentCompilationCopyright?2006ShujuanJiang.Allrightsreserved.*10.1TheGeneralSemanticsofCallsandReturns

Subprogramlinkage:ThesubprogramcallandreturnoperationsofalanguageAsubprogramcallmustincludethemechanism

-Iflocalvariablesarenotstatic,itmustcausestoragetobeallocatedforthelocalsdeclaredinthecalledsubprogramandbindthosevariablestothatstorage-Itmustsavetheexecutionstatusofthecallingprogramunit

Copyright?2006ShujuanJiang.Allrightsreserved.*10.1TheGeneralSemanticsofCalls

andReturns

Asubprogramcallmustincludethemechanism(continue)-Itmusttransfercontroltothecodeofthesubprogramandreturntotheproperplacewhenthesubprogramexecutioniscompleted.-Thecallmustcausesomemechanismtobecreatedtoprovideaccesstononlocalvariablesthatarevisibletothecalledsubprogram.Copyright?2006ShujuanJiang.Allrightsreserved.*10.1TheGeneralSemanticsofCalls

andReturns

AsubprogramReturnmustincludethemechanismIfthesubprogramhasparametersthatareoutmodeandareimplementedbycopyFirst,movelocalvaluesoftheassociatedformalparameterstotheactualparametersNext,ItmustdeallocatethestorageusedforlocalvariablesandrestoretheexecutionstatusofthecallingprogramunitThen,returnthemechanismusedfornonlocalreferencestotheconfigurationithadbeforethecallFinally,

controlmustbereturnedtothecallingprogramunitCopyright?2006ShujuanJiang.Allrightsreserved.*10.1TheGeneralSemanticsofCallsandReturns

Subprogramlinkage:ThesubprogramcallandreturnoperationsofalanguageAsubprogramcallmustincludethemechanism

-Iflocalvariablesarenotstatic,itmustcausestoragetobeallocatedforthelocalsdeclaredinthecalledsubprogramandbindthosevariablestothatstorage-Itmustsavetheexecutionstatusofthecallingprogramunit

Copyright?2006ShujuanJiang.Allrightsreserved.*10.1TheGeneralSemanticsofCalls

andReturns

Asubprogramcallmustincludethemechanism(continue)-Itmusttransfercontroltothecodeofthesubprogramandreturntotheproperplacewhenthesubprogramexecutioniscompleted.-Thecallmustcausesomemechanismtobecreatedtoprovideaccesstononlocalvariablesthatarevisibletothecalledsubprogram.Copyright?2006ShujuanJiang.Allrightsreserved.*10.1TheGeneralSemanticsofCalls

andReturns

AsubprogramReturnmustincludethemechanismIfthesubprogramhasparametersthatareoutmodeandareimplementedbycopyFirst,movelocalvaluesoftheassociatedformalparameterstotheactualparametersNext,ItmustdeallocatethestorageusedforlocalvariablesandrestoretheexecutionstatusofthecallingprogramunitThen,returnthemechanismusedfornonlocalreferencestotheconfigurationithadbeforethecallFinally,

controlmustbereturnedtothecallingprogramunitCopyright?2006ShujuanJiang.Allrightsreserved.*10.3ImplementingSubprogramsin

ALGOL-likeLanguages

InthecaseoftheALGOL-likelanguages,activationrecordinstancesmustbecreateddynamicallyCopyright?2006ShujuanJiang.Allrightsreserved.*Thereturnaddressoftenconsistsofapointertothecodesegmentofthecallerandanoffsetaddressinthatcodesegmentoftheinstructionfollowingthecall(調(diào)用語句之后那條指令的偏移地址)Thestatic

link(sometimescalledastatic

scopepointer)pointstothebottomoftheactivationrecordinstanceofanactivationofthe

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論