Presentation is loading. Please wait.

Presentation is loading. Please wait.

8. Functions 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus

Similar presentations


Presentation on theme: "8. Functions 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus"— Presentation transcript:

1 8. Functions 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus E-mail: ad@fivedots.coe.psu.ac.th How to write and use functions – reusable chunks of code. A look at argument passing, return, local and global variables.

2  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 1. Address Function 2 Address.py

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

4 Many Arguments  Even More Reuseable 4 Address2.py

5  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 2. Having a Function Return Something 5 return answer

6 Calculating Tax 6 CalcTax-1.py

7 Main and Function in Pictures 7 main program (the caller) integer (immutable) copy data : totalPrice = calculateTax( price, 0.06) : return total price total Price calculateTax() (the function) integer (immutable) copy data p tax_ rate 1 2 total

8  Local variables in a function are created when the function starts, and disappear when the function returns. 3. Local Variables in a Function 8 : return total calculateTax() p tax_ rate total local variables local variables disappear when the function returns

9 CalcTaxLocalChg-2.py 9 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 10 p is a local variable p's data is a copy of price's data p disappears at the end of the function

11  Global variables can be seen by every function in a program.  Global variables can not be changed by a function unless you use the special word global.  All variables in the main program are global variables. 4. Global Variables in a Program 11

12 12 main program (the caller) : totalPrice = calculateTax( price, 0.06) price total Price global variables They can be seen by every function. They cannot be changed unless the function uses the special word global

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

14 CalcTaxGlobalChg-5.py 14 price is a global variable defined in the main program It can be seen but not changed by the function. When a change is tried, a new local variable is made instead

15 No Global Variable Changing 15 main program (the caller) no changing of globals allowed : return total price total Price calculateTax() p tax_ rate total price = 10000 price 10000 When a change is tried, a local variable is made instead

16  Python allows global variables to be changed if they are labelled as global in the function.  Avoid this kind of programming. It makes programs hard to understand. Global Variable can be Changed! 16

17 CalcTaxGlobalChg-6.py 17 price is a global variable defined in the main program price is labelled as global in the function. It can now be changed.

18 Global Variable Changing 18 main program (the caller) changing of price allowed : return total price total Price calculateTax() p tax_ rate total global price price = 10000 No local variable created Bad style – avoid this kind of programming

19 5. Silly Sentences 19

20 6. Caesar Cypher secret messages are so cool!  vhfuhw phvvdjhv duh vr frro!

21 # apply Caesar cipher and return the result def encrypt(message): return shiftLetters(message, 3) # undo the Caesar cipher and return the result def decrypt(message): return shiftLetters(message, -3) : caesar.py 21

22 # shift every letter in the word by the given number of places, # wrapping around as necessary def shiftLetters(message, shift): message = message.lower() secret = "" for c in message: if c in "abcdefghijklmnopqrstuvwxyz": num = ord(c) num += shift if num > ord("z"): # wrap if necessary num -= 26 elif num < ord("a"): num += 26 secret = secret + chr(num) else: # don't modify any non-letters in the message; # just add them as-is secret = secret + c return secret : 22

23 # main msg = input("Your message to encode? ") if len(msg) > 0: # wants to encrypt secret = encrypt(msg) print("The encoded message is:", secret) else: # empty message; wants to decrypt secret = input("Your message to decode? ") if len(secret) > 0: msg = decrypt(secret) print("The decoded message is:", msg) 23

24  The Caesar cipher can be written on two rings.  By moving one ring, you can change the code. Varying the Cipher 24 Now 'A' --> 'N', 'B' -->'O', etc.

25 Draw a Smiley (bad coding style) 25 DrawSmiley1.py

26  It is bad style because it is very long, with no comments to explain it.  Also, a big function should be split into smaller functions.  A BIG problem is easier to solve if it is split into smaller problems. Horrible, but it Works... 26

27  A drawing helps: Lots of Magic Numbers in the Code 27 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.

28 Draw a Smiley (better style) 28 Easier to understand Easier to change Use comments and functions DrawSmiley2.py

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

30 Execution 30

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

32 ShapesInput.py with a Function 32

33 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) : 9. Spirograph.py 33

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

35 Execution 35

36 10. Turtles Walking About 36

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

38 11. Draw Multicolored Hearts 38

39 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)]) Hearts.py 39 # main t = Turtle() hearts(t)

40 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.circle(radius, 180 + angle) # arc ttl.forward(size) # line ttl.left(180 - angle) 40

41  Func.py contains graphing functions built using the turtle module.  The main code shows how to draw graphs for:  sine()  cosine()  a function you define; in this case y = x 2 - 4 12. Use turtle for Graphs 41

42 42 Execution


Download ppt "8. Functions 1 Let’s Learn Saenthong School, January – February 2016 Teacher: Aj. Andrew Davison, CoE, PSU Hat Yai Campus"

Similar presentations


Ads by Google