Now a function can read a variable from the main program inside a function, but it cannot write to that variable. This can cause some confusion, and rightly so. So lets not do that.
Instead, we can change how a function behaves by passing values to a function. These values are called parameters.
An argument is another term you might hear. Here is how they relate to a parameter. A Parameter is on the receiving end, and defined in the function definition. Arguments are passed to the function. So they are essentially the same thing, just from different perspectives.
Let’s look at a simple function which takes a parameter.
def squareIt(number) :
answer = number ** 2
print(answer)
This allows us to square, not just a number, but any number. This is a whole lot easier than writing every possible number in a function to square.
When we call a function, we can pass that argument as either a literal value, or as a variable itself.
squareIt(5)
demo = 10
squareIt(demo)
You will notice in both the function header, as well as the function call, the value is within the parentheses.
Variable Scope
As was mentioned earlier, you can read data outside of your function, from your main application, but that isn’t advised.
Typically, a variable is only available in the code block in which it was created. This means two functions cannot share data directly, which is why we need to be able to pass data as variables to one another. It also means two variables can have the same name in a function, and not interfere with each other.
Global Variables
Understand that using global variables will make me cry – so try not to. With that being said…
Remember how we said that a function can see a variable created in the main portion of a program, but cannot write to it? Well a global variable gets around that.
A global variable allows you to access the main variable in another function. To do so, you have to have a variable outside the application, and then specify the keyword global on the variable.
Doing so, can cause all types of issues however, for example trying to debug an issue when you aren’t sure what function modified a value, or having a value change that you weren’t expecting.
So try to avoid global variables if at all possible.
number = 5
print_number()
def print_number() :
global number
print(number)
Global Constants
While you should try to avoid global variables, global constants are recommended. This way, the value is constant throughout the whole program. (Remember, constants cannot change.)
Now technically Python doesn’t have global constants, however, you can simulate them, by specifying all of your constants, and then putting them in the top of your program outside of any function. Python can read those values, and not change them, making them essentially a constant value.
Multiple Parameters
If you want to work with multiple parameters, that is fine. You can pass multiple parameters to a function. In the function header, in the parameter list, enter your parameters, separated by a comma.
Let’s look at an example, where we want to print a person’s name in the form of last, first.
firstName = input("Enter your first name")
lastName = input("Enter your last name")
swapNames(firstName, lastName)
def swapNames(first, last) :
print(last + ", " + first)
Notice, that our parameters do not need to have the same name as our argument variable names. You should also notice, that by default, your parameters and argument variables will match up in the order submitted. Which variable they match with, will be based upon the parameter list that is defined in the function header.
Keyword Arguments
In most programming languages, you have to pass your arguments in a set order, based upon the parameters as defined by the function definition and header.
However, Python allows you to reorder the arguments, if you specify the parameter name. Let’s look at an example from our last function, where we pass the first name, then the last name. However, we are going to pass the last name first, then the first name.
firstName = input("Enter your first name")
lastName = input("Enter your last name")
swapNames(last = lastName, first = firstName)
def swapNames(first, last) :
print(last + ", " + first)
You are technically allowed to mix positional arguments (those passed in the order they are defined, with keyword arguments. While I would strongly not recommend this, if you do, you have to list your positional arguments first, then they keyword arguments.
Passing Values to Functions was originally found on Access 2 Learn