So far you know that when a function is called, every argument it needs has to be given. But what happens if you define the value of those parameters when the function is created? Is that even allowed?
It is. In Python this is called a default argument.
Default Argument
Python lets you give a default value while you create a function argument. When the function is called without that argument, this value is what takes its place.
Creating a default argument works like an ordinary assignment, only the assignment sits inside the function declaration.
Here is an example of a function with default arguments:
def cetak_salam(salam='Halo', nama='Inva'):
print(salam, nama)
Because it already has default arguments, that function can be called with no arguments at all. Like this:
cetak_salam() # output: "Halo Inva"
cetak_salam('Selamat siang', 'Budi') # output: "Selamat siang Budi"
Now how do you change the nama argument without changing the salam argument? Of course that works, but it needs a keyword argument.
Keyword Argument
A keyword argument is a way to give a value to a function argument while naming that argument at the same time.
It looks like this:
cetak_salam(nama='Budi') # output: "Halo Budi"
cetak_salam(salam='Selamat datang') # output: "Selamat datang Inva"
With keyword arguments you can change only the arguments you need. Any argument you don't give a value to automatically holds the default value it was given.
Keyword arguments don't have to follow the order of the parameters. You can fill in argument values in any order, as long as the argument names exist in the function declaration.
cetak_salam(nama='Budi', salam='Selamat siang') # output: "Selamat siang Budi", out of order
cetak_salam(nama='Budi', salam='Selamat siang', hari='senin') # output: error, hari is not declared
Rules for Declaring and Filling Arguments
There are a few rules you have to follow when you build function arguments in Python, among them:
A default argument has to come after the non-default arguments
Wrong ❌
def nama_fungsi(arg1, arg2=128, arg3=64, arg4)
...
Right ✅
def nama_fungsi(arg1, arg4, arg2=128, arg3=64)
...
Keyword arguments come after every positional argument has a value
Wrong ❌
def nama_fungsi(arg1, arg2, arg3=64, arg4=128)
...
nama_fungsi(arg3=70, arg4=56, 30, 64)
nama_fungsi(30, arg3=64, arg4=56)
Right ✅
def nama_fungsi(arg1, arg2, arg3=64, arg4=128)
...
nama_fungsi(30, 64, arg3=70, arg4=56)
nama_fungsi(30, 64, arg4=40, arg3=102)
nama_fungsi(30, arg4=64, arg2=102)
One argument can only be filled once
Wrong ❌
def nama_fungsi(arg1, arg2, arg3=64, arg4=128)
...
nama_fungsi(22, 34, arg1=70) # arg1 is filled twice
nama_fungsi(22, 34, arg3=70, arg3=12) # arg3 is filled twice
When Should You Use a Default Argument?
Using default arguments can be one way to speed up how fast you write and understand code. With default arguments you can minimize filling in the same argument over and over with the same value.
That way your code stays compact and easier to read.
There are also cases where an argument isn't really important, yet you still want to keep it flexible, and that is where default arguments help a lot.
Most Python libraries use default arguments too, to make life easier for programmers. As an example, let's pull one function from the numpy library.
This is the function signature for creating a numpy array:
numpy.array(object, dtype=None, *, copy=True, order='K', subok=False, ndmin=0, like=None)
If you look at it, only 1 argument is a positional argument (the first one), and the rest are all default arguments.
That may be because the owner of numpy knows we will rarely use the other arguments. Rarely isn't the same as never, though, so they still have to be offered as an option.
What's Next?
That's all for this one. Next you'll look at variable length argument in Python 3.
Happy coding 👋