7. Functions 142-253 Computer Programming BSc in Digital Media, PSUIC Aj. Andrew Davison, CoE, PSU Hat Yai Campus E-mail: ad@fivedots.coe.psu.ac.th 7. Functions How to write and use functions – reusable chunks of code. A look at argument passing, return, local and global variables.
1. Address Function I often need to print my address instead of writing the code many times, write the code once in a function, and reuse the function Address.py
Making the Function More Reusable Pass in data (an argument) to the function, which can change with each call. Address1.py
Many Arguments Even More Reuseable Address2.py
2. Having a Function Return Something A function that calculates something would be very useful if it returned the answer back to the main program (the caller) use return at the end of the function return answer
Calculating Tax CalcTax-1.py
Main and Function in Pictures main program (the caller) calculateTax() (the function) integer (immutable) copy data p price tax_ rate 1 : totalPrice = calculateTax( price, 0.06) total : return total 2 integer (immutable) copy data total Price
3. Local Variables in a Function Local variables in a function are created when the function starts, and disappear when the function returns. calculateTax() p tax_ rate local variables local variables disappear when the function returns total : return total
CalcTaxLocalChg-2.py p is a local variable p's data is a copy of price's data p disappears at the end of the function
CalcTaxLocal-3.py p is a local variable p's data is a copy of price's data p disappears at the end of the function
4. Global Variables in a Program Global variables can be seen by every function in a program. Global variables can not be changed in a function unless you use a special word... (see later) All variables in the main program are global variables.
main program (the caller) global variables price They can be seen by every function. They cannot be changed inside a function price : totalPrice = calculateTax( price, 0.06) total Price
CalcTaxGlobalUse-4.py p, tax_rate, and total are local variables in calculateTax() price is a global variable defined in the main program
CalcTaxGlobalChg-5.py price is a global variable defined in the main program It can be seen but not changed in the function. When a change is tried, a new local variable is made instead
No Global Variable Changing calculateTax() main program (the caller) p no changing of globals allowed inside function tax_ rate price total price = 10000 When a change is tried, a local variable is made instead 10000 price total Price : return total
Global Variable can be Changed! Python allows a global variable to be changed inside a function if the variable is labelled as global in the function. Avoid this kind of programming. It makes programs hard to understand.
CalcTaxGlobalChg-6.py price is a global variable defined in the main program price is labelled as global in the function. It can now be changed inside the function.
Global Variable Changing calculateTax() main program (the caller) p tax_ rate price changing of price allowed total global price price = 10000 No local variable created : return total total Price Bad style – avoid this kind of programming
5. Silly Sentences
6. Draw a Smiley (bad coding style) DrawSmiley1.py
Horrible, but it Works... It is bad style because it is very long, with no comments to explain it. A big function should be split into smaller functions. A BIG problem is easier to solve if it is split into smaller problems.
Lots of Magic Numbers in the Code A drawing helps: circle: start at (0,0), radius: 50 left eye: start at (60,-15), r: 10 right eye: start at (60,15), r: 10 -- should be able to use one function, called two times mouth: 3 thick lines: (-25,40) --> (-10,20) (-10,20) --> (10,20) (10,20) --> (25,40) This should be in the program as comments.
7. Draw a Smiley (good style) Easier to understand Easier to change Use comments and functions DrawSmiley2.py
Drawing Lots of Smileys Only the main code changes: DrawSmileys.py
Execution
8. Make Shape Drawing Easier to Understand ShapesInput.py is getting long and complicated. It can be made easier to understand with functions.
ShapesInput.py with a Function
9. Spirograph.py import turtle """ draw a spirograph of shapes using the angle of rotation to turn after each shape is drawn def drawSpiro(ttl, lineLen, numSides, rotAngle): numShapes = 360 // rotAngle for shape in range(numShapes): drawShape(ttl, lineLen, numSides) ttl.right(rotAngle) # draw a shape using the line length and number of sides def drawShape(ttl, lineLen, numSides): if ((numSides > 2) and (lineLen > 0)): angle = 360 / numSides for i in range(numSides): ttl.forward(lineLen) ttl.right(angle) :
# main t = turtle. Turtle() t. speed("fastest") rotAngle = int(turtle # main t = turtle.Turtle() t.speed("fastest") rotAngle = int(turtle.numinput("Shape Rotation", "How much should each shape be rotated?")) lineLen = 100 t.color('blue'); drawSpiro(t, lineLen, 3, rotAngle); t.color('red') drawSpiro(t, lineLen, 4, rotAngle); t.color('green'); drawSpiro(t, lineLen, 5, rotAngle); t.color('orange'); drawSpiro(t, lineLen, 6, rotAngle);
Execution
10. Turtles Walking About
TurtlesWalk.py Ten turtles are stored in a list, and each one is randomly moved and turned
11. Draw Multicolored Hearts
Hearts.py # main t = Turtle() hearts(t) from math import * from turtle import Turtle colors = ["red", "orange", "yellow", "green", "blue", "purple", "violet"] def hearts(ttl): # draw 50 hearts in different colors ttl.speed(0) for i in range(1, 50): color(ttl, i) at(ttl, 0, i * -5) heart(ttl, i * 10, 45) def color(ttl, i): # cycle through the colors list to set the pen ttl.pencolor(colors[i % len(colors)])
def at(ttl, x, y): # move the turtle to (x,y), pointing up screen ttl def at(ttl, x, y): # move the turtle to (x,y), pointing up screen ttl.penup() ttl.home() ttl.forward(x) ttl.left(90) ttl.forward(y) ttl.pendown() def heart(ttl, size, angle): # a heart is made from 2 arcs and 2 straight lines ttl.width(10) radius = size * sin(radians(angle))/ \ (1 + sin(radians(90-angle))) ttl.right(angle) ttl.forward(size) # line ttl.circle(radius, 180 + angle) # arc ttl.right(180) ttl.left(180 - angle)