Function is named sequence of statements that performs a required activity/computation. Best way to describe functions is that these are tools used to compartmentalise your code.
User defined functions
- A function is a block of organised, reusable code that is used to perform a single, related action.
- Function needs to be defined before its called.
- It runs when it is called.
- A function can return data as a result.
Function naming convention is same as that of variable naming conventions
- It can have alphabets, numbers and underscore
- It can not have special character except underscore
- It can not start wit number, it can start only with alphabets or underscore
Simple function definition
def first_function(): print("Inside first_function") first_function()
$ python3.6 funtions00.py Inside first_function
Empty parenthesis indicates that function does not take any input parameters.
print("*** Start of the program ***") def first_function(first_name,last_name,age): """ simple functions with parameters """ print('first_name : ', first_name) print('last_name : ', last_name) print('age : ', age) return first_name = 'Jason' last_name = 'Bourne' age1 = 27 first_function(first_name,last_name,age1) print("*** Second type of function ***") def second_function(first_name,last_name,age): print('first_name : ', first_name) print('last_name : ', last_name) print('age : ', age) return f_name = 'Jason' l_name = 'Bourne' second_function(f_name,l_name,27) print("*** Third type of function ***") def third_function(first_name,last_name,age=29): print('first_name : ', first_name) print('last_name : ', last_name) print('age : ', age) return f_name = 'Jason' l_name = 'Bourne' third_function(f_name,l_name) print("*** Fourth type of function ***") def fourth_function(first_name,last_name='two',age=29): print('first_name : ', first_name) print('last_name : ', last_name) print('age : ', age) return f_name = 'Jason' l_name = 'Bourne' age = 28 fourth_function(f_name,) print("*** End of the program ***")
- function arguments are positional in nature. i.e. you need to pass variables or values in the same sequence.
- Optional parameters should be kept at the end and these needs to be handled using default values in function header.
- We can use keyword parameters to have flexibility of using any sequence but in such cases, we need to define default value and also argument are named argument.
Output of above program is as below
$ python3.6 funtions03-passing\ arguments.py *** Start of the program *** first_name : Jason last_name : Bourne age : 27 *** Second type of function *** first_name : Jason last_name : Bourne age : 27 *** Third type of function *** first_name : Jason last_name : Bourne age : 29 *** Fourth type of function *** first_name : Jason last_name : two age : 29 *** End of the program ***
Statements inside a function will not be executed unless function is called by name
Important note for passing arguments to function
- list is always passed by reference
- other variables are passed by value
- Variables defined in function are local to that function
Types of function
Details seen above are user defined functions. In general functions can be of following types
- Built in functions
- Type conversion functions
- User defined functions
Let us look at other category of functions
Python provides large number of built-in functions which we can use without defining them. These functions address common problems faced by most of the developers.
Here is simple example. It has two functions, len(), it returns left of the argument that is passed and second is print(). Yes, print is a functions, in fact this is a major change between python 2 and 3.
print("*** Start of the program ***") length = len("techtrekking") print("length : ", length) print("*** End of the program ***")
$ python3.6 funtions01.py *** Start of the program *** length : 12 *** End of the program ***
Few examples of python built-in functions
|abs()||returns absolute value|
|max()||returns maximum value from array passed|
|type()||Returns the data type of the variable|
Type Conversion Functions
These functions can convert data type of variable however not all data types can be changed. e.g. You can convert int to float and vice versa without any issue however if you try to convert character data type having actual characters it will throw some error however this function can be used to convert data type of variable having numeric value but character data type.
>>> int(2.1) 2 >>> int(aa) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'aa' is not defined >>> int('10') 10 >>>
Return Values and return Statements
When python function is called, it will do some calculations and return the result of the calculations using return statement. However sometimes you might call a function only to display something and you dont need anything return, in such cases, you can skip teh return statement.
A function is an object that is able to accept some sort of input, possibly modify it, and return some sort of output.
In Python, a lambda function is a one-line shorthand for function.
A lambda function can take any number of arguments, but can only have one expression.
>>> mylambda =lambda a: a+10 >>> print(mylambda(11)) 21 >>>mylambda = lambda x, y : x * y >>>print(mylambda(5, 6)) 30 >>>mylambda = lambda x, y, z : x + y + x >>>print(mylambda(5, 6, 2))
Due to characteristics of lambda, it can not have default values and it can not handle complex logic