Methods and Functions

Python Function Arguments (Default, Keyword, Arbitrary)

Before learning about Python Function Arguments, let us first understand what arguments are used for and why they are important. Python Arguments are used in order to pass the data elements or information to the functions. Arguments are enclosed in parentheses after the function name. You can enter as many parameters as you like; simply separate them with a comma. Below is an example of passing argument values into the function.

Example –

                    

def addition(a, b):
    sum = a+b
    print('Addition of 2 numbers:', sum)

addition(10,7)

Output –

                    

Addition of 2 numbers: 17

In the above example, we defined a function ‘addition’ and passed 2 parameters ‘a’ and ‘b’ into the function. Their values are initialized when we call the function.

Note – It is necessary to call a function that has been defined by passing an argument to it. If we do not pass any argument to a defined function, the compiler/interpreter will print an error.

Example –

                    

def world(a, b):
    print(a, b)

# Calling function without passing any parameters   
world()

Output –

                    

Traceback (most recent call last):
File "", line 5, in 
TypeError: world() missing 2 required positional arguments: 'a' and 'b'

Definition

  • Python function arguments are used in order to pass data values through the functions declared in the program.

Python Function Arguments

In Python function arguments, the user is able to pass its own data values in order to execute the functions Functions had a set number of arguments up until now. There are alternative ways to define a function in Python that can take a variable number of arguments. These types of different forms of Python function arguments are mentioned below. Let us look at each one of them in detail.

  1. Python Default Arguments –

In Python, Function arguments can contain default values assigned to them. This can be done by using the assignment operator (=) in the functional argument. When a function is called without a value for an argument, the default value is used. This is useful when someone forgets to pass an argument to the function and executes the program.

Note – Python Default arguments are also called as Python Optional arguments. An optional argument in Python is one that has a default value. The assignment operator can be used to set a default value for an argument. When calling a function, there is no need to supply a value for an optional argument. This is because if no value is supplied, the default value will be utilized. For example, consider the below program example.

Example –

                    

# Python program to illustrate Default Arguments in Functions
# This function is used to welcome students to school
def welcome(fullname, msg = 'Welcome to School!!'):

# If the msg argument value is not provided, then the default value in the Function argument is considered
    print('Hey there', fullname + ', ' + msg)

welcome('Sam')   # 2nd argument value not passed; will use function default argument
welcome('Zack', 'How have you been?')

Output –

                    

Hey there Sam, Welcome to School!!
Hey there Zack, How have you been?

In the above function, we declared a default argument named ‘msg’ in the function parameters. The argument ‘fullname’ does not have a default value declared. If a value is passed in the function argument for the default argument ‘msg’ then it will override this value and print the value passed to the argument.

Note – A default value can be set for any number of arguments in a function. However, if we have a default argument, we must likewise have default values for all the arguments to their right. Non-default arguments, in other words, cannot be followed by default arguments.

Example –

                    

def function(a = 24, b, c):
    print('Values are:')
    print(a)
    print(b)
    print(c)

function(10, 4)

Output –

                    

File "", line 1
SyntaxError: non-default argument follows default argument

  1. Python Keyword Arguments –

Required Arguments in Python are arguments that are passed in a fixed positional order to the function. But what if we would want a mixed order of passing of the arguments? Python Keyword arguments are the contradiction to the Required Arguments and provide the exact opposite function. In this method, we can pass the arguments to the function in any random order, as long as we specify the keyword to which the value goes.

Example –

                    

def fruits(a, b, p):
    print('We have', a+ ',', b+ ' and', p+ ' at our store.')

fruits('apple', 'banana', 'pineapple')

For the above program, we can use Keyword argument as follows –

                    

# Arguments in order
fruits(a = 'apple', b = 'banana', p = 'pineapple')

# Arguments out of order
fruits(b = 'banana', p = 'pineapple', a = 'apple')

# 2 positional, 1 keyword argument
fruits('apple', b = 'banana', p = 'pineapple')

Output –

                    

We have apple, banana and pineapple at our store.

All the above keyword argument methods will fetch the same output.

Note – However, we must remember that the keyword arguments must always come after the positional arguments. There will be issues if a positional argument is used after the keyword arguments.

Example –

                    

# keyword argument before positional argument
fruits(b = 'banana', 'pineapple', 'apple')

Output –

                    

File "", line 5
SyntaxError: positional argument follows keyword argument

  1. Python Arbitrary Arguments –

As the name suggests, sometimes the programmer does not know the number of arguments to be passed into the function. In such cases, Python arbitrary arguments are used. In this, we use the asterisk (*) to denote this method before the parameter in the function. This is also called as Python *args.

Python *args allows a function to accept any number of positional arguments i.e. arguments which are non-keyword arguments, variable-length argument list.

Remember ‘args’ is just a notation. We can use any other argument name instead of it.

Example 1 –

                    

# Program to add and display sum of n number of integer
def add(*num):
    sum = 0;
    for n in num:
        sum = sum + n;
    print("Sum:", sum)

add(3,4,5,6,7)
add(1,2,3,5,6,7,8)

Output –

                    

Sum: 25
Sum: 32

In the above program, the programmer does not know in advance the number of passing arguments in the function. Hence we have used the Arbitrary arguments method. The *num accepts all the parameters passed in the function add() and computes summation for all the values.

Python *args can also print data of various types which is passed into the function. For example, Information related to a person such as a Name, Sex, Age, City, Mobile number, etc.

Example 2 –

                    

def Person(*args):
    print(args)

Person('Sean', 'Male', 38, 'London', 9375821987)

Output –

                    

('Sean', 'Male', 38, 'London', 9375821987)

Conclusion

To conclude, there are 3 types of Function Arguments in Python. They are – Default, Keyword, and Arbitrary Arguments. Keyword arguments allow us to employ any order, whereas default arguments assist us to deal with the absence of values. And finally, in Python, arbitrary arguments come in handy when we don’t know how many arguments are needed in the program at that moment.

Frequently Asked Questions

Q1. What are optional arguments in the Python function arguments?

Python Default arguments are also called as Python Optional arguments. An optional argument in Python is one that has a default value. The assignment operator can be used to set a default value for an argument. When calling a function, there is no need to supply a value for an optional argument. When a function is called without a value for an argument, the default value is used. This is useful when someone forgets to pass an argument to the function and executes the program.

Example –

                    

# Python program to illustrate Optional Arguments in Functions
def student(name, degree = 'Masters'):
      print('Welcome', name+ '', 'to the', degree+ ' course!')

student('Sam')

Output –

                    

Welcome Sam to the Masters course!

Here, the parameters/argument ‘degree’ has a default value assigned to it. While calling the function, if we do not specify the 2nd argument value, then the default value will be used else it will be overridden by the specified argument value while calling the function.

Q2. How do you add Optional parameters in Python?

Optional parameters contain a default value assigned to them. This can be implemented by using the assignment operator (=) in Python. While defining the function, we need to assign the value to the argument we want as an optional parameter.

Example –

                    

def subj_marks(history, science = 50, algebra = 78):
    print('Marks in History: ', history)
    print('Marks in Science: ', science)
    print('Marks in Algebra: ', algebra)

subj_marks(66)

Output –

                    

Marks in History:  66
Marks in Science:  50
Marks in Algebra:  78

In the above program, function arguments ‘science’ and ‘algebra’ have been assigned default data values with the help of the assignment operator. Hence, they become optional parameters.

Q3. What are the required arguments in Python? Is it a type of Python function arguments?

Values given through arguments during function calls should be in the order of parameters in the function declaration. These are called as Required arguments or Positional arguments. The arguments supplied to a function in the correct positional sequence are known as required arguments.

Example –

                    

def add(a,b,c):
    print('Sum:', a+b+c)

add(a = 10, b = 34, c = 5)

Output –

Q4. What method can we use if we want to pass multiple arguments to the function without defining each separately?

In order to pass multiple argument values to the function, Python provides us with Arbitrary Arguments also known as Python *args. In this, we use the asterisk (*) to denote this method before the parameter in the function. The asterisk (*) allows us to pass any number of values to the defined function.

Python Arbitrary Arguments allows a function to accept any number of positional arguments i.e. arguments that are non-keyword arguments, variable-length argument list.

Example –

                    

def student(*name):
    print('Welcome to the class of 2020-22:')
    for i in name:
        print(i)

student('Ash', 'Betty', 'David', 'Sam', 'Lisa', 'John', 'Isha')

Output –

                    

Welcome to the class of 2020-22:
Ash
Betty
David
Sam
Lisa
John
Isha

Q5. *args is a special syntax used in python to pass variable-length keyword arguments to a function?

  1. True
  2. False

Answer. Option B. Python *args is also called as Arbitrary arguments which are used to pass non0keyworded arguments to the function.

Share with friends

Customize your course in 30 seconds

Which class are you in?
5th
6th
7th
8th
9th
10th
11th
12th
Get ready for all-new Live Classes!
Now learn Live with India's best teachers. Join courses with the best schedule and enjoy fun and interactive classes.
tutor
tutor
Ashhar Firdausi
IIT Roorkee
Biology
tutor
tutor
Dr. Nazma Shaik
VTU
Chemistry
tutor
tutor
Gaurav Tiwari
APJAKTU
Physics
Get Started

Leave a Reply

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

Download the App

Watch lectures, practise questions and take tests on the go.

Customize your course in 30 seconds

No thanks.