WEEK05: functions
---------------------------------------------------------------
F: more functions...
REVIEW of functionWorksheet.py:
- how many arguments are in the call to printdivider()?
answer: 1
- what is the type of the argument in the call to printdivider()?
answer: a string
- what is the name of the parameter in the printdivider() function?
answer: ch
- what does printdivider() return?
answer: nothing...it just prints stuff to the screen
- which function has the most parameters?
answer: function one has 4 parameters (num,s1,s2,s3)
- which function has the least parameters?
answer: oddfunction and main both have 0 params
- what is the difference between function2 and function3?
answer: they do the same thing (add 2x + 2y), but function3
returns the result to main, where function2 just prints it out
- if I did s1="jeff" somewhere in function1, would it change the
variable "a" back in main??
answer: no. it would just reassign s1, but not change a.
- why does the oddfunction() crash??
answer: it tries to use variables (a, b, c) that exist in
main but not in the current function
SCOPE:
- where in a program a given variable may be referenced or used
- the variables in any function are **local** to that function,
and do not exist outside of the function!!
- here's the function1() function from functionWorksheet.py:
def function1(num,s1,s2,s3):
print "in function1...dir = ", dir()
print "in function1...s3 = ", s3
print s1 + s2
print s3*num
you CAN NOT print s1 from main -- it only exists in function1().
and you CAN NOT print a variable from main (like a or b)
here in function1().
- here's a picture of the stack when the function1() function
is executing:
stack
| |
| |
-------------
|function1()|
| |
| num -----------> points to the 5 below
| s3 --------------------------------------
| s2 -------------------------- |
| s1 ---------------- | |
| | | | |
| | | | |
------------- | | |
|main() | | | |
| | V | |
| a ------------> "happy" V |
| b ----------------------> "pink" V
| c -----------------------------------> "pony"
| n ------------> 5
| |
| |
-------------
- when function1 is done, num,s1,s2,and s3 will be gone
- if you need some value from main in a function, you need to send
the value to the function via parameters (and call the function
with the value as an argument)
- if you need some value, calculated in a function, back in main, you
need to return the value from the function to main (using the
return call)
- the function currently executing is at the top of the stack.
the function that called it (if any) is right below it.
REVIEW of squares.py and the drawSquare() function:
- here's one way to write the drawSquare() function
def drawSquare(ul, side, window, fillcolor):
"""
draw a square with ul as upper left point. use side as the
length of square side. fill with given color and draw to window.
"""
# create lower-right corner
lr = ul.clone()
lr.move(side,side)
# make object and draw
s = Rectangle(ul,lr)
s.draw(window)
s.setFill(fillcolor)
YOUR TURN:
- here's a short but interesting function to try:
$ python maxList.py
[33, 25, 52, 39, 33, 46, 32, 22, 65, 40]
max of L is 65
$ python maxList.py
[25, 43, 1, 17, 22, 99, 35, 40, 15, 88]
max of L is 99
- in start-maxList.py I've written a main() function that creates
a 10-element list of random numbers
- your job is to add a maxList() function that takes the given
list and finds and returns the largest number from the list
maxList outline:
input = a list
output = the largest number from the list
** this is EASY for us humans to do, but tricky to write down
as an algorithm the computer can use....