Python – Function Parameters
Function parameters in Python is defined as part of the function header. In last article, we went through what is functions in Python?. Before going further, lets see what is difference between parameter and argument.
- A parameter is a variable defined as part of the function header and is used to make data available within the function itself
- An argument is the actual value or data passed into the function when it is called. The data will be held within the parameters
Python provides different types of parameters to be passed in functions, lets see each type of function parameters in Python.
- Multiple Parameter Functions
- Default Parameter Values
- Named Arguments
- Arbitrary Arguments
- Positional and Keyword Arguments
Multiple Parameter Functions
So far we had seen zero parameter or one parameter, but we can even have two or more parameters in function based on need of the function. In such case, the parameters in the parameter list should be separated with comma. For example,
def generate_message(name, operation, status): return name+' made '+operation+' with status '+status print(generate_message('John', 'add user', 'success')) print(generate_message('Tim', 'authentication', 'success')) print(generate_message('John', 'delete user', 'failure')) print(generate_message('Tim', 'list user', 'success'))
Above code snippet will give output as below,
John made add user with status success Tim made authentication with status success John made delete user with status failure Tim made list user with status success
Default Parameter Values
Once you have one or more parameters you may want to provide default values for some or all of those parameters; particular for ones which might not be used in most cases.
This can be done very easily in Python; all that is required is that the default value must be declared in the function header along with the parameter name.
If a value is supplied for the parameter, then it will override the default. If no value is supplied when the function is called, then the default will be used.
For example,
def generate_message(name, operation, status='not captured'): return name+' made '+operation+' with status '+status print(generate_message('John', 'add user')) print(generate_message('Tim', 'authentication', 'success')) print(generate_message('John', 'delete user', 'failure')) print(generate_message('Tim', 'list user'))
Above code snippet will give output as below,
John made add user with status not captured Tim made authentication with status success John made delete user with status failure Tim made list user with status not captured
- name – mandatory field/parameter
- operation – mandatory field/parameter
- status – an optional field/parameter (as it has a default value)
Important point – when one parameter has a default value all remaining parameters to the right of that parameter must also have default values.
For example, we could not define the generate_message function as below,
def generate_message(status='not captured', name, operation): return name+' made '+operation+' with status '+status
Executing above code snippet will give error as below,
def generate_message(status='not captured', name, operation): ^ SyntaxError: non-default argument follows default argument
Named Arguments
With default parameter values, consider a function which is going to have one or more default parameter values as below,
def generate_message(name, operation='user operation', status='not required to be captured'): return name+' made '+operation+' with status '+status
Using above function, if we want to call method passing only below scenarios,
- name and operation as argument
- name and status as argument
The answer is to use named arguments (or keyword arguments). In this approach we provide the name of the parameter we want an argument/value to be assigned to; position is no longer relevant. For example:
def generate_message(name, operation='user operation', status='as optional'): return name+' made '+operation+' with status '+status print(generate_message('John', status='success')) print(generate_message('Tim', operation='authentication')) print(generate_message('John', 'delete user', 'failure')) print(generate_message('Tim'))
Above code snippet will give output as below,
John made user operation with status success Tim made authentication with status as optional John made delete user with status failure Tim made user operation with status as optional
Arbitrary Arguments
In some cases, you do not know how many arguments will be supplied when a function is called. Python allows you to pass an arbitrary number of arguments into a function and then process those arguments inside the function.
To define a parameter list as being of arbitrary length, a parameter is marked with an asterisk (*). For example:
def greeter(*args): for name in args: print('Welcome', name) greeter('John', 'Denise', 'Phoebe', 'Adam', 'Gryff', 'Jasmine')
Above code snippet will give output as below,
Welcome John Welcome Denise Welcome Phoebe Welcome Adam Welcome Gryff Welcome Jasmine
Positional and Keyword Argument
Some functions can either be provided using a variable number of positional or keyword arguments. Such functions have two arguments,
- *args – for positional arguments
- **kwargs – for keyword arguments
They are useful if you do not know exactly how many of either position or keyword arguments are going to be provided.
For example, the function my_function takes both a variable number of positional and keyword arguments:
def my_function(*args, **kwargs): for arg in args: print('arg:', arg) for key in kwargs.keys(): print('key:', key, 'has value: ', kwargs[key]) my_function('Tim', 'Liz', daughter='Phoebe', son='Adam') print('-' * 50) my_function('John', 'Jen', son_number_one='Andrew', son_number_two='James', daughter='Joselyn')
Above code snippet will give us output as below,
arg: Tim arg: Liz key: daughter has value: Phoebe key: son has value: Adam -------------------------------------------------- arg: John arg: Jen key: son_number_one has value: Andrew key: son_number_two has value: James key: daughter has value: Joselyn
Conclusion
In this article, we have went through how function parameters can be used and different types of function parameters in Python are available.
References
- https://docs.python.org/3/library/functions.html for a list of built-in functions in Python.
- https://www.w3schools.com/python/python_functions.asp the W3 Schools brief introduction to Python functions.