Python Functions Tutorial | Types of function in Python 3

Preview
You must first complete Python Functions Tutorial | Using Functions in Python 3 before viewing this Lesson

Types of function in Python 3

Python functions are categorized into three types:

1. Built-in Functions

They are predefined functions provided by Python. They are also known as library functions. These functions are basically used to perform basic input/output operations and many other operations which can’t be performed by any user defined function.

Built in functions can be called in any program The most commonly used built in functions are input(), id(), type(), int(), float(), bool(), len()etc.

2. Functions inside modules

These functions are defined within modules and can be used only when we import a particular module in our program.

For example, to use pre-defined functions sqrt(), we need to import the module math as it contains definition of sqrt() function in it.

3. User defined function

It is a function defined by a programmer depending upon his own requirement. User defined functions are mainly defined for those operations which are repeatedly required in a program.

A user defined function is a self contained group of statements which can perform a specific task. Once a user defined function has been defined, it can be called within another function in the same program or another program.

A user defined function can also take values from other functions and can also return some value.

There are two terms associated with functions:

Calling function: The function which calls another function within its body is known as calling function.

Called function: The function which is called by another function is known as called function. It is the called function which contains the actual set of statements which would be executed when function call is made.


Types of user defined function

There are four types of user defined function in Python

(i) Function without arguments and without return value

This type of function has no arguments so it doesn’t receive any values from the calling function. So In we don’t specify anything within the parenthesis written after the function name. This type of function doesn’t contain return statement.

Features:

  • Function contains no arguments.
  • Calling function does not receive any value from the function.
  • There is simple passing of control from calling function to called function and vice versa but there is no passing of values between them.
  • Everything is performed within the body of function.
Program of function without arguments and without return value.
def sum():
     a=int(input(“Enter first number=”))
     b=int(input(“Enter second number=”))
     c=a+b
     print (“sum=”,c)
sum()   #Function sum() is called here
Output
Enter first number=2
Enter second number=3
sum= 5

Description of Above Program

In the above program, we have defined a user define function named sum() which reads values of two  variables, adds them and shows their addition. This function has been called  as sum().


(ii) Function with arguments and without return value

In this type of function, we need to specify arguments within the parenthesis written after the function name. Arguments are generally variables declared individually within the parenthesis.

Arguments within the parenthesis while defining  a function are known as formal arguments. Arguments within the parenthesis while calling the user defined function are known as actual arguments.

The formal arguments receive values from actual arguments. This function doesn’t return any value so we don’t write return statement.

Features:

  • Function contains arguments.
  • Calling function does not receive any value from the called function.
  • There is passing of values from calling function to user defined function.
  • Input is received from calling function and only manipulation is performed within the user defined function.
Program of function with arguments and without return value.
def sum(a,b):
     c=a+b
     print (“sum=”,c)
sum(10,5)
Output
sum= 15

Description of Above Program

In the above program, we have defined a user defined function named sum() which has two formal arguments namely a and b. It performs addition of a and b and shows their addition.

This function has been called  as sum(10,5). 10 and 5 are actual arguments. 10 will go to a and 5 will be go to b so their sum 15 will be shown in the output.



(iii) Function with arguments and with return value

 In this category of function, we need to specify formal arguments within the parenthesis written after the function name.

In this type of function, input is generally received from calling function and some manipulation is performed within the user defined function and result of manipulation is returned back to the calling function.

While calling such type of function, we need to assign function calling statement to some variable or we can directly write function calling statement within the print() function.

Features:

  • Function contains arguments.
  • we must need to write return statement.
  • There is a two-way communication of values between calling function as well as called function i.e. values are transferred from calling function to called function and again some value is returned back to the calling function.
  • At a time only one value can be returned by this type of function.
  • Input is received from calling function and after manipulation within the user defined function, result is returned back to the calling function.
Program of function with arguments and with return value.
def sum(a,b ):
     c=a+b
     return(c)
s=sum(3,5)
print(“sum of 3 and 5 is”,s)
print(“sum of 5 and 6 is “,sum(5,6))
Output
sum of 3 and 5 is 8
sum of 5 and 6 is 11

Description of Above Program

In the above program, we have defined a user defined function named sum() which has two formal arguments namely a and b. It performs addition of a and b and returns their addition by statement return(c).

This function has been called twice. Firstly as s=sum(3,5). 3 goes to a and 5 goes to b so their sum 8 is stored in variable c which will be returned and be saved in variable s.

Function sum() is called again as print(“sum of 5 and 6 is “,sum(5,6)) within print() function. 5 and 6 go to arguments a and b respecitively. Their sum is returned and shown as 11


(iv) Function without arguments and with return value

This category of function doesn’t contain any argument. Manipulations are performed within the body of user defined function and result of manipulation is returned back to the calling function.

There is one way communication in this function i.e. value is transferred from called function to calling function. While calling such type of function, we need to assign function calling statement to some variable or we can directly call function within the print function.

Features:

  • Function doesn’t contain arguments.
  • There is a one way communication of values between called function and calling function i.e. values are transferred from called function to calling function.
  • All manipulations are performed within the user defined function and result is returned back to the calling function.
Program of function without arguments and with return value.
def sum():
     a=int(input(“Enter first value=”))
     b=int(input(“Enter second value=”))
     c=a+b
     return(c)
s=sum()
print(“Sum=”,s)
print(“Sum=”,sum())
Output
Enter first value=2
Enter second value=3
Sum= 5
Enter first value=4
Enter second value=5
Sum= 9

Description of Above Program

In the above program, we have defined a user defined function named sum() It performs addition two values after reading them and result is returned back by statement return(c).

This function has been called twice. Firstly as s=sum(). Values of variables a and b are input  and their sum is stored in variable c which will be returned and be saved in variable s.

Function sum() is called again as print(“sum =”,sum()) within print() function. Values of variables a and b are input  again and their sum is stored in variable c which will be returned and shown.




Lesson tags: argument passing in python, passing arguments of a function in python, python function types, using return statement in python
Back to: Python Programming Tutorial