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:
1 2 3 4 5 6 7 8 9 10 11 |
def main(): '''This function defines the mainline logic for our program''' value = 5 show_double(value) def show_double(number): '''This function accepts a numeric argument and displays double its value.''' result = number * 2 print(result) main() |
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:
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:
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:
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:
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 ConstantsWhile 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!
-
- Add the heading “1-10 Passing Data to Functions” to your learning journal and answer the following questions:
- What is the difference between an argument and a parameter?
- 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.
- What is the scope of a global variable? How can a function change the value of a global variable?
- Why should global variables be avoided?
- Without running this program, what should it display? Now, run it and check if you are right.
12345678910111213def main():x = 1y = 3.4print(x, y)change_us(x, y)print(x, y)def change_us(a, b):a = 0b = 0print(a, b)main()
- Add the heading “1-10 Passing Data to Functions” to your learning journal and answer the following questions: