Function Arguments


 Function Arguments

           The arguments are the type of information which can be passed into the function. The arguments are specified in the parentheses. We can pass any number of arguments, but they must be separate them with a comma.

Consider the following example, which contains a function that accepts a string as the argument.

Example


  1. #defining the function    
  2. def func (name):    
  3.     print("Hello ",name)   
  4. #calling the function     
  5. func("Guptaji")     

Output

Hello guptaji

Types of Arguments

       There may be several types of arguments which can be passed at the time of function call.
  1. Required arguments
  2. Keyword arguments
  3. Default arguments
  4. Variable-length arguments

1. Required Arguments

   These are the arguments which are required to be passed at the time of function calling with the exact match of their positions in the function call and function definition.If either of the arguments is not provided in the function call, or the position of the arguments is changed, the Python interpreter will show the error.

Example 1

  1. def func(name):    
  2.     message = "Hello "+name  
  3.     return message  
  4. name = input("Enter the name:")    
  5. print(func(name))    

Output

Enter the name: guptaji

Hello guptaji

Example 2

  1. #the function calculate returns the sum of two arguments a and b    
  2. def calculate(a,b):    
  3.     return a+b    
  4. calculate(10# this causes an error as we are missing a required arguments b.    

Output

TypeError



2. Keyword Arguments

      Python allows us to call the function with the keyword arguments. This kind of function call will enable us to pass the arguments in the random order. Keyword arguments are related to the function calls. When you use keyword arguments in a function call, the caller identifies the arguments by the parameter name.

The name of the arguments is treated as the keywords and matched in the function calling and definition. If the same match is found, the values of the arguments are copied in the function definition.

Example

def student(firstName,lastName):

             print(firstName,lastName)

student(firstName="technical",lastName="guptaji")

student(lastName="guptaji",firstName="technical")

Output

technical guptaji

technical guptaji

Note : The idea is to allow caller to specify argument name with values so that caller does not need to remember order of parameter.The order of parameter does not matter.


3. Default Arguments

Python allows us to initialize the arguments at the function definition. If the value of any of the arguments is not provided at the time of function call, then that argument can be initialized with the value given in the definition even if the argument is not specified at the function call.

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

Example 1

  1. def printme(name,age=25):    
  2.     print("My name is",name,"and age is",age)    
  3. printme(name = "guptaji")  

Output

My name is guptaji and age is 25


Example 2

  1. def printme(name,age=25):    
  2.     print("My name is",name,"and age is",age)    
  3. printme(name = "guptaji"#the variable age is not passed into the function however the default value of age is considered in the function    
  4. printme(age =15,name="david"#the value of age is overwritten here, 10 will be printed as age  

Output

My name is guptaji and age is 25

My name is david and age is 15

4. Variable-length Arguments

     Sometimes we may not know the number of arguments to be passed in advance. In such cases, Python provides us the flexibility to offer the comma-separated values which are internally treated as tuples at the function call. By using the variable-length arguments, we can pass any number of arguments.

An (*) asterisk is placed before the variable name that holds the values of all the non-keyword variable arguments. This tuple remains empty if no additional arguments are specified during the function call.

Example

def printinfo(arg1,*vartuple):

            "this prints variable passed arguments"

             print("output is:",arg1)

             for var in vartuple:

                             print(var)

             return;

printinfo(10)

printinfo(70,60,50)

Output

output is: 10

output is: 70

60

50






No comments:

Post a Comment