Friday, January 21, 2022

What are the types of Arguments of the Functions in Python Program

The types of Arguments passed to the Functions in Python Program
In Python, we can define a function with arguments(or parameters). These arguments are useful to pass the values from outside of the function when we are calling.
While defining the function, the arguments specified are called the 'formal arguments', and when we are calling the function, the values we pass for the arguments are called 'actual arguments'.

Example:
# Formal and Actual arguments of the Function:
def person(name, age): # name, age are formal arguments
    if age > 55:
        print('{0} is eligible for Pension Scheme'.format(name))
    else:
        print('{0} is not eligible for Pension Scheme'.format(name))
x = 'Goldfish'
y = 45
status = person(x, y) # x, y are actual arguments

#Result:
Goldfish is not eligible for Pension Scheme

The actual arguments using calling a function are generally 4 types:
  • Positional arguments
  • Keyword arguments
  • Default arguments
  • Variable length arguments
# ------------------------------------------------------------------------------------------------------------------------ #
Positional Arguments:
These are the arguments passed to a function in correct positional order. The no.of arguments and their positions in the function definition should match exactly with the number and position of the argument in the function call.

Example:
def funprod(product, price, discount): 
    """function to calculate the discount rate for the product"""
    disc_rate = round((discount/price),2)
    print('The discount rate for the product {0} is: {1}%'.format(product, disc_rate))
x = 'Mango'
y = 45
z = 5
result = funprod(x, y, z) 

#Output:
The discount rate for the product Mango is: 0.11%

If we observe the above example, while calling the function we passed the actual arguments in the correct positional order as per the order of the formal arguments in the function.

Otherwise, if you try to pass as funprod(y, x, z) then it will throw an exception as the function is unable calculate the discount rate based on x which is a string.
disc_rate = round((discount/price),2)
TypeError: unsupported operand type(s) for /: 'int' and 'str'

Also if you try to skip one argument, funprod(x, y, ) still we get an error:
result = funprod(x, y,)
TypeError: funprod() missing 1 required positional argument: 'discount'

# ------------------------------------------------------------------------------------------------------------------------ #
Keyword Arguments:
The keyword arguments are the arguments that identify the parameters by their names. In this type of arguments the order is not a matter as we directly calling the names of the arguments while passing the values.

Example:
def funprod(product, price, discount): 
    """function to calculate the discount rate for the product"""
    disc_rate = round((discount/price),2)
    print('The discount rate for the product {0} is: {1}%'.format(product, disc_rate))

# Calling the function with keyword arguments
result = funprod(product='Mango', discount=5, price=45)

#Output:
The discount rate for the product Mango is: 0.11%

# ------------------------------------------------------------------------------------------------------------------------ #
Default Arguments:
We can mention a default value for a parameter or argument while defining the function. So that when we are calling the function, we no need to pass the value for the default argument.
Example:
def funprod(product, price, discount=5): # discount is the default argument here
    """function to calculate the discount rate for the product"""
    disc_rate = round((discount/price),2)
    print('The discount rate for the product {0} is: {1}%'.format(product, disc_rate))

# Calling the function with keyword arguments
result = funprod(product='Mango', price=45) # no need to pass the discount

#Output:
The discount rate for the product Mango is: 0.11%

# ------------------------------------------------------------------------------------------------------------------------ #
Variable Length Arguments:
In some cases, when we don't know how many values a function may receive. In such case, we cannot decide how many arguments to be given in the function definition.
For example, if we create a function as add(a, b) to find the sum of two numbers. Later if we try to use this function to find the sum of 3 numbers add(10, 5, 15) then it will give an error.

In this type of scenario, we can develop a function that accepts 'n' no.of arguments using the concept of Variable length arguments.
The variable length argument can be defined by prefixing the '*" to the argument in a function definition as shown below:
def add(farg , *args)
Here, farg is the formal argument and *args is the variable length argument.

Example:
def addn(farg, *args):
    """function to calculate the sum of given n numbers"""
    print('The formal argument is: ', farg)
    total = 0
    for i in args:
        total = total+i
    print('The sum of all numbers is= ', (farg+total))

# call the addn() function and pass arguments
addn(5,7)
addn(2, 3, 5, 7, 8)

#Results:
The formal argument is:  5
The sum of all numbers is=  12
The formal argument is:  2
The sum of all numbers is=  25

Similarly, a keyword variable length argument is an argument that can accept any number of values provided in the format of key and values.
We can declare a keyword length argument by prefixing the ** before argument as:
def funvalues(farg, **kwargs)
Here, farg is a formal argument, and kwargs represents the keyword variable length argument which internally represents a dictionary object.

Example:
def display(prodid, **kwargs):
    """function to display the key and values"""

    print('The formal argument is: ', prodid)
    for x, y in kwargs.items():
        print('key = {}, value = {}'.format(x, y))

# passing one formal argument and 3 keyword arguments
display(123, product='Mango', price=45, discount=0.5)
OR
display(prodid=123, product='Mango', price=45, discount=0.5)

Result:
The formal argument is:  123
key = product, value = Mango
key = price, value = 45
key = discount, value = 0.5

#--------------------------------------------------------------Thanks--------------------------------------------------------------#

No comments:

Post a Comment

Hi User, Thank You for visiting My Blog. Please post your genuine Feedback or comments only related to this Blog Posts. Please do not post any Spam comments or Advertising kind of comments which will be Ignored.

Featured Post from this Blog

How to compare Current Snapshot Data with Previous Snapshot in Power BI

How to Dynamically compare two Snapshots Data in Power BI Scenario: Suppose, we have a sample Sales data, which is stored with Monthly Snaps...

Popular Posts from this Blog