Python allows function arguments to have default values. If the function is called without the argument, the argument gets its default value.
Python has a different way of representing syntax and default values for function arguments. Default values indicate that the function argument will take that value if no argument value is passed during function call. The default value is assigned by using assignment(=) operator of the form
Let’s understand this through a function student. The function student contains 3-arguments out of which 2 arguments are assigned with default values. So, function student accept one required argument (firstname) and rest two arguments are optional.
We need to keep the following points in mind while calling functions:
- In case of passing keyword argument, order of arguments is not important.
- There should be only one value for one parameter.
- The passed keyword name should match with the actual keyword name.
- In case of calling function containing non-keyword arguments, order is important.
Example #1: Calling functions without keyword arguments
John Mark studies in Fifth Standard John Gates studies in Seventh Standard John Gates studies in Fifth Standard John Seventh studies in Fifth Standard
In the first call, there is only one required argument and the rest arguments use the default values. In the second call, lastname and standard arguments value is replaced from default value to new passing value. We can see order of arguments is important from 2nd, 3rd, and 4th call of function.
Example #2: Calling functions with keyword arguments
John Mark studies in Fifth Standard John Mark studies in Seventh Standard John Gates studies in Fifth Standard
In the first call, there is only one required keyword argument. In the second call, one is required argument and one is optional(standard), whose value get replaced from default to new passing value. In the third call, we can see that order in keyword argument is not important.
Example #3: Some Invalid function calls
Above code will throw error because –
- In the first call, value is not passed for parameter firstname which is the required parameter.
- In the second call, there is non-keyword argument after a keyword argument.
- In the third call, the passing keyword argument is not matched with the actual keyword name arguments.
- Packing and Unpacking Arguments in Python
- Python | Passing dictionary as keyword arguments
- Python | Set 6 (Command Line and Variable Arguments)
- NLP | Part of Speech - Default Tagging
- Python | Convert list to Python array
- Python | Index of Non-Zero elements in Python list
- Reading Python File-Like Objects from C | Python
- Python | Merge Python key values to list
- Important differences between Python 2.x and Python 3.x with examples
- Python | Add Logging to Python Libraries
- Python | Add Logging to a Python Script
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Visualizing O(n) using Python
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.