Python Functions using Default Values
In Python programming language, Function Arguments are allowed to have default values. When we define a function, we have to provide arguments for that specific function. But, if the function is called without an argument, then the default value is assigned to that particular argument.
In Python, when we are representing syntax and default values for function arguments, Default values indicate that it is taken only when there is no argument value passed during Function call.
The default value can be assigned by using the assignment operator (=). While passing the keyword argument, the order of argument is not necessary. Only one value can be passed for each parameter. Passed keyword name should match with Actual keyword name. If we call function with non keyword arguments, order is necessary.
Let us understand this in more detail by an example.
We will define a function employee with three arguments employee_name, department, experience.
Out of the three arguments, we will assign two arguments with default values. Function will automatically assign default values for department and experience as we didn’t provide any values while calling the function.
def employee(employee_name, department='technical', experience='5'): print(employee_name, 'works in', department, 'department has', experience, 'experience') employee('siva')
siva works in technical department has 5 years experience.
Here, we will assign values for each parameter in the function.
rani works in testing department has 3 years experience.