Title: FUNCTIONS in Python
1FUNCTIONS in Python
2What Are Functions?
- Functions are sub-programs which perform tasks
which may need to be repeated - Some functions are bundled in standard
libraries which are part of any languages core
package - Functions are similar to methods, but may not be
connected with objects - Programmers can write their own functions
3Why Write Functions?
- Reusability
- Fewer errors introduced when code isnt rewritten
- Reduces complexity of code
4Happy Birthday Example Function Flow
- happy.py
- Simple illustration of functions.
- def happy()
- print "Happy Birthday to you!"
- def sing(person)
- happy()
- happy()
- print "Happy birthday, dear", person "."
- happy()
- def main()
- sing("Fred")
- print
- sing("Lucy")
- print
- sing("Elmer")
5Functions Formal vs. Actual Paramaters (and
Arguments)
Formal Parameters
- moveto.py
- from graphics import
- def moveTo(object, point)
- c object.getCenter()
- dx point.getX() - c.getX()
- dy point.getY() - c.getY()
- object.move(dx,dy)
- def main()
- win GraphWin()
- circ Circle(Point(100,100), 20)
- circ.draw(win)
- p win.getMouse()
- moveTo(circ, p)
- win.close()
- center circ.getCenter()
- print center.getX(), center.getY()
- main()
Function definition
Actual parameters or arguments
Call or invocation of function Arguments must be
in correct order according to function definition
6Functions Return values
- return keyword indicates what value(s) will be
kicked back after a function has been invoked - def square(x) return x xThe call
output square(myNum)
Formal parameter
Return value
7Return value used as argument Example of
calculating a hypotenuse
- num1 10
- num2 14
- Hypotenuse math.sqrt(sum_of_squares(num1,
num2)) - def sum_of_squares(x,y) t (xx) (y y)
return t
8Triangle2.py example
- Triangle2.py
- Text of triangle2.py
9Returning more than one value
- Functions can return more than one value
- def hi_low(x,y) if x y return x, y
else return y, x - The callhiNum, lowNum hi_low(data1, data2)
10Modifying Parameters How could this be?
- Usually, we ask a function to return a value to
the calling function. This is how the calling
function gets data processed. - But we can also have a function modify a
parameter used in the calling function. - This seems counterintuitive, since we said
earlier that the scope of a variable is limited
to the function in which it is declared. - It also seems counterintuitive because we know
that the VALUE of the parameters is passed to
other functions, not the parameters themselves.
11Function call that fails to get a value back
- def addInterest(balance, rate)
- newBalance balance (1rate)
- balance newBalance
- def test()
- amount 1000 rate 0.05addInterest(amount,
rate) - print amount
- test()
12Function call that gets a return value
- def addInterest(balance, rate) newBalance
balance (1rate) return newBalance def
test()amount 1000rate 0.05amount
addInterest(amount, rate)print amounttest()
13Changing parameter values by reference
- Some programming languages allow a calling
function to pass parameters by reference (the
memory address of the variable is passed, not the
actual value, so the value of parameter in the
calling function is changed. PYTHON DOES NOT
ALLOW THIS
14Passing parameters by value
- Python (like all languages) permits passing
parameter VALUES, not actual parameters, as we
have already seen. This is called PASSING BY VALUE
15Changing passed parameters
- Python does have a mechanism for having a called
function alter the value of a parameter passed by
a calling function. - The trick is that Python allows OBJECTS to be
passed, and when this happens the values
associated with that object CAN be changed by a
called function.
16Changing parameters passed as objects
- def addInterest(balances, rate)for i in
range(len(balances))balancesi balancesi
(1rate)def test()amounts 1000, 2200, 800,
360rate 0.05addInterest(amounts, 0.05)print
amountstest()
17Summary passing parameters
- Python always passes VARIABLES (given as
parameters) by VALUE. In this case, the original
variables in the calling function are NOT
changed. - Functions can RETURN a value which alters a
variable in the calling function. - Python allows OBJECTS (lists, graphics, etc.) to
be passed and altered by functions.
18Program Structure
- Functions serve to reduce complexity in the main
algorithm of a program, making the program easier
to understand and maintain. - Examine futval_graph4.py