Ad Code



Python: FUNCTIONS | Built in | User Defined

If a group of statements is repeatedly required then it is not recommended to write these statements every time separately. We have to define these statements as a single unit and we can call that unit any number of times based on our requirement without rewriting. This unit is nothing but function.

The main advantage of functions is code Reusability.

Python supports 2 types of functions

1. Built in Functions

2. User Defined Functions

1. Built in Functions:

The functions which are coming along with Python software automatically,are called built 

in functions or pre defined functions



2. User Defined Functions:

The functions which are developed by programmer explicitly according to business requirements ,are called user defined functions.

Syntax to create user defined functions:

def function_name(parameters) :
 """ doc string"""
 return value

Eg 1: Write a function to print Hello

1) def wish(): 
2) print("Hello Good Morning") 
3) wish() 
4) wish() 
5) wish() 


Parameters are inputs to the function. If a function contains parameters,then at the time of calling,compulsory we should provide values otherwise,otherwise we will get error.

Eg: Write a function to take name of the student as input and print wish message by name.

1. def wish(name): 
2. print("Hello",name," Good Morning") 
3. wish("Durga") 
4. wish("Ravi") 
7. D:\Python_classes>py 
8. Hello Durga Good Morning 
9. Hello Ravi Good Morning 

Return Statement:

Function can take input values as parameters and executes business logic, and returns output to the caller with return statement.

Q. Write a function to accept 2 numbers as input and return sum.

1. def add(x,y): 
2. return x+y 
3. result=add(10,20) 
4. print("The sum is",result) 
5. print("The sum is",add(100,200)) 
8. D:\Python_classes>py 
9. The sum is 30 
10. The sum is 300 

Returning multiple values from a function:

In other languages like C,C++ and Java, function can return atmost one value. But in Python, a function can return any number of values.

Eg 1:

1) def sum_sub(a,b): 
2) sum=a+b 
3) sub=a-b 
4) return sum,sub 
5) x,y=sum_sub(100,50) 
6) print("The Sum is :",x) 
7) print("The Subtraction is :",y) 
9) Output
10) The Sum is : 150 
11) The Subtraction is : 50

Types of arguments

def f1(a,b):

a,b are formal arguments where as 10,20 are actual arguments

There are 4 types are actual arguments are allowed in Python.

1. positional arguments
2. keyword arguments
3. default arguments
4. Variable length arguments

1. positional arguments:

These are the arguments passed to function in correct positional order.

def sub(a,b):

The number of arguments and position of arguments must be matched. If we change the order then result may be changed.

If we change the number of arguments then we will get error.

2. keyword arguments:

We can pass argument values by keyword i.e by parameter name.


1. def wish(name,msg): 
2. print("Hello",name,msg) 
3. wish(name="Durga",msg="Good Morning") 
4. wish(msg="Good Morning",name="Durga") 
6. Output
7. Hello Durga Good Morning 
8. Hello Durga Good Morning 

Here the order of arguments is not important but number of arguments must be matched.

3. Default Arguments:

Sometimes we can provide default values for our positional arguments.


1) def wish(name="Guest"): 
2) print("Hello",name,"Good Morning") 
4) wish("Durga") 
5) wish() 
7) Output
8) Hello Durga Good Morning 
9) Hello Guest Good Morning

If we are not passing any name then only default value will be considered.

4. Variable length arguments:

Sometimes we can pass variable number of arguments to our function,such type of arguments are called variable length arguments.

We can declare a variable length argument with * symbol as follows

def f1(*n):

We can call this function by passing any number of arguments including zero number.

Internally all these values represented in the form of tuple.


1) def sum(*n): 
2) total=0 
3) for n1 in n: 
4) total=total+n1 
5) print("The Sum=",total) 
7) sum() 
8) sum(10) 
9) sum(10,20) 
10) sum(10,20,30,40) 
12) Output
13) The Sum= 0 
14) The Sum= 10 
15) The Sum= 30 
16) The Sum= 100

Recursive Functions

A function that calls itself is known as Recursive Function.


factorial(n)= n*factorial(n-1) 

The main advantages of recursive functions are:

  1. We can reduce length of the code and improves readability
  2. We can solve complex problems very easily.

Q. Write a Python Function to find factorial of given number with recursion.


1) def factorial(n): 
2) if n==0: 
3) result=1 
4) else: 
5) result=n*factorial(n-1) 
6) return result 
7) print("Factorial of 4 is :",factorial(4)) 
8) print("Factorial of 5 is :",factorial(5)) 
10) Output
11) Factorial of 4 is : 24 
12) Factorial of 5 is : 120 

Anonymous Functions:

Sometimes we can declare a function without any name,such type of nameless functions are called anonymous functions or lambda functions.

The main purpose of anonymous function is just for instant use(i.e for one time usage)

Normal Function:

We can define by using def keyword.
def squareIt(n):
return n*n

lambda Function:

We can define by using lambda keyword

lambda n:n*n

Syntax of lambda Function:

lambda argument_list : expression

filter() function:

We can use filter() function to filter values from the given sequence based on some condition.


where function argument is responsible to perform conditional check  sequence can be list or tuple or string.

1) def isEven(x): 
2) if x%2==0: 
3) return True 
4) else:
5) return False 
6) l=[0,5,10,15,20,25,30] 
7) l1=list(filter(isEven,l)) 
8) print(l1) #[0,10,20,30]

map() function:

For every element present in the given sequence,apply some functionality and generate new element with the required modification. For this requirement we should go for map() function.

Eg: For every element present in the list perform double and generate new list of doubles.



The function can be applied on each element of sequence and generates new sequence.

Eg: Without lambda

1) l=[1,2,3,4,5] 
2) def doubleIt(x): 
3) return 2*x 
4) l1=list(map(doubleIt,l)) 
5) print(l1) #[2, 4, 6, 8, 10]

reduce() function:

reduce() function reduces sequence of elements into a single element by applying the specified function.


reduce() function present in functools module and hence we should write import statement.


1) from functools import * 
2) l=[10,20,30,40,50] 
3) result=reduce(lambda x,y:x+y,l) 
4) print(result) # 150 

Function Aliasing:

For the existing function we can give another name, which is nothing but function aliasing. 


1) def wish(name): 
2) print("Good Morning:",name) 
4) greeting=wish 
5) print(id(wish)) 
6) print(id(greeting)) 
8) greeting('Durga') 
9) wish('Durga') 


Good Morning: Durga
Good Morning: Durga

Nested Functions:

We can declare a function inside another function, such type of functions are called Nested functions.


1) def outer(): 
2) print("outer function started") 
3) def inner(): 
4) print("inner function execution") 
5) print("outer function calling inner function") 
6) inner() 
7) outer() 
8) #inner() => NameError: name 'inner' is not defined 


outer function started
outer function calling inner function
inner function execution

In the above example inner() function is local to outer() function and hence it is not possible to call directly from outside of outer() function.

"Built In And User Defined Functions In Python"

"Python Built-In Functions"

"Python User-Defined Functions"

"Python Built In Search Functions"

"User Defined Function In Python Examples"

"What Is User Defined Functions In Python"

"Python Built-In Functions Definition"

"Python Built In Mean Function"