Functions in python

A function is a group of statements which performs a specific task. A function is group of codes which runs when it is called. A function passes data which is known as a parameters. Function is defined using the keyword ‘def’. In a program function is called using the function name which is followed by parenthesis having appropriate parameters. In the following program name of the function is ‘function_name’, which is defined using the keyword ‘def’. Function is executed when function is called by the function name ‘function_name’ followed by parenthesis.

def function_name():

print("Hello world ")

function_name()

 Output :

 Hello world

parameters and arguments

A function has its function name as well as parameters. Parameters are inside the parenthesis which are after the function name. Any function will take any number of parameters.

When a function is called using its function name then the parameters are called arguments. So parameters or arguments are the same thing.

In the following program ‘mango’,’strawberry’ and ‘apple’ are the arguments for the function  while ‘fruit’ is the parameter. The function is called using the parameter ‘fruit’.

def function_name(fruit):

print(" Sweet " + fruit )

function_name("mango")

function_name("strawberry")

function_name("apple")

 Output :

 Sweet mango
 Sweet strawberry
 Sweet apple

Arbitrary, keyword and arbitrary keyword arguments

If there is an unknown number of arguments which is passed to the function, then a ‘*’ needs to be placed in the function definition parameter. The function is receiving arguments in tuple. In the following program a ‘*’ is placed in fruits, thus not giving an exact number of parameter. When the function ‘function_name’ is called the arguments are passed in tuple, starting with ‘0’. Here fruits [1] is used to denote ‘Apple’. This is the example of arbitrary arguments.

def function_name(*fruits):

print("The sweetest fruit is " + fruits[1])

function_name("Banana", "Apple", "Orange")

 Output :

 The sweetest fruit is Apple

Keyword arguments are used to avoid the specific ordering of the arguments. In the following program keyword used is ‘fruit1′,’fruit2’ and ‘fruit3’, so the particular argument can be called by using the specific keyword.

def function_name(fruit3, fruit1, fruit2):

print("The sweetest fruit is " + fruit2)

function_name(fruit1 = "Orange", fruit2 = "Apple", fruit3 = "Banana")

 Output :

 The sweetest fruit is Apple

Arbitrary keyword arguments are used if the number of keyword arguments is not known. A ‘**’ is used before the parameter name in the function definition. In the following example function has a keyword arbitrary arguments of ‘fruit’ and ‘vegetable’. ‘item’ has ‘**’ preceding it in the function parameter name.

def function_name(**item):

print("Today his food item was " + item["Vegetable"])

function_name(fruit = "Apple", Vegetable = "Potato")

 Output:

 Today his food item was Potato

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments