Functions
Functions are a convenient way to divide your code into reusable block, make it more readable and save your time.Writing functions are way to define interface and which you can incorporate into your own or other program.
Python functions should start with def and every Python function returns a value,if the function ever executes a return statement, it will return that value, otherwise it will return None
1 2 3 4 |
def user_function_name(function_parameter_1,function_parameter_2): {function block} return {return value} {code outside the function as its outside the indentation} |
1 2 3 4 5 6 7 |
# Definition of function def first_function(): print "My First Function" return True # Function calling print first_function() |
Output of the above code will be :
1 2 |
My First Function True |
Execution will follow like this :
You can use following types of arguments:
-
i. Required arguments
ii. Keyword arguments
iii. Default arguments
iv. Variable-length arguments
Required arguments User need to call this function with correct order. Number of argument while calling should be same as function definition.User will get error if number of argument doesn’t match.
1 2 3 4 5 6 |
# Function definition def greeting_function( name ): "this function will accept string as argument and that will store in name variable" return "How are you %s"%name # calling function print greeting_function('Sachin') |
Function should be called with one argument.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
>>> print greeting_function("Sachin") How are you Sachin >>> print greeting_function() Traceback (most recent call last): File "<pyshell#33>", line 1, in <module> print greeting_function() TypeError: greeting_function() takes exactly 1 argument (0 given) >>> print greeting_function("Master","Sachin") Traceback (most recent call last): File "<pyshell#34>", line 1, in <module> print greeting_function("Master","Sachin") TypeError: greeting_function() takes exactly 1 argument (2 given) >>> |
Keyword arguments are come into picture while calling of function.Arguments will be defined in key-value pair. In Keyword argument order does not matter.
1 2 3 4 5 6 7 |
# Function definition def greeting_function( f_name, l_name ): "this function will accept first and last name as argument" return "How are you %s"%(f_name + " "+l_name) # calling function print greeting_function(f_name='Reetesh',l_name='Nigam') print greeting_function(l_name='Nigam',f_name='Reetesh') |
Both function will print same output on the screen
1 2 |
How are you Reetesh Nigam How are you Reetesh Nigam |
Default arguments We provide default value in function declaration itself. If user does not provide any value to default parameter while calling function, it will take the default value.
1 2 3 4 5 6 7 |
# Function definition def greeting_function( initial='Mr', f_name ): "this function will accept first and last name as argument" return "How are you %s"%(initial + " "+f_name) # calling function print greeting_function(initial='Miss',f_name='X') print greeting_function(f_name='XYZ') |
Both function will print same output on the screen
1 2 |
How are you Miss X How are you Mr XYZ |
Variable-length arguments When we do not know how many argument will be required at the time of calling, then we use Variable length argument. Argument must start with an asterisk(*). All non keyword argument will be stored in that variable as tuple.If calling function does not provide any value then argument will be an empty tuple.
1 2 3 4 5 6 7 |
# Function definition def greeting_function( *name ): "All the calling function function argument will be stored in name variable as tuple" return "How are you %s"%(" ".join(name)) # calling function print greeting_function('Mr','X','Y') print greeting_function() |
Output of the function will be
1 2 |
'How are you Mr X Y' 'How are you' |