Presentation is loading. Please wait.

Presentation is loading. Please wait.

7. Functions Computer Programming BSc in Digital Media, PSUIC

Similar presentations


Presentation on theme: "7. Functions Computer Programming BSc in Digital Media, PSUIC"— Presentation transcript:

1 7. Functions 142-253 Computer Programming BSc in Digital Media, PSUIC
Aj. Andrew Davison, CoE, PSU Hat Yai Campus 7. Functions How to write and use functions – reusable chunks of code. A look at argument passing, return, local and global variables.

2 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

3 Making the Function More Reusable
Pass in data (an argument) to the function, which can change with each call. Address1.py

4 Many Arguments  Even More Reuseable
Address2.py

5 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

6 Calculating Tax CalcTax-1.py

7 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

8 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

9 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

10 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

11 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.

12 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

13 CalcTaxGlobalUse-4.py p, tax_rate, and total are local variables
in calculateTax() price is a global variable defined in the main program

14 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

15 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

16 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.

17 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.

18 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

19 5. Silly Sentences

20 6. Draw a Smiley (bad coding style)
DrawSmiley1.py

21 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.

22 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.

23 7. Draw a Smiley (good style)
Easier to understand Easier to change Use comments and functions DrawSmiley2.py

24 Drawing Lots of Smileys
Only the main code changes: DrawSmileys.py

25 Execution

26 8. Make Shape Drawing Easier to Understand
ShapesInput.py is getting long and complicated. It can be made easier to understand with functions.

27 ShapesInput.py with a Function

28 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) :

29 # 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);

30 Execution

31 10. Turtles Walking About

32 TurtlesWalk.py Ten turtles are stored in a list, and each one is randomly moved and turned

33 11. Draw Multicolored Hearts

34 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)])

35 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, angle) # arc ttl.right(180) ttl.left(180 - angle)


Download ppt "7. Functions Computer Programming BSc in Digital Media, PSUIC"

Similar presentations


Ads by Google