Function with variable length argument

There might be a scenario where you need to pass more arguments than specified during the function definition. In this case, variable length arguments can be passed:

Syntax
def  function_name(arg, *var):
code block
return

Here, arg means normal argument which is passed to the function. The *var refers to the variable length argument. This is will be more clear through the example:

def variable_argument( var1, *vari): 
print "Out-put is",var1
for var in vari:
print var
variable_argument(60)
variable_argument(100,90,40,50,60)

In this case, we define a function which takes two arguments, where the second argument is the variable length argument. When we call the function for the first time, we pass only 60 as the value to the argument and the function takes it as the first argument. During our second call to the function, we pass five numbers, so the function takes them as variable length argument. Depending on the nature of arguments passed, the function either considers the first argument or the variable length argument. Finally, we could see a different output based on our passing of different values as shown here:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.206.112