Python is a high-level, interpreted programming language, and is one of the most popular programming languages present in the software industry.
One of the most important concepts in python is that of positional arguments used in functions. Let us have a look at it in detail.
In python, arguments are used in functions to pass on specific information when they are called so that their functionality can be tailored according to requirements.
Arguments are often termed as parameters as well. One type of argument is the positional argument.
These are used when you do not want to supply the arguments in the form of key=value pairs and work well when you need to keep the code as short as possible.
The syntax to specify positional arguments in python using functions is pretty straightforward and is as follows.
When defining the function, we specify the name of the argument and if we have multiple arguments we separate them using commas.
Do keep in mind the indentations required to specify the function body.
def <function_name>(<argument_name1>, <argument_name2>, …..):
<statements>
To call the functions using positional arguments, the syntax is as follows:
<function_name>(<argument1_or_value1>, <argument2_or_value2>, ….)
This is a very simple syntax however there are some rules one needs to keep in mind when specifying arguments.
The order of arguments in python is very important both while defining and calling functions when using positional arguments.
When we call functions we can either use variables or values directly.
One important rule to remember is that when calling a function, keyword arguments cannot be specified before positional arguments.
In this example, we look at the usage of the concept of positional arguments in a function call that helps us to calculate the difference between two given arguments.
One important thing here is that we need to pay special attention to the order of the arguments passed in during function calls.
Here, we have a function named func that takes in two arguments a and b. It stores the difference of these arguments in a variable named diff and returns it, i.e. a-b.
We call this function first using two variables x and y, with respective argument names as well, containing values 3 and 2 respectively.
As expected the result is stored in a variable z and on printing it we see an output of 1 which is the difference between 3 and 2.
Let us take a look at the python code and corresponding output for this method:
# Function with arguments a and b
def func(a, b):
# Variable to store the difference
diff = a - b
# Returning the difference
return diff
# Variables to store values to be passed
x = 3
y = 2
# Storing the result in variable z and printing it
z = func(x, y)
print(z)
Output
1
Now let us say we interchange the order in which the arguments are passed and pass b first followed by a, both as positional arguments.
The output gets changed and we receive -1 as the output on printing the returned value after storing it in a variable named z.
Obviously, this is not the desired output that tells us how important the order of positional arguments is while calling a function.
Let us take a look at the python code and corresponding output for this method:
# Function with arguments a and b
def func(a, b):
# Variable to store the difference
diff = a - b
# Returning the difference
return diff
# Variables to store values to be passed
x = 3
y = 2
# Storing the result in variable z and printing it
z = func(y, x)
print(z)
Output
-1
Let's understand with one more example. Let's say we have a function that takes one integer and one string argument. In this case,
# Function with arguments a and b
def func(a, b):
if(len(a)>b):
return a[b]
return a
# Variables to store values to be passed
x = "Hello"
y = 4
# Storing the result in variable z and printing it
z = func(x, y)
print(z)
Output:
o
What if we change the order of arguments and call the function, then we get the error:
# Function with arguments a and b
def func(a, b):
if(len(a)>b):
return a[b]
return a
# Variables to store values to be passed
x = "Hello"
y = 4
# Storing the result in variable z and printing it
z = func(y, x) # changed the order
print(z)
Output:
TypeError: object of type 'int' has no len()
To handle this situation/problem, Python provides keyword argument passing. You can read this important topic in our detailed article: Python Keyword Arguments
As mentioned earlier, we cannot have positional arguments after keyword arguments in a function call.
In this example, we have the same function func as before to return the difference between two arguments but a difference here is that only a is a keyword argument with a value of x whereas b is a positional argument.
As we know, having positional arguments after keyword ones is not allowed in python and we get an error stating the same.
Let us look at the python code and corresponding output for this example:
# Function with arguments a and b
def func(a, b):
# Variable to store the difference
diff = a - b
# Returning the difference
return diff
# Variables to store values to be passed
x = 3
y = 2
# Storing the result in variable z and printing it
z = func(a = x, y)
print(z)
Output
SyntaxError: positional argument follows keyword argument
In this topic, we have learned the use and advantages of positional arguments in a Python program, following some simple running examples, thus giving us an intuition of how this concept could be applied in real-world situations. Feel free to reach out to info.javaexercise@gmail.com in case of any suggestions.