In this post, we’ll learn what Python function arguments are and how to properly use them.
We’ll be using multiple practical code examples and will explain them step by step so you can have a complete idea of Python function arguments.
After reading this post, I’m sure that you’ll be able to easily use Python function arguments in your own code.
So let’s not keep ourselves waiting anymore and just dive right into understanding it practically.
What is Python Function Arguments?
Arguments are used to pass some data to Python function. The number of arguments should match the number of parameters of Python function. If not, then an error will be generated. We’ll see that in our examples as well.
Let’s now implement Python function arguments using practical code examples. But first, let’s understand its syntax.
Syntax of Function Arguments
functionName(argument) // calling a function
Assume that we have a function with 2 parameters. So when calling that function, we’ve to pass two arguments to the function. These can be a string, integer, float etc.
You can use as much arguments as you want but keep in mind that the number should match the number of parameters of that specific function. We’ll also look at how to pass less arguments but still make the code work.
Implementing Python Function Arguments (Multiple Example)
Below examples will practically demonstrate how to use Python function arguments properly.
Example 1: Passing Multiple Arguments
def justFun(a,b): c=a+b print(c) justFun(10,46) // 2 arguments passed to function
Output
56
You can see that we have defined two parameters in our custom function. It means when we call this function then we have to pass two arguments to it.
They are required and is position sensitive. It means the first argument will pass the value to first parameter of function, the second argument to the second parameter and so on.
Example 2: Mismatching Number Of Arguments and Parameters
def justFun(a,b): c=a+b print(c) justFun(10)
Output
TypeError: justFun() missing 1 required positional argument: 'b'
You can see that we got an error when we define two parameters in function and pass just one argument when calling that Python function. This error says that the second argument is missing.
First one is passed successfully as it is position sensitive so first argument is passed to the first parameter but second one is not present.
So always make sure that the number of both arguments and parameters match.
In order to remove this error, you can also specify default parameter value. See the below section to understand how its done.
Example 3: Assign Parameters a Default Value
def justFun(a,b=20): c=a+b print(c) justFun(10) // 1 arguments passed to function
Output
30
As you can see, we’ve passed only one argument but still its not showing any error. Reason for that is, we have passed a default value to the second parameter so if the second argument is not passed then that default value will be used.
We can specify both the parameters with some value and it will work if we don’t pass any argument while calling the function. See below code:
def justFun(a=10,b=20): c=a+b print(c) justFun() // no arguments passed to function
Output
30
Just make sure that the parameters having default value should be in the end. Reason is that the arguments passed to it will be position sensitive. So assume that you have two parameters in a function but you’ve passed one argument to it. First one has a default value while the second one don’t, then still the value of argument will be passed to the first parameter. See below code:
def justFun(a=10,b): c=a+b print(c) justFun(10) // 1 arguments passed to function
Output
SyntaxError: non-default argument follows default argument
Example 4: Keyword Arguments
By using this method, we don’t have to focus on the order in which the arguments are passed. If you pass arguments directly then the first argument’s value will be assigned to the first parameter, second argument’s value to the second parameter and so on.
Let’s now see how to use keyword Python function arguments. See below code:
def justFun(a,b): print(a+b) justFun(b='Assigns value to second parameter', a='Assigns value to first parameter')
Output
Assigns value to first parameter Assigns value to second parameter
You can see that by using the name of parameters, we can specify which parameter should be initialized first.
Example 5: Arbitrary Arguments
Its very useful where we don’t know the number of arguments that will be passed to the Python function’s parameter. See below code:
def justFun(*value): print(value) justFun(2,3,'Green',3.5)
Output
(2, 3, 'Green', 3.5)
You can pass even more items as arguments.
So this is how you can use Python function arguments properly in your own code. Hope you like this post.
Feel free to ask if you still have any questions related to the implementation of Python function arguments. I’ll be happy to answer all.
Conclusion
To conclude, hope you now have a detailed practical understanding of what Python Function arguments are and how to properly use them. I’ll be looking forward to receive your valuable thoughts on this post. Thank you for reading it.