Class07.docx

上传人:b****5 文档编号:2862401 上传时间:2022-11-16 格式:DOCX 页数:9 大小:41.57KB
下载 相关 举报
Class07.docx_第1页
第1页 / 共9页
Class07.docx_第2页
第2页 / 共9页
Class07.docx_第3页
第3页 / 共9页
Class07.docx_第4页
第4页 / 共9页
Class07.docx_第5页
第5页 / 共9页
点击查看更多>>
下载资源
资源描述

Class07.docx

《Class07.docx》由会员分享,可在线阅读,更多相关《Class07.docx(9页珍藏版)》请在冰豆网上搜索。

Class07.docx

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

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 人文社科 > 设计艺术

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1