Download - Course A201: Introduction to Programming
![Page 1: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/1.jpg)
Course A201:Introduction to Programming
11/11/2010
![Page 2: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/2.jpg)
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
![Page 3: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/3.jpg)
Recap
• Difference between return and print Ex: circle_area.py
def circle_area(radius): return (3.14159 * radius ** 2)
circle_area(5) print(“Now I know the area of the circle”)
![Page 4: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/4.jpg)
Recap def circle_area(radius): return (3.14159 * radius ** 2)
circle_area(5) print (“Now I know the area of
the circle”)
I’m IDLE for a while…
![Page 5: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/5.jpg)
Recap def circle_area(radius): return (3.14159 * radius ** 2)
circle_area(5) print (“Now I know the area of
the circle”)
Below is my Memory
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
![Page 6: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/6.jpg)
Recap def circle_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
I know function circle_area !
Start to execute.f- means function, p- means
parameter
![Page 7: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/7.jpg)
Recap def circle_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
(in f-circle_area) radius = 5
Program tells me that now the parameter
radius is 5
![Page 8: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/8.jpg)
Recap def circle_area(radius): return (3.14159 * radius ** 2)
circle_area(5) print(“Now I know the area of
the circle”)
IDLE’s memory
Go to functioncircle_area(radius)
f-circle_area with p-radius
(in f-circle_area) radius = 5
This region of memory is only for function circle_area
![Page 9: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/9.jpg)
Recap def circle_area(radius): return (3.14159 * radius ** 2)
circle_area(5) print(“Now I know the area of
the circle”)
IDLE’s memory
Go to functioncircle_area(radius)
f-circle_area with p-radius
(in f-circle_area) radius = 5
It will be erased when I finish executing the function
![Page 10: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/10.jpg)
Recap def circle_area(radius): return (3.14159 * radius ** 2)
circle_area(5) print(“Now I know the area of
the circle”)
IDLE’s memory
Go to functioncircle_area(radius)
f-circle_area with p-radius
(in f-circle_area) radius = 5
radius = 5
![Page 11: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/11.jpg)
Recap def circle_area(radius): return (3.14159 * radius ** 2)
circle_area(5) print(“Now I know the area of
the circle”)
IDLE’s memory
Go to functioncircle_area(radius)
f-circle_area with p-radius
(in f-circle_area) radius = 5
radius = 5return value = 78.53975
![Page 12: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/12.jpg)
Recap def circle_area(radius): return (3.14159 * radius ** 2)
circle_area(5) print(“Now I know the area of
the circle”)
IDLE’s memory
Jump out of function circle_area
f-circle_area with p-radius
(in f-circle_area) radius = 5(f-circle_area) return
value=78.53975
![Page 13: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/13.jpg)
Recap def circle_area(radius): return (3.14159 * radius ** 2)
circle_area(5) print(“Now I know the area of
the circle”)
IDLE’s memory
Result stays in the memory of IDLE, but nothing will be shown on the screen
f-circle_area with p-radius
(in f-circle_area) radius = 5(f-circle_area) return
value=78.53975
Nothing on screen
![Page 14: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/14.jpg)
Recap def circle_area(radius): return (3.14159 * radius ** 2)
circle_area(5) print(“Now I know the area of
the circle”)
IDLE’s memory
Finish executing the function, goes to the next line
f-circle_area with p-radius
(in f-circle_area) radius = 5(f-circle_area) return
value=78.53975Execute print
Now I know the area of the circle
![Page 15: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/15.jpg)
Recap def circle_area(radius): return (3.14159 * radius ** 2)
circle_area(5) print(“Now I know the area of
the circle”)
IDLE’s memory
Finish executing the function, goes to the next line
f-circle_area with p-radius
(in f-circle_area) radius = 5(f-circle_area) return
value=78.53975Execute print
Now I know the area of the circle
The result is not stored in any variable, so it is lost!
![Page 16: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/16.jpg)
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)
![Page 17: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/17.jpg)
Let’s change to print
• Difference between return and print Ex: circle_area.py
def circle_area(radius): print(3.14159 * radius ** 2)
circle_area(5)
![Page 18: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/18.jpg)
Recap def circle_area(radius): print(3.14159 * radius ** 2)
circle_area(5)
IDLE’s memory
Go to functioncircle_area(radius)
f-circle_area with p-radius
(in f-circle_area) radius = 5
radius = 5
![Page 19: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/19.jpg)
Recap def circle_area(radius): print(3.14159 * radius ** 2)
circle_area(5)
I am asked to print a value
Screen shows: 78.53975
f-circle_area with p-radius
(in f-circle_area) radius = 5
radius = 5
78.53975
![Page 20: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/20.jpg)
Recap def circle_area(radius): print(3.14159 * radius ** 2)
circle_area(5)
IDLE’s memory
Screen shows: 78.53975
f-circle_area with p-radius
(in f-circle_area) radius = 5
radius = 5return value = None
78.53975
![Page 21: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/21.jpg)
Recap def circle_area(radius): print(3.14159 * radius ** 2)
circle_area(5)
IDLE’s memory
Jump out of function circle_area, no return value
f-circle_area with p-radius
(in f-circle_area) radius = 5(f-circle_area) return value
is None
78.53975
![Page 22: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/22.jpg)
Recap def circle_area(radius): print(3.14159 * radius ** 2)
circle_area(5)
IDLE’s memory
Jump out of function circle_area, no return value
f-circle_area with p-radius
(in f-circle_area) radius = 5(f-circle_area) return value
is None
78.53975
![Page 23: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/23.jpg)
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
![Page 24: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/24.jpg)
Recap
[parameter]– A special kind of variable– In the parentheses of function definition,
separated by commas ex: def circle_area(radius): def print_heading(title):
![Page 25: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/25.jpg)
Recap
[argument]– The values in the parentheses in function calls– Each value then is assigned to the corresponding
parameter ex: def circle_area(radius): …… print(circle_area(5)) This is a function call
![Page 26: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/26.jpg)
Recap
[argument]– The values in the parentheses in function calls– Each value then is assigned to the corresponding
parameter ex: def circle_area(radius): …… print(circle_area(5)) “5” is the argument
![Page 27: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/27.jpg)
Recap
[argument]– The values in the parentheses in function calls– Each value then is assigned to the corresponding
parameter ex: def circle_area(radius): …… print(circle_area(5))
Then radius is assigned with value 5 in the function of cricle_area
![Page 28: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/28.jpg)
Recap
[argument]– The values in the parentheses in function calls– Each value then is assigned to the corresponding
parameter ex: def circle_area(r1, r2, r3): …… print(circle_area(5, 6, 7))
In the same way:r1 is assigned 5r2 is assigned 6r3 is assigned 7
![Page 29: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/29.jpg)
Recap
[default parameter values]def birthday1(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.
![Page 30: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/30.jpg)
Variable scope
• See lecture slides: Global variable and Local variable
• Let’s see an example
![Page 31: Course A201: Introduction to Programming](https://reader036.vdocuments.net/reader036/viewer/2022062410/56815f3e550346895dce10a5/html5/thumbnails/31.jpg)
Recursive Function
• See lecture slides: Recursive Function