Python Function Arguments with Types, Syntax and Examples

1. Python Function Argument

Previously, we have covered Functions in Python. In this Python Function Arguments tutorial, we will learn about what function arguments used in Python and its type: Python Keyword Arguments, Default Arguments in Python, and Python Arbitrary Arguments.

So, let’s start Python Function Arguments.

Python Function Arguments with Types, Syntax and Examples

Python Function Arguments with Types, Syntax and Examples

 

2. What is Python Function?

Python function is a sequence of statements that execute in a certain order, we associate a name with it. This lets us reuse code.

We define a function using the ‘def’ keyword. Let’s take an example.

>>> def sayhello():
                """
                This prints Hello
                """
                print("Hello")

Then to call this, we simply use the function’s name with parentheses. Also, notice the docstring.

>>> sayhello()

Hello
This one takes no arguments. Now, let’s see one with python function arguments.

>>> def sum(a,b):
                return a+b
>>> sum(2,3)

5
To this, if we pass only one argument, the interpreter complains.

>>> sum(3)
Traceback (most recent call last):
File "<pyshell#44>", line 1, in <module>
sum(3)

TypeError: sum() missing 1 required positional argument: ‘b’
To deal with such situations, we see different types of arguments in python functions.
Learn: Recursion in Python with Examples

Python Interview Questions

3. Types of Python Function Arguments

There are various types of Python arguments functions. Let’s learn them one by one:

a. Default Argument in Python

Python Program arguments can have default values. We assign a default value to an argument using the assignment operator in python(=). When we call a function without a value for an argument, its default value (as mentioned) is used.

>>> def greeting(name='User'):
                print(f"Hello, {name}")
>>> greeting('Ayushi')

Hello, Ayushi

>>> greeting()

Hello, User

Join DataFlair on Telegram

Here, when we call greeting() without an argument, the name takes on its default value- ‘User’.

Any number of arguments can have a default value. But you must make sure to not have a non-default argument after a default argument. In other words, if you provide a default argument, all others succeeding it must have default values as well. The reason is simple. Imagine you have a function with two parameters. The first argument has a default value, but the second doesn’t. Now when you call it(if it was allowed), you provide only one argument. The interpreter takes it to be the first argument. What happens to the second argument, then? It has no clue.

>>> def sum(a=1,b):
                return a+b

SyntaxError: non-default argument follows default argument
This was all about the default arguments in Python
Learn: Python Built-In Functions with Syntax and Examples

b. Python Keyword Arguments

With keyword arguments in python, we can change the order of passing the arguments without any consequences. Let’s take a function to divide two numbers, and return the quotient.

>>> def divide(a,b):
                return a/b
>>> divide(3,2)

1.5
We can call this function with arguments in any order, as long as we specify which value goes into what.

>>> divide(a=1,b=2)

0.5

>>> divide(b=2,a=1)

0.5
As you can see, both give us the same thing. These are keyword python function arguments.
But if you try to put a positional argument after a keyword argument, it will throw Python exception of SyntaxError.

>>> divide(b=2,1)

SyntaxError: positional argument follows keyword argument in python.
Learn how to do Exception Handling in Python.
Any Doubt yet in Python Function Arguments

c. Python Arbitrary Arguments

You may not always know how many arguments you’ll get. In that case, you use an asterisk(*) before an argument name.

>>> def sayhello(*names):
                for name in names:
                                print(f"Hello, {name}")

And then when you call the function with a number of arguments, they get wrapped into a Python tuple. We iterate over them using the for loop in python.

>>> sayhello('Ayushi','Leo','Megha')

Hello, Ayushi
Hello, Leo
Hello, Megha
Learn: Python Applications – Python Use Cases in Real World
This was all about the Python Function Arguments.

4. Conclusion: Python Function Arguments

Hence, we conclude that Python Function Arguments and its three types of arguments to functions. These are- default, keyword, and arbitrary arguments. Where default arguments help deal with the absence of values, keyword arguments let us use any order. Finally, arbitrary arguments in python save us in situations where we’re not sure how many arguments we’ll get. Furthermore, if you have a query, feel free to ask in the comment box
Top python Books to learn Python programming language.
For reference

2 Responses

  1. venu says:

    for what purpose the arguements are using!!

    • DataFlair Team says:

      Hi Venu,
      We can pass arguments to a function to make it operate on their values. Like a function add(a,b) adds the two numbers a and b and needs their values to perform the addition.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.