1 / 31

Course A201: Introduction to Programming

Course A201: Introduction to Programming. 11/11/2010. Outline for this week. Assignment 6 Recap for f unction Variable scope: global variable vs local variable Recursive function Assignment 8 Divide into two teams, come up with pseudo code then choose a partner. Recap.

patsy
Download Presentation

Course A201: Introduction to Programming

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. Course A201:Introduction to Programming 11/11/2010

  2. Outline for this week • Assignment 6 • Recap for function • Variable scope: global variable vs local variable • Recursive function • Assignment 8 • Divide into two teams, come up with pseudo code • then choose a partner

  3. Recap • Difference between return and print Ex: circle_area.py defcircle_area(radius): return (3.14159 * radius ** 2) circle_area(5) print(“Now I know the area of the circle”)

  4. Recap I’m IDLE for a while… defcircle_area(radius): return (3.14159 * radius ** 2) circle_area(5) print (“Now I know the area of the circle”)

  5. Recap Below is my Memory defcircle_area(radius): return(3.14159 * radius ** 2) circle_area(5) print (“Now I know the area of the circle”) This region shows what I know before I starts to execute the program This region shows what I know during I’m executing the program

  6. Recap I know function circle_area ! defcircle_area(radius): return(3.14159 * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) f-circle_area with p-radius • Start to execute. • f- means function, p- means parameter

  7. Recap Program tells me that now the parameter radius is 5 defcircle_area(radius): return (3.14159 * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) f-circle_area with p-radius • (in f-circle_area) radius = 5 • Start to execute. • f- means function, p- means parameter

  8. Recap IDLE’s memory defcircle_area(radius): return(3.14159 * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) f-circle_area with p-radius (in f-circle_area) radius = 5 This region of memory is only for function circle_area • Go to function • circle_area(radius)

  9. Recap IDLE’s memory defcircle_area(radius): return(3.14159 * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) f-circle_area with p-radius (in f-circle_area) radius = 5 It will be erased when I finish executing the function • Go to function • circle_area(radius)

  10. Recap IDLE’s memory defcircle_area(radius): return(3.14159 * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) f-circle_area with p-radius (in f-circle_area) radius = 5 radius = 5 • Go to function • circle_area(radius)

  11. Recap IDLE’s memory defcircle_area(radius): return(3.14159 * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) f-circle_area with p-radius (in f-circle_area) radius = 5 radius = 5 return value = 78.53975 • Go to function • circle_area(radius)

  12. Recap IDLE’s memory defcircle_area(radius): return(3.14159 * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) f-circle_area with p-radius (in f-circle_area) radius = 5 (f-circle_area) return value=78.53975 • Jump out of function circle_area

  13. Recap Nothing on screen IDLE’s memory defcircle_area(radius): return(3.14159 * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) f-circle_area with p-radius (in f-circle_area) radius = 5 (f-circle_area) return value=78.53975 • Result stays in the memory of IDLE, but nothing will be shown on the screen

  14. Recap Now I know the area of the circle IDLE’s memory defcircle_area(radius): return(3.14159 * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) f-circle_area with p-radius (in f-circle_area) radius = 5 (f-circle_area) return value=78.53975 Execute print • Finish executing the function, goes to the next line

  15. Recap Now I know the area of the circle IDLE’s memory defcircle_area(radius): return(3.14159 * radius ** 2) circle_area(5) print(“Now I know the area of the circle”) f-circle_area with p-radius (in f-circle_area) radius = 5 (f-circle_area) return value=78.53975 Execute print • The result is not stored in any variable, so it is lost! • Finish executing the function, goes to the next line

  16. Recap • Therefore, the correct way to call a function with a return statement is: result = circle_area(5) print(“Now I know the area of the circle:”, result) result = result * 2 print(“And also the area of 2 such circles:”, result)

  17. Let’s change to print • Difference between return and print Ex: circle_area.py defcircle_area(radius): print(3.14159 * radius ** 2) circle_area(5)

  18. Recap IDLE’s memory defcircle_area(radius): print(3.14159 * radius ** 2) circle_area(5) f-circle_area with p-radius (in f-circle_area) radius = 5 radius = 5 • Go to function • circle_area(radius)

  19. Recap 78.53975 I am asked to print a value defcircle_area(radius): print(3.14159 * radius ** 2) circle_area(5) f-circle_area with p-radius (in f-circle_area) radius = 5 radius = 5 • Screen shows: • 78.53975

  20. Recap 78.53975 IDLE’s memory defcircle_area(radius): print(3.14159 * radius ** 2) circle_area(5) f-circle_area with p-radius (in f-circle_area) radius = 5 radius = 5 return value = None • Screen shows: • 78.53975

  21. Recap 78.53975 IDLE’s memory defcircle_area(radius): print(3.14159 * radius ** 2) circle_area(5) f-circle_area with p-radius (in f-circle_area) radius = 5 (f-circle_area) return value is None • Jump out of function circle_area, no return value

  22. Recap 78.53975 IDLE’s memory defcircle_area(radius): print(3.14159 * radius ** 2) circle_area(5) f-circle_area with p-radius (in f-circle_area) radius = 5 (f-circle_area) return value is None • Jump out of function circle_area, no return value

  23. Notice • What shows on the screen does not equal with what IDLE knows/keeps in its memory • When you write all codes in a file and execute it by pressing F5, IDLE will not show what it knows onto the screen unless you tell it to, using print

  24. Recap [parameter] • A special kind of variable • In the parentheses of function definition, separated by commas ex: defcircle_area(radius): defprint_heading(title):

  25. Recap [argument] • The values in the parentheses in function calls • Each value then is assigned to the corresponding parameter ex: defcircle_area(radius): …… print(circle_area(5)) • This is a function call

  26. Recap [argument] • The values in the parentheses in function calls • Each value then is assigned to the corresponding parameter ex: defcircle_area(radius): …… print(circle_area(5)) • “5” is the argument

  27. Recap [argument] • The values in the parentheses in function calls • Each value then is assigned to the corresponding parameter ex: defcircle_area(radius): …… print(circle_area(5)) • Then radius is assigned with value 5 in the function of cricle_area

  28. Recap [argument] • The values in the parentheses in function calls • Each value then is assigned to the corresponding parameter ex: defcircle_area(r1, r2, r3): …… print(circle_area(5, 6, 7)) • In the same way: • r1 is assigned 5 • r2 is assigned 6 • r3 is assigned 7

  29. Recap [default parameter values] defbirthday1(name= “Jackson”,age=1): print(“Happy birthday,”, name, “!”, “ I hear you're”, age, “today.\n”) >>>birthday1(‘Mary’, 2) Happy birthday, Mary ! I hear you are 2 today. >>>birthday1() Happy birthday, Jackson ! I hear you are 1 today.

  30. Variable scope • See lecture slides: Global variable and Local variable • Let’s see an example

  31. Recursive Function • See lecture slides: Recursive Function

More Related