资源描述:
《c语言入门教程函数递归(c language tutorial function recursion)》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、c语言入门教程函数递归(Clanguagetutorialfunctionrecursion)Clanguagetutorial:functionrecursive.TxtLaoHuyou,childrencallededucation,childrenfooledLaoZicalleddeception,flickereachothercalledgenerationgap.Themaninthisworld,Iwanttousewhatcolortoattractyou.First,stackWhenitcomestorecursivefunctions,byth
2、eway,theconceptofthestack.Stackisabackwardinfirstout(push)andpop(POP)datastructure.Whentheprogramisrunning,thesystementersanobjectintothestackeachtime,andthenthestackpointermovesdownaposition.Whenthesystempopsupanobjectfromthestack,theobjectthathasrecentlyenteredthestackwillpopup.Thenth
3、estackpointermovesupaposition.Programmersoftenusethestackdatastructuretohandleprogrammingproblemsthatarebestsuitedtothedescriptionofbackwardfirstoutlogic.Thestackintheprogramdiscussedhereispresentineveryprogram.Itdoesnotrequireprogrammerstowritecodetomaintain,butrunsautomaticallybythesy
4、stem.Theso-calledsystemautomaticmaintenance,infact,isgeneratedbythecompilercode.Evenifyoudon'tseetheminthesourcecode,programmersshouldknowsomethingaboutit.Lookathowthestackintheprogramworks.Whenafunction(caller)callsanotherfunction(callee),theruntimesystemwillgivethecalleralltheargument
5、sandthereturnaddressontothestack,thestackpointerwillmovetotherightpositiontoaccommodatethesedata.Thelastentrytothestackisthecaller'sreturnaddress.Whenthecallerstartsexecuting,thesystempressesthecaller'sindependentvariableintothestackandmovesthestackpointerdowntoensurethatthereisenoughsp
6、acetostorealltheargumentsdeclaredbythecaller.Whenthecallerpressestheargumentintothestack,thecallerbuildstheparameterintheformofanindependentvariableinthestack.Othervariablesinsidethecallerarealsostoredinthestack.Becauseofthesestackoperations,thestackpointerhasmovedbelowalltheselocalvari
7、ables.However,thecallerrecordstheinitialstackpointerwhenitisbeginningtoexecute,anduseshimasareferencetoaccessthevariablesinthestackwithpositiveornegativeoffsetvalues.Whenthecallerisreadytoreturn,thesystempopsupalltheindependentvariablesinthestack,andthenthestackpointermovesthep