VarArgs parameters
VarArgs Parameters (Variable-Length Arguments)
In some instances you might need to pass more arguments than have already been specified. Going back to the function to redefine it can be a tedious process. Variable-Length arguments can be used instead. These are not specified in the function’s definition and an asterisk (*) is used to define such arguments.
Lets see what happens when we pass more than 3 arguments in the sum() function.
Example:
def sum(x,y,z):
print("sum of three nos :",x+y+z)
sum(5,10,15,20,25)
When the above code is executed, it produces the following result :
TypeError: sum() takes 3 positional arguments but 5 were given
Syntax - Variable-Length Arguments
def function_name(*args):
function_body
return_statement
Example:
def printnos (*nos):
for n in nos:
print(n)
return
# now invoking the printnos() function
print ('Printing two values')
printnos (1,2)
print ('Printing three values')
printnos (10,20,30)
Output:
Printing two values
1
2
Printing three values
10
20
30
Qus. 1 : Which part of the memory does the system store the parameter and local variables of a function call ?
- Heap
- Stack
- Uninitialized data segment
- None of the above
Programs
Python program to convert decimal into other number systems
python program that generates six random numbers in a sequence created with start stop step
Python program to illustrate the global variable
python program to create a function to calculate factorial value
Python program to find the sum of factorial upto n terms using function
python program to create a function for reverse the number
Python program to check number is Palindrome or not using function
Python program to create a function for calculating sum of digit
Python program to create recursive function to find factorial
python function to accept 2 number and return addition subtraction multiplication and division
python function to accept number of days and return week and days to its equivalent
python function to extract the day month year from given date
Python function to find the sum of all numbers between 100 and 500 which are divisible by 2
Python function to get two matrices and multiply them
python recursive function to find the sum of digits
python recursive function to print the reverse of number
Write a function with name dividebyfive which generate and prints a random integer number from the range 0 to 100 and then return True if the randomly generated number is divisible by 5, and False otherwise.