Function in Python

Tue Apr 18, 2023

Function in Python

  • A function is a set of related statements that performs a specific task. Functions improves a program's clarity and readability and makes programming more efficient by reducing code duplication and breaking down complex task into more manageable small parts.
  • A function is a block of code that performs a specific task.
  • A function is a block of code which only runs when it is called.
  • A function is a block of organized, reusable code that is used to perform a single, related action.
  • A function is simply a “part” of code that you can use over and over again, rather than writing it out multiple times.
  • Functions enable programmers to break down or decompose a problem into smaller parts, each of which performs a particular task.
  • Functions also known as routines, sub-routines, methods, procedures and sub-programs.

Types of Function:

  1. Built - In or Library  Functions
  2. Functions in Module
  3. User - Defined Functions
1. Built - In or Library Functions

Built -in functions are the predefined functions that are already available in Python.

For Example:

input(), print(), int(), float(), eval(), max() and min()

abs(), type(), len(), round(), range()

2. Functions in Module
When a program become more lengthy and complex, there arises a need for the tasks to be spilt into smaller segments called modules.

When we break a program into modules, each module should contain functions that perform related tasks.

A module in Python is a file that contains a collection of related functions.

Functions of math module:

ceil(), floor(), pow(), sqrt(), fabs(), cos(), sin(), tan()

Functions of random module:

randrange(), randint(), random()

3. User - defined functions

A user defined function is created or defined by the def keyword, followed by the function name, parentheses () and colon (:)

Syntax of Python Function 

               def function_name (optional parameters ): 

                                Code of function 

               function_name(optional parameters values)

  • In Python every function defined by def keyword.
  • Function must be called/invoked to execute it code.
  • A function must be defined before the function calling.
  • Every function return a value using the return statement as the last statement of the function code.
  • If a function does not have any return statement then it will return None

The first line of function definition i.e. def function_name (optional parameters ): is called function header.

function_name (optional parameters values) statement is called function calling statement

For Example:

def show():
         print("Hello")
show()

Here def show(); is called function header, print("hello") is code of the function and show() is function calling statement.


S P SHARMA CLASSES

Launch your GraphyLaunch your Graphy
100K+ creators trust Graphy to teach online
𝕏
S P SHARMA CLASSES 2024 Privacy policy Terms of use Contact us Refund policy