1 / 45

Announcements

Announcements. Project1 Due Wednesday September 21 st 4:30pm We will post instructions on how to turnin from home Note: you can always turn in from the lab machines Midterm 1 October 6 th 2011 WTHR 200 Midterm 2 November 8 th 2011 WTHR 200. Project 1.

zev
Download Presentation

Announcements

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Announcements • Project1 • Due Wednesday September 21st 4:30pm • We will post instructions on how to turnin from home • Note: you can always turn in from the lab machines • Midterm 1 • October 6th 2011 WTHR 200 • Midterm 2 • November 8th 2011 WTHR 200

  2. Project 1 You will be tasked with writing function to calculate: the velocity need to hit a pig at a given distance and elevation with a certain angle.

  3. Project 1 You will be tasked with writing function to verify the trajectory using the information you calculated in the previous function. Did rounding affect the calculation?

  4. Project 1 You will be tasked with displaying some of the information you calculate graphically and “gluing” the various parts together in one larger “main” function.

  5. Python Boot Camp! • Multiple Arguments • Returns • Passing a value out of a function (output) • Last statement executed • Scope • Local Variables • Comments / Coding Practices

  6. Indentation • Indentation is very important in python • In functions it defines where the function ends • Python will give errors if the indentation is incorrect

  7. Multiple Arguments • Functions can take multiple arguments defsumOfTwo(a,b): returna+b defsumOfThree(a,b,c): returna+b+c defsumOfFour(a,b,c,d): returna+b+c+d

  8. Call Sites • You must provide the number of arguments the function expects defsumOfTwo(a,b): returna+b print(sumOfTwo(4,5)) defsumOfThree(a,b,c): returna+b+c print(sumOfThree(3,3,3)) defsumOfFour(a,b,c,d): returna+b+c+d print(sumOfFour(3,3,3)) ERROR!

  9. Clicker Question:Are these programs equivalent? 1 2 defmyFun(a): print (a) return 5 print(myFun(4)) defmyFun(a): print (a) return 5 res = myFun(4) print(res) A: yes B: no

  10. “Capturing” the return • The return statement defines the value that is passed out from the function • We can store the return value in a variable defsumOfTwo(a,b): returna+b result = sumOfTwo(4,5)) defsumOfThree(a,b,c): returna+b+c result = sumOfThree(3,3,3)

  11. Stringing together functions • We can build up complex expressions from functions defsumOfTwo(a,b): returna+b print(sumOfTwo(sumOfTwo(4,5), sumOfTwo(1,2))) OR a = sumOfTwo(4,5) b = sumOfTwo(1,2) c = sumOfTwo(a,b) print(c)

  12. A “Sample” Program def task1(a,b): … y = …. return y deftask2(a,b,c): …. z = … returnz def main(): input1, input2 = eval(input(…)) output1 = task1(input1, input2) output2 = task2(input1, input2, output1) print(output2)

  13. More on Returns • Returns are the last statement executed by a function • The function is considered to be “done” at this point • There may be other statements “after” the return, but these are omitted defsumOfTwo(a,b): returna+b print(a+b) print(a+b) print(sumOfTwo(4,5))

  14. Clicker Question:Are these programs equivalent? 1 2 a = 3 defmyFun(a): print (a) myFun(4) a = 3 print (a) A: yes B: no

  15. Naming and Binding • Arguments are bound at the “call site” • Call site: a point in the program where the function is called • The binding is active up until the return of the function

  16. Naming and Binding a = 3 defmyFun(a): print (a) myFun(4) print(a)

  17. Local Variables • We saw that arguments can be rebound for the duration of the call • What about variables that we define in the function body?

  18. Local Variables • Abstractly there are two types of local variables • 1) those which introduce a new variable • 2) those which redefine an existing variable which was initially defined outside the function definition • Local variables, just like arguments, have a lifetime associated with the function call

  19. Local Variables a = 3 y = 10 defmyFun(a): print (a) y = 1 myFun(4) print(a) print(y)

  20. Clicker Question: does this program print 3 or 4? x = 3 defmyFun(): print (x) x = 4 myFun() A: 3 B: 4

  21. Homework • Work on Project 1 • Read chapter 6 (functions)

  22. Announcements • Project1 • Due Wednesday September 21st 4:30pm • We will post instructions on how to turnin from home • Note: you can always turn in from the lab machines • PreLab 4 • Important! Practice  • Lab 4 • Graphics Library methods

  23. Python Boot Camp! • Local vs Global Variables • Comments / Coding Practices

  24. Variables and Functions • Variables used in functions but defined outside of the function can be changed • Multiple calls to a function may yield different results if the program “rebinds” such variables

  25. Variables and Functions x = 3 defmyFun(): print (x) x = 4 myFun() x =5 myFun()

  26. You must be careful! x = 3 defmyFun(): print (x) x =1 x = 4 myFun() x =5 myFun() ERROR! ERROR!

  27. Global Variables • How can we get the example code we saw earlier to work? • Python is not sure if we want x to be a local variable or if it should refer to the x we defined outside of the function • We can inform python if we want x to refer to the variable outside of the function • New keyword global

  28. This works! x = 3 defmyFun(): global x print (x) x =1 x = 4 myFun() myFun()

  29. Global or local? • If the global keyword is use, the variable is global • If the first use of the variable is a read the variable is global • NOTE: We cannot assign to the variable • The variable is not defined in the arguments • If the first use of the variable is a write (assignment) the variable is local • Unless the variable is defined global

  30. Clicker Question: Is X global or local? x = 3 defmyFun(): y = 4 z = x + y myFun() A: global B: local

  31. Lets Review • Functions take input and produce output • Output is provided by the “return” statement • Otherwise the function does not provide output • At the call site of the function the arguments get bound • The arguments can rebind variables that have already been defined for the duration of the call • You can use variables defined outside the function but you must be careful!

  32. Function Arguments • Functions typically assume something important about the arguments • Will this work no matter what we provide as arguments? defsumOfTwo(a,b): returna+b

  33. Function Arguments • Consider the following three cases: • One of these cases will throw an error. This behavior is defined by the code inside the function res = sumOfTwo(1,2) res = sumOfTwo(“Hello “, “World”) res = sumOfTwo(“Hello”, 1)

  34. Function Arguments • There are two ways to handle this difficulty • Tell everyone what the function expects • Include checks inside the function to ensure the arguments are what is expected • A combination of both techniques should be used

  35. Function Documentation • This solution uses comments and if-statements. • We will revisit this in later slides #This function expects two integers and returns -1 defsumOfTwo(a,b): if(type(a) == intandtype(b) == int) : returna+b return-1

  36. 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. • defaddInterest(balance, rate):newBalance = balance * (1 + rate) balance = newBalance

  37. 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.05addInterest(amount, rate) print(amount)

  38. Functions that Modify Parameters • We hope that that the 5% will be added to the amount, returning 1050. • >>> test()1000 • What went wrong? Nothing!

  39. 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. defaddInterest(balance, rate): newBal= balance*(1+rate) balance = newBal def test(): amount = 1000 rate = 0.05 addInterest(amount, rate) print(amount) Functions that Modify Parameters

  40. Functions that Modify Parameters

  41. Functions that Modify Parameters

  42. Functions that Modify Parameters defaddInterest(balance, rate): newBalance = balance * (1 + rate) return newBalance def test(): amount = 1000 rate = 0.05 amount = addInterest(amount, rate) print(amount) test()

  43. Functions that Modify Parameters amount = 1000 rate = 0.05 defaddInterest(): global amount global rate amount = amount * (1 + rate) addInterest() print(amount)

  44. What all can I do within the body of a function? • Define local variables • Call other functions • Define new functions

  45. Homework • Work on Project 1 • Do the pre lab / review chapter 4 • Read Chapter 6 • Read Python Wiki “Functions” • http://en.wikibooks.org/wiki/Python_Programming/Functions

More Related