Sunday, May 16, 2010

Creating Functions with Parameters

In my last post, you might have noticed something a little different about one of my functions.  It had what’s called a “parameter.”  Parameter’s aren’t totally new for you, even if you don’t realize it.  When you use the code:

>>> print(“Hello World!”)
Hello World!

You are using a parameter.  You see, the function called “print” requires a parameter.  You need to give it some sort of value to print out.  If you don’t, how will it know what to print?  Let’s create a simple function that requires a parameter.  As you know, let’s open up IDLE and create a new window:

def dogYears(age):
    dogAge = age * 7
    print(“You are %d years old in dog years!” %dogAge)

Notice how the function declaration has the word “age” inside the parenthesis.  This is a variable that the user must supply in order to call on the function.  Whatever value the user passes in is treated as age.  Let’s try it in the Python shell, remember to save and run your program first:

  1. >>> adamsAge = 23
  2. >>> dogYears(adamsAge)
  3. You are 161 years old in dog years!

Notice how the variable I passed in is called “adamsAge” yet the function uses a variable called “age.”  This concept can be surprisingly difficult for people new to programming, but it’s really not hard. 

inside the dogyears function, the parameter passed is treated as “age.”  The original name of the variable is irrelevant once its passed into the dogYears function.  Outside the function, the variable age doesn’t exist.  Remember, this is a part of what is called scope.

There’s another very nice technique that python allows us to use that can be very helpful.

def congratulate(firstName = “John”, lastName = “Doe”):
    print(“Congratulations %s %s!” %(firstName,lastName))

Notice this time, I assign firstName and lastName values in the function declaration.  Think of these as default values.  Let’s test them out in the python shell:

  1. >>> congratulate(“Adam”, “Smith”)
  2. Congratulations Adam Smith!
  3. >>>
  4. >>> congratulate()
  5. Congratulations John Doe!
  6. >>>
  7. >>> name = “Danny”
  8. >>> congratulate(name)
  9. Congratulations Danny Doe!

In line 1, we see the code working just like any other function.  In line 4, we don’t pass any arguments.  Because we created default values, the function used the default values we provided.  Finally, in line 8 we pass in one parameter.  As a result, the function uses the parameter we passed in as the first parameter in the function.  Because no other values were passed in, the function uses the default values in their place.

You may be wondering, “Why am I using parameters when I could just use input?”  In bigger programs, you won’t want to stop the program and ask the user for more information all the time.  That would be annoying and no one would want to use your programs.

To test your knowledge, I’m giving you a little assignment.  I'm sure you’ve heard the expression “money is the root of all evil.”  I want you to build a program that will display this.

This should be your function declaration:

def rootFinder(value1, value2):

and it should print a string with the following:
value1 is the root of all value2.

for example if we ran it in the python shell:

  1. >>>rootFinder(“dough”,”cookies”)
  2. dough is the root of all cookies.

Feel free to post your results in the comments section or to e-mail me with them.  If you have any questions, you can post them as well!

2 comments:

  1. >>> def rootFinder(value1,value2):
    print("%s is the root of all %s" %(value1,value2))
    return

    >>> rootFinder("python","happiness")
    python is the root of all happiness

    ReplyDelete
  2. That's great! You actually don't need the return statement at the bottom, it'll work fine without it. I reworded the assignment because I realized I hadn't gone over the return statement when I wrote this article. That one was my bad. Based on what I was looking for, and what someone reading this blog should know by this point, this is pretty perfect.

    ReplyDelete