Python Function Arguments – Learn the 3 types of Arguments

Python, like many other programming languages, allows you to break large code into smaller components. We do this by creating functions. But before we go ahead and discuss the types of arguments a Python function can take, let’s quickly recall what a Python function looks like.

Python Functions

A function is basically a group of statements that performs some task. A function can:

accept some arguments ( if needed),

perform some functionality,

and lastly can return a result.

Below is a function that takes an argument and calculates its square:

Function definition:

def calcSquare(x):
result = x ** 2
return result

Function call:

num = 5
print("The square of", num, "is", calcSquare(num))

Output:

The square of 5 is 25
>>>

We see here that we are passing a value (through function call) to the function calcSquare, and the function is receiving value (in the function definition). Let us define these two types of values more formally:

Here, the “sayhello” function’s header contains a parameter “name” which has its default value set to the string “World”.

As you can see in the function call, the function prints “Hello, World” when no argument is passed. Whereas, if we do pass a value for the argument, the function will print “Hello” followed by the argument value.

The output looks like this:

Hello, World
Hello, Techvidvan
>>>

One very important thing to remember while dealing with default arguments is :

In a function’s definition, a parameter cannot have a default value unless all the parameters to its right have their default values.

2. Python Keyword Arguments

Python offers a way to write any argument in any order if you name the arguments when calling the function. This allows us to have complete control and flexibility over the values sent as arguments for the corresponding parameters.

In the first function call, a gets value 10 and b gets value 5. In the second function call, a gets value 9 and b gets value 20.

The output is:

50
180
>>>

3. Python Arbitrary Arguments

Arbitrary arguments come in handy when we don’t know how many arguments the function will take. Often at the time of defining the function, we cannot determine the number of arguments our function is going to accept.

We place an asterisk ( * ) before the parameter to denote that the function can take an arbitrary number of arguments.

The function summation takes multiple arguments. This outputs to the following:

60
>>>

You can now give any number of arguments while calling the function summation.

Summary

Here we come to the end of our article. In this article, we learned about the different arguments supported by a Python function. We have multiple types of arguments at our disposal in Python. This article will surely help you in your future Python Career.

Now it’s upon you to decide what kind of argument will best suit your function’s requirements.

Thank you for reading our article. If you have any doubts related to Python Function Arguments, do let us know by dropping a comment below.