Class07.docx
《Class07.docx》由会员分享,可在线阅读,更多相关《Class07.docx(9页珍藏版)》请在冰豆网上搜索。
Class07
Lecture8前15分钟回首页
ThisisComputerProgrammingIfromtheUniversityofWashington,I’mMartinDickey.Andtodaywe’llcontinueourdiscussionoffunctionsinlectureA.AlthoughI’vetitledthelecture“functionparameters”.Parametersareonlyoneofmanyimportanttopicswe’lltalkabouttoday.Inadditiontoparameter,we’llbetalkingaboutreturnvalues,returntypes,returnstatement,localvariables,functionprototypesandheaderfiles.Thereistimesofnewterminologiesinthislectureaswell.Tilltheendofthelecture,I’lldoastandardexamplewithanaturalprogramtracedindetail.Andfollowingthat,IthinktherewillbetimeforacoupleofbonustopicsthatIthinkyoumayfindusefulwhenyousitdowntowriteyourprograms.Let’sgobacktotheexampleweuseextensivelylasttime.Afunctionwhichprintsabinalof2linesit’squiteasimplefunction.Oneoftheadvantagesoffunctions,oneofourprimarymotivations,istomakeprogrameasiertochangeincasethedesignerortheclientfindthattheyneedtodothat.Let’sconsideroneofthesechangesandseehowitmightaffectusandhowwemighthandleit.Supposetheclientnowdecides2rowsarenotenough,thereshouldbe5rowsofasteriskswhentheprogramstartsandstops.Butalltheothertimes,thereshouldbeonlytwo.Howcouldwesolvethisproblem?
Well,usingwhatweknownow,youcouldsitdownandwriteasecondfunction,maybeyoucallitprintBinalr2thatfunctioncouldprint5rowsofasterisks.Andthenbackinthemainprogramyouwouldcallitatthebeginningoftheprogram.Callitattheendandalltheotherspotyouwillstillhavecallstotheoriginalfunctions.Youcoulddothat,butcomputersciencetendencyarenewtoils(2:
12)fortryingtogeneralizetofindtheabstractionandcoversmorethanonecase.Couldwegeneralizehere?
Couldwesomehowtakeasinglefunction,printBinalLinesandmakeitdobothoftheoperationthatweneed?
Makeitprint5linessometimesandmakeit2linesothertimes.Inthatsense,that’swhatparametersareallabout.Andatagreatdealoflecturewe’llbedevotedtoshowinghowwecandothat.Someoftheconsequencesandsomeoftheprogrammingdetailsinvolved.Let’sstatetheproblemandslideitinadifferentway.Ouroriginalspecificationwastoprint2rowsofasterisks.We’dliketomodifythatsothatitcouldeitherprint2rowsor5rows.Amoregeneralwayofstatingthiswouldbe:
let’sprintnrows.Well,nissomequantitythatwedon’tknowaheadoftime,itmightbe2,itmightbe5,maybeitcouldbesomethingelse,OK.Atthemomentthefunctioniscalled,thatvaluewouldbeknownsothefunctionwouldknowwhattodo.Sonisn’tnumber,it’sinformationofthefunctionneedstoknowandthat’sacrucial(至关重要的)concept.Weneedawayofgettinginformationintothefunction.Let’ssupposethereisawaytodothat,Iwanttoshowyouthecodeforitrightnow,butlet’ssupposewecoulddothat.Howwouldthatchangeourmainprogram?
HereishowitlookswhenIcalledthenewversionofprintBianlLines.WhenIwanttoprint5lines,Iwouldputthatrequestedvalueinparenthesesfollowingthefunctionname.Nowpauseaminuteandtrytorememberwhatwasthereoriginally.WhatdidIusetohaveinsteadofaparenthesis5?
Ifyourememberorgobackandlook.Itusedtobeaparenthesiswithnothinginside,justemptyparenthesis,OK.Theparenthesisistheretoholdtheinformationthat’sbeingsenttothefunctionandwehaveanameforthatkindofinformation,it’scalledaparameter.Thisisterminologywe’llma(4:
39)useextensivelyintherestofthecourse.Theparameter,informationthatistobesenttothefunction.Howwouldwewritethisfunction?
I’mnotgoingtogiveyoufullcodenow.It’snotreallyrelevant(有关的)whatthefunctionbodyisdoingandwe’llseeotherexampleslateroninthislecture.Thecrucialthingisthebeginningofthefunction,howthefunctionknowswhetherinformationtolookfor.Andthisisdoneinawaythatitiswhetherparallel(这里是函数声明的意思)tothefunctioncall.WestillhavethefunctionnameprintBianlLines.Butnowinparenthesiswehavesomethingthatlookslikeadeclaration.Itsays“intn”.Thinkbacktowhatwastherebefore.Itwasnotanemptyparenthesisbefore,itwasamagickeyword“void”(魔:
程序中经常出现的).I'vebrieflysaidthatthevoidmeantwerenotsendinginformationandtherearenoparameters.Nowthereisaparameter.Wewantthefunctiontoknowavalue.Sowhatwedoiswedeclare,whatwecallaformalparameter,we'llgiveitaname,we'llgiveitatype.ItisexactlythesameformatIusewhenIdeclarevariableintheprogramanditdoesdeclare"n".Itdeclareswhoselocationsandwhosenamesandwhosetypeisint.Thespecialaremagicthingsaboutthis"n"iswhenthefunctionstartstoexecute,itwillcontainan"n",thevaluesenttothefun