How to pass optional parameters to a function in Python?
In Python, when we define functions with default values for certain parameters, it is said to have its arguments set as an option for the user. Users can either pass their values or can pretend the function to use theirs default values which are specified.
In this way, the user can call the function by either passing those optional parameters or just passing the required parameters.
There are two main ways to pass optional parameters in python
- Without using keyword arguments.
- By using keyword arguments.
Passing without using keyword arguments
Some main point to be taken care while passing without using keyword arguments is :
- The order of parameters should be maintained i.e. the order in which parameters are defined in function should be maintained while calling the function.
- The values for the non-optional parameters should be passed otherwise it will throw an error.
- The value of the default arguments can be either passed or ignored.
Below are some codes which explain this concept.
Example 2: we can also pass strings.
Passing with keyword arguments
When functions are defined then the parameters are written in the form “datatype keyword-name”. So python provides a mechanism to call the function using the keyword name for passing the values. This helps the programmer by relieving them not to learn the sequence or the order in which the parameters are to be passed.
Some important points we need to remember are as follows:
- In this case, we are not required to maintain the order of passing the values.
- There should be no difference between the passed and declared keyword names.
Below is the code for its implementation.
As we can see that we don’t require any order to be maintained in the above example. Also, we can see that when we try to pass only the optional parameters then it raises an error. This happens since optional parameters can be omitted as they have a default with them, but we cannot omit required parameters (string1 in the above case.) Hence, it shows an error with the flag: “missing 1 required argument”.
This example will give a more insight idea of above topic:
first call 2 type is <class 'int'> z type is <class 'str'> 2.0 type is <class 'float'> second call 2 type is <class 'int'> 1 type is <class 'int'> geeks type is <class 'str'> third call geeks type is <class 'str'> 3 type is <class 'int'> 2 type is <class 'int'>
So basically python functional calls checks only if the required number of functional parameters are passed or not.
Below shows the case where a user tries to pass arguments in both ways discussed above along with the precaution given:
first call first parameter is smaller second call second parameter is smaller third call first parameter is smaller fourth call first parameter is smaller
So one thing we should remember that the keyword argument should used after all positional arguments are passed. Hence this is an important thing we must keep in mind while passing parameters in both ways to same function.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course