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 | Set 6 (Command Line and Variable Arguments)
- Python | Passing dictionary as keyword arguments
- Python: Passing Dictionary as Arguments to Function
- Command Line Arguments in Python
- Tuple as function arguments in Python
- Python - pass multiple arguments to map function
- Executing functions with multiple arguments at a terminal in Python
- How to handle invalid arguments with argparse in Python?
- How to find the number of arguments in a Python function?
- Unpacking arguments in Python
- Python IMDbPY – Default info of Company object
- Python IMDbPY – Default info of Movie object
- Python IMDbPY – Default info of Person object
- Python - Alternate Default Key Value
- How to pass multiple arguments to function ?
- Core arguments in serializer fields - Django REST Framework
- Functions that accept variable length key value pair as arguments
- NLP | Part of Speech - Default Tagging
- default - Django Built-in Field Validation
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. 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.