Default Argument in Python
Default arguments in Python can be used with two types of arguments:
- With Keyword Arguments
- Without Keyword Arguments
Default Argument with Keyword Argument:
Here is the implementation of default argument with keyword:
def student(firstname, lastname ='Mark', standard ='Fifth'):
print(firstname, lastname, 'studies in', standard, 'Standard')
# 1 keyword argument
student(firstname ='John')
# 2 keyword arguments
student(firstname ='John', standard ='Seventh')
# 2 keyword arguments
student(lastname ='Gates', firstname ='John')
Output
John Mark studies in Fifth Standard John Mark studies in Seventh Standard John Gates studies in Fifth Standard
Default Argument without Keyword Argument:
Here is the implementation of default argument without keyword:
def student(firstname, lastname ='Mark', standard ='Fifth'):
print(firstname, lastname, 'studies in', standard, 'Standard')
# 1 positional argument
student('John')
# 3 positional arguments
student('John', 'Gates', 'Seventh')
# 2 positional arguments
student('John', 'Gates')
student('John', 'Seventh')
Output
John Mark studies in Fifth Standard John Gates studies in Seventh Standard John Gates studies in Fifth Standard John Seventh studies in Fifth Standard
Default Argument in Programming
Default arguments are one of the powerful features in programming that allows developers to specify a default value for one or more parameters in a function and provides flexibility as it enables functions to be called with different numbers of arguments. When calling a function, the programmer can omit some of the arguments for parameters that have default values, and the function will use those default values instead which makes functions more versatile and user-friendly. Languages like C++, Python, etc. allow the programmer to specify default arguments that always have a value, even if one is not specified when calling the function.
Table of Content
- What are Default Arguments?
- Syntax of Default Argument
- Default Argument in C++
- Default Argument in Python
- Default Argument in Javascript
- Default Argument in Java
- Advantages of Default Argument
- Disadvantages of Default Argument