160 likes | 239 Views
Draw a Square. >>> import turtle >>> myTurtle = turtle.Turtle () >>> myTurtle.forward (100) >>> myTurtle.right (90) # side 1 >>> myTurtle.forward (100) >>> myTurtle.right (90) # side 2 >>> myTurtle.forward (100) >>> myTurtle.right (90) # side 3 >>> myTurtle.forward (100)
E N D
Draw a Square >>> import turtle >>> myTurtle = turtle.Turtle() >>> myTurtle.forward(100) >>> myTurtle.right(90) # side 1 >>> myTurtle.forward(100) >>> myTurtle.right(90) # side 2 >>> myTurtle.forward(100) >>> myTurtle.right(90) # side 3 >>> myTurtle.forward(100) >>> myTurtle.right(90) # side 4
Simpler way of handling repetition ? • => Functions • Abstraction • Black Box • Container for a sequence of actions • Use the function by name
Defining Functions • Name • Parameters • Body
Listing 1.1 deffunctionName(param1,param2,...): statement1 statement2 ...
Listing 1.2 defdrawSquare(myTurtle): myTurtle.forward(100) myTurtle.right(90) # side 1 myTurtle.forward(100) myTurtle.right(90) # side 2 myTurtle.forward(100) myTurtle.right(90) # side 3 myTurtle.forward(100) myTurtle.right(90) # side 4
How to Use a Function 1 • Create a new folder C:\100 • Save the function into a file with .pyextension • Such a file is called a module, script, function • Open a notepad (Start>Accessories>Notepad) • Right click on notepad -> Select Send to Desktop • Type in Python statements • Save it as drawS.py in C:\100
How to Use a Function 2 • Open a Command Window (Start>Accessories) • Right click on Command Prompt to Desktop • In the Command Window • “cd ../../100” to bring the system to C:\100 folder • “python” • “>>> import turtle” • “>>> myT = turtle.Turtle()” • “>>> import drawS” • “>>> drawS.drawSquare(myT, 100)”
Questions • File name is drawS.py • But we use ‘drawS.drawSquare() • Parameter name is myTurtle • But we use drawS.drawSquare(myT) drawS.py def drawSquare(myTurtle): myTurtle.forward() myTurtle.right(90) …
Arguments are placeholders • Think of an argument as a placeholder • It takes the place of the actual input object • Only when a function is executed, the input object takes actual values replacing the argument def drawSquare(myTurtle): myTurtle.forward(100) myTurtle.right(90) …
What if we want a function drawS() to draw a square of different sizes ? • From • To def drawSquare(myTurtle): myTurtle.forward(100) myTurtle.right(90) … def drawSquare(myTurtle, sideLength): myTurtle.forward(sideLength) myTurtle.right(90) …
An imaginary wedding computer def marry(husband, wife): sayVows(husband) sayVows(wife) pronounce(husband, wife) kiss(husband, wife) def sayVows(speaker): print “I, “ + speaker + “ blah blah” def pronounce(man, woman): print “I now pronounce you…” def kiss(p1, p2): if p1 == p2: print “narcissism!” if p1 <> p2: print p1 + “ kisses “ + p2 So, how do we marry Ben and Jo ?
An imaginary wedding computer def marry(husband, wife): sayVows(husband) sayVows(wife) pronounce(husband, wife) kiss(husband, wife) def sayVows(speaker): print “I, “ + speaker + “ blah blah” def pronounce(man, woman): print “I now pronounce you…” def kiss(p1, p2): if p1 == p2: print “narcissism!” if p1 <> p2: print p1 + “ kisses “ + p2
An imaginary wedding computer def marry(husband, wife): sayVows(husband) sayVows(wife) pronounce(husband, wife) kiss(husband, wife) def sayVows(speaker): print “I, “ + speaker + “ blah blah” def pronounce(man, woman): print “I now pronounce you…” def kiss(p1, p2): if p1 == p2: print “narcissism!” if p1 <> p2: print p1 + “ kisses “ + p2