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.
Keeping you updated with latest technology trends, Join TechVidvan on Telegram
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:
def calcSquare(x): result = x ** 2 return result
num = 5 print("The square of", num, "is", calcSquare(num))
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:
- Arguments: The values being passed, here num.
- Parameters: The values being received, here x.
Enhance your knowledge by exploring Python Built-in Functions in detail with Techvidvan.
Types of Python Arguments
We are going to talk about the values being passed, that is, the Arguments in this article. Python supports the following types of arguments :
1. Python Default Arguments
Python allows function parameters to have default values. This is useful in case a matching argument is not passed in the function call statement.
def sayhello(name = "World"): print("Hello,", name) sayhello() sayhello("Techvidvan")
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:
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.
def multiply(a, b): return a*b print(multiply(a = 10, b = 5)) print(multiply(b = 20, a = 9))
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:
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.
def summation(*numbers): sum1 = 0 for number in numbers: sum1 += number return sum1 print(summation(10,20,30))
The function summation takes multiple arguments. This outputs to the following:
You can now give any number of arguments while calling the function summation.
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.
Keep Learning 🙂