Arguments or parameters are the values that are passed by the caller function to the called function at the time of invocation based on the definition of called function. The number, order and data type of arguments that a function can accept is depends upon the formal arguments specified at the header of its definition. A function may take 0 to many number of arguments. All the arguments that a function can accept appear inside the parenthesis of its header separated each other with a comma.
Types of arguments:
1. Formal arguments: arguments appear in the header of function definition
2. Actual arguments: arguments supplied by the caller function at the time of invocation
The identifiers of actual arguments can be same as formal arguments, but those are treated as different variables since separate memory area will be allocated for each function in RAM. The formal arguments are considered as local variables of a function which is being called.
The caller of a function must aware the definition of it before supplying arguments to it. Incorrect number or order or type of argument may cause compile error or unexpected result.
#Example
def dispname(fname, lname):
print(fname + ” ” + lname)
dispname(“best”, “computers”)
dispname(“best”)
Multiple Arguments, *args
When you want to pass many number of arguments to a function simply you can use * with single identifier in its definition.
#Example
def dispname(*names):
print(“The younger son of Dasaradha is “, names[3])
dispname(“ram”, “lakshman”, “bharath”, “shatrughn”)
dispname(“best”)
Default value
When a default value is specified in the function definition that value will be used for executing function definition if the caller is not supplied any value.
#Example
def showCountry(country = “India”):
print(“I am from ” + country)
showCountry(“Sweden”)
showCountry(“America”)
showCountry()
showCountry(“Brazil”)
Keyword Argument
Keyword arguments help to supply arguments in any order other than specified in the function definition.
#Example
def ShowName(child3, child2, child1):
print(“The third child is ” + child3)
ShowName(child1 = “Ram”, child2 = “Lakshman”, child3 = “Bharath”)
Multiple Keyword Arguments
You can also supply multiple keyword arguments to a single argument in the function definition using ** before identifier.
#Example
def kidname(**kid):
print(“His last name is ” + kid[“lname”])
kidname(fname = “best”, lname = “computers”)
Sequence as an Argument
A function can accept any type of sequences of python as argument.
You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.
#Example
def fruitname(food):
if(food==fDct):
for x in fDct:
print(fDct[x])
else:
for x in food:
print(x)
print()
fList = [“apple”, “banana”, “cherry”]
fTuple = (“apple”, “banana”, “cherry”)
fSet = {“apple”, “banana”, “cherry”}
fDct = {“fruit1″:”apple”, “fruit2″:”banana”, “fruit3″:”cherry”}
fruitname(fList)
fruitname(fTuple)
fruitname(fSet)
fruitname(fDct)