google.com, pub-8786015629279405, DIRECT, f08c47fec0942fa0 Types of Function (Argument)

Types of Function (Argument)

0

Types of Function in Python

 Types of Arguments

Python supports various types of arguments that can be passed at the time of the function call. Let’s discuss each type in detail.

  1. Default arguments
  2. Keyword arguments
  3. Variable-length arguments
    • *args (Non-Keyword Arguments)
    • **kwargs (Keyword Arguments)

Default Argument

A default argument is a parameter that assumes a default value if a value is not provided in the function call for that argument. 

The syntax and default values for function parameters are represented differently in Python. If no argument value is given during the function call, the default value will be used for the function parameter. Let's look at this from the perspective of a function student. The function student has three arguments, two of which have default values supplied to them. As a result, the function student takes one mandatory argument (name) and two optional arguments (branch and year).

Example:

def student(name, branch ='Computer Science', year ='Fifth'):

 

            print(name, 'studies in', branch, year, ’Semester’)

 

 

 

When calling functions, we must consider the following tips in mind:

 

  • When passing keyword parameters, the order of the arguments is crucial.
  • For each parameter, there should only be one value.
  • The keyword name passed in should be the same as the actual keyword name.
  • When calling a function that takes non-keyword arguments, the order is crucial.
Example:

 

# Calling functions without keyword arguments

 

def student(name, branch ='Computer Science', year ='Fifth'):

 

            print(name, 'studies in', branch, year, ’Semester’)

 

# First Argument

student('Abhi')

 

# All Three Argument                                                               

student(' Abhi ', 'Electronics', 'Third')       

 

# First Two Argument

student(' Abhi ', 'Electrical')  

 

#First and Third Argument                           

student(' Abhi ', 'First')

 

Output

Abhi Computer Science Fifth Semester

Abhi Electronics Third Semester

Abhi Electrical Third Semester

Abhi Electrical First Semester

 

 

In the first call, there is only one required argument and the rest arguments use the default values. In the second call, branch and year arguments value is replaced from default value to new passing value. We can see the order of arguments is important from the 2nd, 3rd, and 4th calls of the function.

Keyword Argument

The goal is to allow the caller to specify the argument name along with values, eliminating the requirement for the caller to remember the parameter order.

 

#Calling functions with keyword arguments

 

def student(name, branch ='Computer Science', year ='Fifth'):

 

            print(name, 'studies in', branch, year, ’Semester’)

 

# First Keyword Argument

student(name='Abhi')

 

# All Three Keyword Argument                                                                   

student(name=' Abhi ', branch='Electronics', year='Third')           

 

# First Two Keyword Argument

student(name=' Abhi ', branch='Electrical')      

 

#First and Third Keyword  Argument                               

student(name='Abhi ', year='First')

 

Output

Abhi Computer Science Fifth Semester

Abhi Electronics Third Semester

Abhi Electrical Third Semester

Abhi Electrical First Semester

 

 

Variable-length arguments

In Python, we can pass a variable number of arguments to a function using special symbols. There are two special symbols:

 

*args (Non-Keyword Arguments)

**kwargs (Keyword Arguments)

 

*args (Non-Keyword Arguments)

To send a variable number of arguments to a function, use the special syntax *args in function declarations in Python. It's used to send a variable-length argument list with non-keyworded arguments.

 

The symbol * is used to accept a variable number of arguments; it is frequently combined with the term args by convention.

You can use *args to accept more arguments than the number of formal arguments you specified earlier. *args allows you to add any number of additional arguments to your existing formal parameters (including zero extra arguments).

Example:

 

#Write a Python Program to illustrate *args for variable number of arguments

 

def hum(*argv):

            for arg in argv:

                        print (arg)

 

hum('Hello', 'Welcome', 'to', 'a2pstudy')

 

 

Output

 

Hello

Welcome

to

a2pstudy

 

 

**kwargs (Keyword Arguments)

**kwargs is a special syntax used in Python function definitions to pass a keyworded, variable-length argument list. We call ourselves kwargs because of the double star. The reason for this is that the double star allows us to send keyword arguments through (and any number of them).

When you provide a variable into a function as a keyword argument, you give it a name.

The kwargs may be thought of as a dictionary that maps each term to the value we provide along with it. That's why there doesn't appear to be any sequence in which the kwargs were printed out when we iterate through them.

Example:

 

# Write Python program to illustrate **kwargs for variable number of keyword arguments

def hum(**kwargs):

            for key, value in kwargs.items():

                        print ("%s == %s" %(key, value))

# call Function

hum(First ='Teach', Second ='with', Third=’A2pstudy')

 

Output

First==Teach

Second==with

Third==A2pstudy

 

 


Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.

Thank you for your interest 😊

We will back shortly after reviewing...

Thank you for your interest 😊

We will back shortly after reviewing...

Post a Comment (0)

#buttons=(Accept !) #days=(20)

Our website uses cookies to enhance your experience. Learn More
Accept !
To Top