Python

Python Optional Function Arguments

While programming, you may have passed many values in the function through the parameter of a function call to a specific function. These parametric values are said to be of different types like default, optional, positional, etc. The default arguments are the values already specified in the function parameters. While the optional parameters are the values passed by the function call and the user can decide to either pass them or not. Our topic is to discuss the use of optional function arguments in Python to see what it looks like in the code and on the execution part. So, let’s start today’s article with the shell console application launch with our very own Ctrl+Alt+T. Make sure to have Python’s latest version installed already. If not, try using the demonstrated below query from the image and add your password to complete it.

Example 01:

We will start our first example of a function call with parameters in Python with the most basic illustration. In this illustration, we will be deliberating the basics of function calls and their know-how. We have opened our python new file “optional.py” using the Gnu nano editor after its creation with the Linux “touch” command.

After its opening, we have added the python-support at the first line of code “#!/usr/bin/python”. While defining functions in Python, we tend to use only the required parameters that fulfill the function execution needs. For instance, we have declared a function “sum” with two positional parameters “x” and “y” using the “Def” keyword to define it in the code. Within its implementation, we have used the “print” statement to display the sum of both “x” and “y” values passed in the parameters by the function call.

After this function definition, we have called the function by a function call passing 2 parametric values i.e., 2 and 3. The overall python code is valid because we have fulfilled the function need. It’s time to save this code and run it on the shell using python3.

After running the optional.py file on the terminal, we have got the sum “5” of values 2, and 3 passed by the function call. It shows no errors as we have not done any mistakes while coding.

Let’s see what happens when we don’t pass the obligatory number of arguments in the function call. So, we have unlocked the same code file and updated the last line of this code. We have passed a single argument value to the function “sum” by a function call without taking into consideration that this function needs 2 arguments to calculate the sum. It will throw an error at the shell while executing this code. Let’s save this code and execute it.

On running the saved code file again, we have encountered an error “TypeError: sum() missing 1 required positional argument: ‘y’ “. The same error would occur when a user doesn’t pass any argument in the function call of the “sum” function. So, try to avoid it by passing the right number of values a function required.

Example 02:

After looking at the necessary positional arguments, it’s time to look at the optional function arguments in Python now. To understand the optional arguments for functions, we have to take a good look at the default arguments first. To demonstrate the default arguments in a function, we have been opening the optional.py file within the nano editor and making the code changes. The default arguments are the ones already stated in the definition of a function. For instance, we have defined a function sum with two positional arguments “x” and “y”. The argument “y =9” is a default argument here. It’s a choice of a user to pass either a single value for argument “x” in a function call or pass 2 arguments. We have been passing a single argument value “3” in the “Sum” function call for the argument “x” and using the default value of argument y i.e., 9. Save this code and quit the file.

After the execution of this python code with 1 positional argument and 1 default argument without passing an optional argument, we have got the sum 12. This doesn’t throw an exception because the function call will use the passed argument “3” and the default value “9” of the function to create a sum.

Now, if you want to replace the default argument value defined in the function definition with a new optional argument value, you can also do that easily. For this, you need to pass the optional argument value in the function call as well. So, we have updated the same code file and used the 2 value function call now. The value “3” will be passed to argument “x” and the optional value “10” will replace the default value “9” of “y” to calculate the sum. In return, the print statement will utilize the passed value 3, and 10 to calculate the sum “13” and display it.

It’s time to run this code after saving it again. We have got the sum 13 as expected. This shows that the passed optional argument has replaced the default value in the function definition.

Example 03:

Let’s get started with the implementation of optional arguments in a function call for string type values now. Open the same file and add the python-support if you didn’t add it before. We have been declaring a function “sum” with the keyword “Def” taking two arguments in its parameters. The value “s1” will be the positional argument that must be passed by the function call and the value s2 = “ World” is the default argument. The print statement will display the concatenation of both s1 and s2 argument values. Two function calls have been made. The first is passing a single value to the s1 argument of function “sum”. In this function call, the default value “ World” for s2 will be used for concatenation. On the other hand, the next function call is passing two string values to the s1 and s2 arguments. The s1 will take the first string value passed and s2 will replace its “ World” value with the second string value passed in the function call. Let’s just save this simple code to see them working on the shell using the Ctrl+S. After that, exit this file in nano editor and go back to the shell.

On execution, the first result is displayed using the default value for argument s2. The second result has utilized the passed argument values i.e. optional value for s2 in the function call.

You can also pass the optional arguments in the function call by using the keyword arguments. You can also pass the keyword optional arguments irrespective of their position in the function definition. We have used the keyword arguments in the below-shown code.

Got quite similar output as we got in the above illustration.

Conclusion

Finally! We have explained the way to use the optional function arguments in the Python function call. For this, we have covered the examples for positional and default arguments first. After that, we have tried to use the optional arguments with the simple value passing in the function call, and the use of keywords arguments for passing the optional arguments.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.