Function in python and types

Function in python and types

Function in Python


Function is a block of code used to perform some specific task that can be used from anywhere and any number of time as per requirement .Function provide better modularity for your application and a high degree of code reusability .Python gives you many built-in-functions like print(),input(),range(),etc.and you can also define your own functions in python .These functions are called user-defined-functions.

 

Defining a function in python

You can also define your own functions as per your requirement. The syntax for defining a function is as following:

def function name(parameter(s)):

                      statement(s)

Parameters are optional i.e. you may define parameters or may skip as per requirement.

 

Calling a function in python

Once a function is defined you have to call the function to execute statement(s) in the function body. The syntax for calling a function is as following:

function name(parameter(s))

Parameters will be passed only if defined in function definition

 

Program for demonstration of function

def print_messages():

                 print(“Hello“)

                 print(“Hii“)

                 print(“Bye“) 

 print_messages()

print_messages()

print(“Welcome”) 

print_messages()

print(“mad4python“)

print_messages()

print_messages()

print_messages()

 

Output

Hello

Hii

Bye

Hello

Hii

Bye

welcome

Hello

Hii

Bye

mad4python

Hello

Hii

Bye

Hello

Hii

Bye

Hello

Hii

Bye

 

Types of function based on returning a value and parameter passing

Functions can be categorized into four categories based on returning a value and parameter passing as following:

1. Function not returning a value and no parameter passing.

2. Function not returning a value but parameter passing

3. Function returning a value but no parameter passing.

4. returning value and parameter passing

Leave a Reply

Your email address will not be published. Required fields are marked *