Learning Modules, Arguments, Parameters (imma noob)

Laura Creighton lac at openend.se
Fri Sep 25 22:15:26 CEST 2015


In a message of Fri, 25 Sep 2015 11:50:10 -0700, Cody Cox writes:
>Awesome guys! Thank you for helping me understand this material. Parameters and Arguments are tricky. Looks like its mainly a game of connect the dots with variables. lol.
>
>When you return a variable, it needs somewhere to go, and that's why it goes to the next call into the argument area if I need to use that piece of information for a calculation, right?

No.

>so if I understand correctly, when I create a function and return a variable, that variable needs to go in the argument when it is called? or into another functions argument for calculation? 
>I apologize if I sound dumb. lol. I know this must be very elementary.

No.

>Yes this is a homework assignment, but I was trying to figure out how parameters and arguments work, not get an answer, this was a huge help. I will keep looking over your details over and over to make sure I understand.
>
>Appreciate all the help and understanding! :)
>
>-Cody

You have a very basic conceptual misunderstanding.

def main():
    count = 0
    animals = 'tigers'
    change_my_value_to_50_frogs(count, animals)
    print ("I fought", count, animals)

def change_my_value_to_50_frogs(count, animals):
    count = 50
    animals = 'frogs'

main()

-------------

I am pretty sure you expect this code to print  'I fought 50 frogs'.
It doesn't.  It prints 'I fought 0 tigers'.
You have the idea that when you pass parameters to a function, by name,
you want the values of those parameters to get changed by the function.
(and I sort of cheated by calling the function change_my_value).
This is not what happens.  You cannot change values this way.

so we rewrite change_my_value_to_50_frogs

def fifty_frogs():
    count = 50
    animals = 'frogs'
    return count, animals

Now fifty_frogs is going to happily return the changed values you wanted.
But you have to set up your main program to receive those changed values
you wanted.

so we rewrite main as:

def main():
    count = 0
    animals = 'tigers'
    count, animals = fifty_frogs() 
    print ("I fought", count, animals)

now

main()

says 'I fought 50 frogs'.

NOTE: there is nothing, nothing, nothing special about the fact that
in fifty_frogs I used the names 'count' and 'animals'.

Let us try a new version of fifty_frogs()

def fifty_frogs():
    experience = 50
    monsters = 'frogs'
    return experience, monsters

All the same, just different words.
Does that matter?

No.  You are going to return whatever is called 'experience' and
whatever is called 'monsters' and assign them to 'count' and 'monsters'.

Whether you name these things the same thing, or different things
makes no difference.

Does this make sense?

If not, come back with more questions.  And play around with the
python interactive interpreter, or idle -- that is what it is for.
neat little experiements like this. :)

Laura




More information about the Python-list mailing list