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
- Command Line Arguments in Python
- Python | Passing dictionary as keyword arguments
- Python | Set 6 (Command Line and Variable Arguments)
- How to pass multiple arguments to function ?
- NLP | Part of Speech - Default Tagging
- default - Django Built-in Field Validation
- Python | Merge Python key values to list
- Reading Python File-Like Objects from C | Python
- Important differences between Python 2.x and Python 3.x with examples
- Python | Index of Non-Zero elements in Python list
- Python | Convert list to Python array
- Python | Set 4 (Dictionary, Keywords in Python)
- Python | Sort Python Dictionaries by Key or Value
- Python | Add Logging to a Python Script
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.