More on Parameters in Python

Not long ago I posted an article explaining the importance of parameters in programming. The article was based on the Java programming language, and although the concepts were explained as abstractly as possible, I decided to dig in my vault for some more explanations put in the context of another language; Python.

I found a post I used for a school assignment back when I was learning Python. I think it helps to demonstrate how the abstract concepts carry over from one language to another. This post also looks at parameters from a different angle, so I thought it would be helpful for anyone still struggling on the use of them.

Before reading this post you should have a basic understanding of what a subroutine or function is, and know what scope means. You should also have a basic understanding of Python.

What is a parameter?

In the context of Python, a parameter is a variable that is used to store the value of an argument that will be passed to a function from a function call. Parameters are local to the function they are defined in.

Parameters are useful because they allow us to pass information to functions, which can change the way the function reacts in a program depending on the value of the argument(s).

How do I use them?

As an example I might have a function that checks to see if two numbers are the same. Because I’m comparing two numbers I probably want my function to have two parameters, through which I can pass the numbers I want compared.

Perhaps it looks like this:

def isEqual(num1, num2):
   if (num1 == num2):
      return true
       return false

Note: Although the above example may be easier to read for a beginner, the if statement is redundant. It’s much cleaner to use the following code:

def isEqual(num1, num2):
    return num1 == num2 # returns true if equal, false if not

Depending on the values I pass to this function as arguments, I will get different results returned to me. If the two numbers are equal I will get a value of true, otherwise it’s false.

I might make a call to the function like this:

x = raw_input()
y = raw_input()

Here I get two numbers from the user and store them inside of x and y. Then I pass my isEqual() function as an argument to the print() function. isEqual() is passed x and y as arguments, and returns true or false, which is passed to the print() function, which then prints the result on the screen.

If that example is difficult to follow or hard to read, another way of doing the same thing would be this:

x = raw_input()
y = raw_input()
result = isEqual(x,y)

This does the same thing as the previous example, only we pass the returned value of isEqual() into a variable called result. Then we pass result as an argument to print(). The ending result is the value returned by isEqual() being printed to the user’s console.

How else can parameters be useful?

There are many more situations were parameters should be used in functions. Maybe I want to compare the position of two objects in a game. If they are overlapping, I know a collision has happened and I want to do something. If they are not, than no collision has happened and I may want to do something else.

The way I tell my function which objects to check for collision on is by passing variables which store the position of those objects as arguments to the function. Or I could even pass the objects as arguments.

Imagine if the print() function had no parameters. How would you tell the function what you want to print? Parameters allow us to create functions that perform different tasks or return various results depending on different circumstances. They give us a way to tell functions what to do.

Life would be bad without parameters.

Without parameters it would be much more difficult and “buggy” to make functions dynamic and flexible. If there were no parameters the only way to influence the behavior of a function would be to have it perform tasks on globally scoped variables.

This is buggy, because when all of your variables are global, it makes it too easy for another function to change the value of those variables, when it’s not supposed to.

The main roll parameters play.

One roll functions play is to help us keep variables local to the blocks of code that need them, and inaccessible to the blocks of code that don’t. In this way, the use of parameters is how we can control the flow of information between our functions and other parts of our program.

They offer a way to expose information that is otherwise hidden from the function’s scope, but only when we need them to be. With that in mind parameters can be thought of as messengers.

To those who read the last article on parameters, I hope this helped add some more context to the ideas. If you haven’t read it, I suggest you do as it explains parameters from a very different angle. You can find it here. I think the best way to learn something new is to have it explained in as many ways as possible.

Be sure to leave a comment bellow. Let me know if I can make this article better. Or if you have any questions, ask them. I love helping people.



Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Show Buttons
Hide Buttons
%d bloggers like this: