Function and Arguments

XII class notes....,
Arguments and parameters....
Arguments are the values which are passed while calling a function.
Parameters are the values which are provided at the time of function definition
e. g.
def sum(a,b):   .......,...>parameters
      s=a+b
      return s
x = 12
y = 56
t = sum( x, y)    ...............> arguments
Type of arguments......
There are 4 types of arguments..
1)   Required / Positional arguments
       Arguments passed to a function
      in correct positional order,     
     no. of arguments must match
     with no. of parameters required 
     e.g
def sum(a,b):   .......,...> 2 parameters
      s=a+b
      return s
x = 12
y = 56
t = sum( x, y)    ............> 2 arguments
In above example argument x is copied to parameter a and y is copied to parameter b
2)  Default arguments.........
Default values are specified in function header
It is optional in function call statement. If not
provided in function call statement then default value is considered.
e. g.
   def col_discount(amt,dis_rate=10):
               return amt - dis_rate

cal_discount(500)                       calling without passing dis_rate cal_discount(600,5)
calling with passing dis_rate

3)  Keyword arguments..........
      Key word arguments are the
named arguments with assigned values being passed in
function call statement.
Rules for combining three types of arguments:
e. g.
def sum(a,b): 
      s=a+b

      return s
sum(a=20, b= 30)
sum(b=30, a=20)
In above calling statements, we need not follow positional arguments

4)  Variable length arguments
......
      In some situation one needs
to pass as many as argument to a function, python provides a way to pass number of argument to a function, such type of
arguments are called variable length arguments. Variable
length arguments are defined with * symbol.
e. g.
def show ( *n) :
       print(n)
#calling function
Show (12)           O/p    (12,)
show (17, 34)     O/p   (17, 34)
show (2, 3,7,5)    O/p   (2 3,7,5)

Comments