1-10 Passing Data to Functions

Passing One Argument

It’s often useful to be able to pass one or more pieces of data into a function.  An argument is any piece of data that is passed into a function when the function is called.  For example, when you call the int() function, the value or variable that you put within the parentheses are called arguments.

For a function to be able to receive one or more arguments, you must specify one or more parameter variables in the function header.  A parameter is a variable that is assigned the value of an argument when a function is called. The scope of a parameter variable is the entire body of the function in which it is defined, and no statement outside the function can access it.  Here’s an example:

When this code is run, the main() function creates a local variable named value and assigns it a value of 5.  Next, value is passed as an argument when the show_double() function is called.  When the show_double() function executes, the number parameter will be assigned a copy of the value referred to by the value variable.  It’s important to realize that value and number now refer to two different copies of the same value.

The show_double() function creates its own local variable called result and assigns it to double the value referred to by number.  The result is then displayed and the show_double() function returns.

What do you think would happen if we tried to change the value that the number parameter references, like so:


Well, any change to the number parameter variable will not affect the original value argument.

This form of argument passing, where a function cannot change the original value referred to by an argument is called pass-by-value.  Often this is also referred to as pass-by-copy since we are making a copy of any value passed into a function.  You can think of pass-by-value as a one-way communication channel between functions.

Passing Multiple Arguments

To pass more than one argument to a function, simply separate each with a comma.  Similarly, in the function header, include a comma-separated list of parameter variables.  For example:

Style Rule #6 (REVISED!): Function Commenting

Now that you know how to pass arguments to a function, we need to improve how we document our functions. In addition to every function having a docstring that explains its purpose, you should also clearly state what parameters are expected by the function, and their purpose — as shown in the example above.  This way other programmers using your function won’t make mistakes calling it.

Finally, note that when you have more than one parameter for a function the quantity, data types, and order of the parameters is very important! For example, these are incorrect ways to call the birthday() function:

Global Variables = Bad Design

When an assignment statement outside all the functions in a program creates a variable, it is called a global variable.  A global variable can be accessed by any statement in any function of a program.  The following example illustrates this idea:


However, for safety (by default) Python does not allow you to change global variables within a function, they are read-only. For example, line 4 below would cause an error:


An additional step is required if you want a statement in a function to change the value referenced by a global variable.  In the function you must declare the global variable using the global keyword (line 5 below):


Although this works, professional programmers avoid using global variables because they make programs harder to understand and debug.  For example, if a function relies on a global variable and you want to use the function in another program, then you can’t cut & paste the function without also copying the global variable.  This really hinders code reuse!  In most cases, any external data that a function requires should be passed to it as an argument.  Global variables generally lead to poor design.

Global Constants

In rare cases, global constants can be useful in a program.  For example, you might have a global constant to refer to the frame rate in a game that all functions refer to.  A global constant is essentially a global variable, but one where you never attempt to change its initial value.  That is, you never declare it as global in a function.

In Python, a global constant is created the same way as a global variable, but one should use all UPPERCASE letters to indicate to others reading your code that the value referenced by the variable should not be changed during program execution.

Style Rule #8: Global Constants

While global variables should be avoided, global constants are sometimes useful in a program. It is good style to write a constant’s name in all UPPERCASE letters. This serves as a reminder to other programmers that the value referenced by the name is not to be changed in the program.

You Try!

    1. Add the heading “1-10 Passing Data to Functions” to your learning journal and answer the following questions:
      1. What is the difference between an argument and a parameter?
      2. What is the scope of a parameter variable?  If a parameter value is changed within a function, does this affect the argument that was passed into the function?  Explain why or why not.
      3. What is the scope of a global variable? How can a function change the value of a global variable?
      4. Why should global variables be avoided?
    2. Without running this program, what should it display?  Now, run it and check if you are right.