Functions in Python are used to implement logic that you want to execute repeatedly at different places in your code. You can pass data to these functions via function arguments. In addition to passing arguments to functions via a function call, you can also set default argument values in Python functions. These default values are assigned to function arguments if you do not explicitly pass a parameter value to the given argument. Parameters are the values actually passed to function arguments. Show
In this article, you will see how to use default arguments in Python functions. But first, we will see how to define a function in Python and how to explicitly pass values to function arguments. Function without ArgumentsLet's define a very simple Python function without any arguments:
The above script defines a function, The following script
shows how you'd actually call the
In the output, you should see a simple statement printed to the screen by the
Function with Explicit ArgumentsLet's now define a simple Python function where we have to pass multiple values for the function arguments. If you do not specify values for all the function arguments, you will see an error. Here is the function we'll be using as an example:
In the code above we create a function, Let's try calling the above function with two arguments:
The above script calls the Let's now try to call the
In the output, you should see the following error:
The error is quite clear, the function call to Let's now pass a value for one of the arguments and see what happens:
Now in the output, you should again see the following error:
The difference here is that the error now tells us that the value for one of the positional arguments, i.e. What if you want your function to execute with or without the argument values in the function call? This is where default arguments in Python functions come in to play. Function with Default ArgumentsDefault arguments in Python functions are those arguments that take default values if no explicit values are passed to these arguments from the function call. Let's define a function with one default argument.
The above script defines a function Otherwise, if you do not pass any value for the Let's first call the
Output:
Check out our hands-on, practical guide to learning Git, with best-practices, industry-accepted standards, and included cheat sheet. Stop Googling Git commands and actually learn it! When you execute the above script, the value 10 overwrites the default value of 2 for the argument Now we will call the
Output:
A Python function can have multiple default arguments as well. For instance, in the following script, the function adds the integers number passed to the arguments. If none of the integer values is passed to the function, the default arguments would take values 2 and 4 respectively, as shown below:
Let's first call the
Output:
Since we did not pass any values for the function arguments, the default argument values, i.e 2 and 4, have been added together. Let's now pass two of our own values to the
Output:
As expected, 4 and 8 were added together to return 12. A Python function can have
both normal (explicit) and default arguments at the same time. Let's create a function
Let's first pass only a single argument:
Output:
In the script above, 3 has been passed as a value to
the Let's now pass two values to the
In the output, you will see 3 raised to the fourth power, i.e. 81. It's important to note that parameters with default arguments cannot be followed by parameters with no default argument. Take the following function for example:
Trying to call this function will result in an error since the first argument has a default, but the second one does not:
Executing this code results in the following error:
Can all the parameters of a function can be default parameters?C. All the parameters of a function can be default parameters.
Is it possible for a function to have some parameters?It is possible for a function to have some parameters with default arguments and some without.
Can we give all arguments as default arguments in function yes or no?You can pass data to these functions via function arguments. In addition to passing arguments to functions via a function call, you can also set default argument values in Python functions. These default values are assigned to function arguments if you do not explicitly pass a parameter value to the given argument.
Which function Cannot have default parameters?Constructors cannot have default parameters.
|