Python Functions: Default, Keyword, and Arbitrary Arguments.
By: Dennis Akagha
Tags: Python Programming

Remember the software principle that says, “Don’t Repeat Yourself (DRY)”and the main aim is to reduce repetition of code. The Zen of Python says “Simple is better than complex. Complex is better than complicated….” When I started learning coding, Functions in python was a thorn in the flesh for me. As much as I tried, I kept on not getting the syntax and in most cases, I could not differentiate the difference between default argument, keyword argument and arbitrary arguments. I was always repeating same code instead of wrapping my codes inside a function and call it whenever I want to use it.

The best way to learn how to code is to teach others and the best way to remember is to have a journal where you document all your learnings. I consider medium.com a journal for documenting my learnings which I can always refer to and also; a reference material for upcoming developers.

In Python, functions are a convenient way to divide your code into useful blocks, allowing us to order our code, make it more readable, reuse it and save some time. Also functions are a key way to define interfaces so programmers can share their code. You can pass data, known as parameters, into a function. Here, you will learn how to define a function and call it using default, keyword, and arbitrary arguments. For the benefit of those who are just learning python function, let’s learn how to create a function.

def my_function():

print("Hello, from Dennis") my_function()

Output:

Hello, from Dennis

Parameters and Arguments

Most people regard these two terms to be the same but in the real sense of it, they are different. A parameter is the name within the function definition and one thing to understand is that parameters don’t change when the program runs. Arguments are the values passed in when the function is called and going to be different every time the function is called. For instance:

def my_function(name, years):

print(name + ' is' + ' ' + years + ' years old') my_function('David', '20')

Output:

David is 20 years old

Considering the above function, ‘name’ and ‘year’ are the parameter while ‘David’ and ‘20’ are the arguments. Here, the function my_function() has two parameters. We called this function with two arguments which shows no error but when it’s called with more than the required number of arguments or no arguments, it will output an error message.

my_function('Devid')

Output:

--------------------------------------------------------------------

TypeError Traceback (most recent call last)

<ipython-input-259-00c478010f75> in <module>

----> 1 my_function('Devid')



TypeError: my_function() missing 1 required positional argument: 'years'

In Python, there are three ways to define a function that can take variable number of arguments which are default arguments, keyword arguments and arbitrary arguments.

Default Arguments

Python allows function arguments to have default values. If the function is called without the argument, the argument gets its default value. Default values indicate that the function argument will take that value if no argument value is passed during the function call. The default value is assigned by using the assignment(=) operator of the form keywordname=value.

def greeting(name, msg='Good morning sir!'):



print('Hello', name + ', ' + msg)



greeting('Dennis')

greeting('Felix', 'Hope you are fine?')

Output:

Hello Dennis, Good morning sir!

Hello Felix, Hope you are fine?

From the above, the first function was called without the argument, it output the default value but when the argument function was called with the argument, its output overwrites the default value. However, it’s important to note that a non-default arguments cannot follow a default arguments. For instance,

def greeting(msg='Good morning sir!', name):



print('Hello', name + ', ' + msg)

Output:

File "<ipython-input-266-4ed0c3bc0469>", line 1

def greeting(msg='Good morning sir!', name):

^

SyntaxError: non-default argument follows default argument

Python Keyword Arguments

Keyword arguments are arguments that can be called by their name. In other programming languages such as Dart, JavaScript, and Java , it’s called named parameter. With keyword arguments, when we call a function with some values, these values get assigned to the arguments according to their position. For instance:

def greeting(msg='I love you', name='Jane'):



print('Hello', name + ', ' + msg) greeting('Kindly come to the house', 'Dennis')

Output:

Hello Dennis, Kindly come to the house

Python Arbitrary Arguments

This type of argument is used if one do not know how many arguments that will be passed into one’s function. Python allows us to have the arbitrary number of arguments. This is especially useful when we are not sure in the advance how many arguments, the function would require.

We define the arbitrary arguments while defining a function using the asterisk (*) sign.

def greetings(*names):

for name in names:

print('Hello', name) greetings('Dennis', 'James', 'Peter'

All the passed arguments are stored in as a tuple, before they are passed to the function. Inside the function, we are displaying all the passed arguments using for loop.