# Function in Notepad def drawSquare(myTurtle): myTurtle.forward(100) myTurtle.right(90) # side 1 myTurtle.forward(100) myTurtle.right(90) # side 2 myTurtle.forward(100)

## Presentation on theme: "Function in Notepad def drawSquare(myTurtle): myTurtle.forward(100) myTurtle.right(90) # side 1 myTurtle.forward(100) myTurtle.right(90) # side 2 myTurtle.forward(100)"— Presentation transcript:

Function in Notepad def drawSquare(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

Saved Function in C:\100 1. Open a Notepad, and enter function statements 2. Save the file with.py extension Save it as drawS.py in C:\100 3. Open a 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 why use drawS.drawSquare()  Bring Python to C:\100 folder where drawS.py is  Import drawS  Within drawS.py, execute drawSqure() function drawS.py has ‘myTurtle’ But why use drawS.drawSquare(myT) def drawSquare(myTurtle): myTurtle.forward() myTurtle.right(90) … drawS.py

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) … >>> myT=turtle.Turtle() >>> myT.forward(100) >>> myT.right(90) >>> myT…. >>> myT=turtle.Turtle() >>> drawS.drawSquare(myT) =

What if we want a function drawS() to draw a square of different sizes ? From To def drawSquare(myTurtle, sideLength): myTurtle.forward(sideLength) myTurtle.right(90) … def drawSquare(myTurtle): myTurtle.forward(100) 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

>>> import turtle >>> tt = turtle.Turtle() >>> tt.width(5) >>> tt.color(“blue”) >>> tt.circle(100) >>> tt.up() >>> tt.goto(150,0) >>> tt.down() >>> tt.color(“black“) >>> tt.circle(100) >>> tt.up() >>> tt.goto(300,0) >>> tt.down() >>> tt.color(“red“) ….. LAB: Can we make a function for Olympic rings?

Similar presentations