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!”)
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:
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:
- >>> adamsAge = 23
- >>> dogYears(adamsAge)
- 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:
- >>> congratulate(“Adam”, “Smith”)
- Congratulations Adam Smith!
- >>> congratulate()
- Congratulations John Doe!
- >>> name = “Danny”
- >>> congratulate(name)
- 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:
- 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!