- 2
- 0
- 约1.39万字
- 约 68页
- 2019-09-23 发布于湖北
- 举报
* * * * * End Functions that Modify Parameters Return values are the main way to send information from a function back to the caller. Sometimes, we can communicate back to the caller by making changes to the function parameters. Understanding when and how this is possible requires the mastery of some subtle details about how assignment works and the relationship between actual and formal parameters. Functions that Modify Parameters Suppose you are writing a program that manages bank accounts. One function we would need to do is to accumulate interest on the account. Let’s look at a first-cut at the function. def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance Functions that Modify Parameters The intent is to set the balance of the account to a new value that includes the interest amount. Let’s write a main program to test this:def test(): amount = 1000 rate = 0.05 addInterest(amount, rate) print amount Functions that Modify Parameters We hope that that the 5% will be added to the amount, returning 1050. test()1000 What went wrong? Nothing! Functions that Modify Parameters The first two lines of the test function create two local variables called amount and rate which are given the initial values of 1000 and 0.05, respectively. def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance def test(): amount = 1000 rate = 0.05 addInterest(amount, rate) print amount Functions that Modify Parameters Control then transfers to the addInterest function. The formal parameters balance and rate are assigned the values of the actual parameters amount and rate. Even though rate appears in both, they are separate variables (because of scope rules). def addInterest(balance, rate): newBalance = balance * (1 + rate) balance = newBalance def test(): amount = 1000 rate = 0.05 addInterest(amount, rate) print amount Functions that Modify Parameters Th
您可能关注的文档
- 三驾马车达成希腊救助协议悬望债券回购.ppt
- 上古卷轴5:天际药材采集地点炼金术士们的福利.pptx
- 三顾茅庐 完整.ppt
- 三顾茅庐课件11111.ppt
- 上善若水任方圆.pptx
- 上市公司信息披露与规范运作-张春芳.ppt
- 上善弱水 厚德载物.ppt
- 三顾茅庐 第二课时1.ppt
- 三顾茅庐ppt演示课文教程.ppt
- 上海中医药大学中医内科学 心 悸.ppt
- 03-2 2025年度民主生活会征求意见座谈会主持词.docx
- 03-1 2025年度民主生活会主持讲话提纲.docx
- 02-1 会前学习-《县以上党和国家机关党员领导干部民主生活会若干规定》相关要求.docx
- 在2025年度民主生活会上的总结讲话三篇.docx
- 在2025年度民主生活会上的总结讲话三篇 (3).docx
- 市委宣传部2025年度民主生活会查摆问题整改方案两篇.docx
- 在市行政审批和政务信息管理局机关2026年全体干部政绩观教育大会上的党课讲稿.docx
- X市应急管理局2026年度安全生产监督检查计划.docx
- 2025年度民主生活会领导班子对照检查材料(五个带头)三篇.docx
- 2025年度民主生活会“五个带头”个人对照检查材料三篇.docx
原创力文档

文档评论(0)