Python Functions
DR. ARCHANA RAJE
Python-Functions
➢ A Python function is a block of organized, reusable code
that is used to perform a single, related action.
➢ Functions provide better modularity for your application
and a high degree of code reusing.
➢ A Python function may be invoked from any other function
by passing required data (called parameters or arguments).
➢ The called function returns its result back to the calling
environment.
Types of Python Functions:
Python provides the following types of functions −
➢ Built-in functions
➢ Functions defined in built-in modules
➢ User-defined functions
Defining a Function in Python
Here are simple rules to define a function in Python:
➢ Function blocks begin with the keyword def followed by the function name and
parentheses ( ( ) ).
➢ Any input parameters or arguments should be placed within these parentheses. You can
also define parameters inside these parentheses.
➢ The first statement of a function can be an optional statement; the documentation string of
the function or docstring.
➢ The code block within every function starts with a colon (:) and is indented.
➢ The statement return [expression] exits a function, optionally passing back an expression to
the caller. A return statement with no arguments is the same as return None.
Create a Function
Working of Python Function
Python Function with Arguments
The Return Statement
Python Recursive Function
In Python, we know that a function can call other functions. It is even possible for the function
to call itself. These types of construct are termed as recursive functions.
The following image shows the working of a recursive function called recurse.
Factorial of a number