[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Function and turtle help

On 02/21/2014 10:38 PM, Scott W Dunning wrote:
> On Feb 21, 2014, at 7:13 PM, Dave Angel <davea at davea.name> wrote:
>> Scott W Dunning <swdunning at cox.net> Wrote in message:
>>> On Feb 20, 2014, at 11:30 PM, Dave Angel <davea at davea.name> wrote:
>>>> Look at turtle.begin_fill and turtle.end_fill
>>>> That's after making sure your star is a closed shape.
>>> So, this is what I have so far and it ?works? but, it fills in the star with black and as you can see below I am trying to fill it in with red.  So I?m obviously messing up somewhere.
>>> def fillcolor(red):
>>>     pencolor()
>>>     begin_fill()
>>>     star(500)
>>>     end_fill()
>>> red = pencolor
>>> fillcolor(red)
>> You seem to have a fundamental misunderstanding of how variables
>> and function parameters work.
> Yes I just learned a little bit about functions a couple days ago in school.  I am completely new to programming so thanks for any help!
>>> def fillcolor(red):
>>    The parameter should be something like color, not red.
> Ok I see.
> I changed it a little based on what you suggested and I got a change, the outline became red but the fill is still black.  Can you walk me through where I?m going wrong on what I have below?

I'll try.  First, a web page from Python's library reference


This assumes Python 3.3, but you didn't say.  If you're using 2.x, then 
change the 3.3 in the link to 2

Notice that I've never actually used the turtle module, and I don't 
believe I've even looked at the docs till responding to your questions. 
  So I may easily make errors in the API.

>  From what I gather, I have a function called fillcolor with a parameter called color.  I know this is going to sound stupid
> but I get a little lost after that.  The function says when ever I call fillcolor do what?s in the body?

That's right.  When you call a function, it causes the body to be 
executed, after filling in a value for the parameters if any.

> What exactly does the parameter do (in this case color, how is that doing anything) and what is an argument?

The argument was the top-level variable red, which has the value of 
string "red".  So when the function starts, the parameter color is equal 
to the string "red"

That doesn't do anything till you use the parameter color (which is a 
local variable).  You used it as a parameter in the call to pencolor(), 
and if you also used in the call to fillcolor(), you'd get the effect 
you want.

> def fillcolor(color):    <- Function with parameter, right?
>      pencolor(color)     <- Says make pencolor whatever I state when I call fill color, right?  	
>      begin_fill()	      <- starts filling the star.  When I put color in () though I get an error that it takes no arguments?
>      star(500)	      <- Star function
>      end_fill()	      <- Ends fill
> red = "red"
> fillcolor(red)
> I know that I know absolutely nothing but I?m trying and I just started so please bare with me.  And thanks for any help, it's GREATLY appreciated!!
> Scott

The remaining problem is that after doing the
     from turtle import *

(which I generally avoid doing), you then define your own function 
fillcolor(), which masks the one you just imported.

So as a minimum change, I might pick a new function name:

def filledpolygon(size, color):    <- Function with parameter, right?

red = "red"
filledpolygon(500, red)

Some terminology:

def myfunc(parm1, parm2):
     q = 3*4
     otherfunc(parm1, q)
     return 12

arg = 42
res = myfunc(arg, 99)

Top level varaibles are arg and res.  When myfunc is called, it is 
passed two "arguments", arg and 99.

When myfunc actually starts running, it has 3 local variables.  The 
first two, called parameters, are set from the arguments above, arg and 
99.  In effect, the function begins:
       parm1 = arg
       parm2 = 99

The other local variable is q, and it's set right away.

Then two other functions are called, using these local variables are 
arguments.  We're not going to study them, just assume they're there.

Now a value of 12 is returned.  That value will end up in res, as we 
turn control back to the top-level code.

Most of the functions a beginner calls are already written for him, so 
he just worries about the arguments to those functions.  But it's still 
important to have the right number and types of arguments for whatever 
those functions are expecting.

Obviously it gets trickier to explain when you are calling your own 
function, so you have to worry about caller and callee.

I hope this helps some.