Functions

M3-R5.1 · Chapter 6: Functions · 5 min read

Python Functions

1. Introduction to Functions

A function is a reusable block of code designed to perform a specific task. Functions help break down complex programs into smaller, manageable parts, improving readability and efficiency.

                                                                                                                     OR

In Python, a function is a block of reusable code that performs a specific task. You define a function once, and then you can call (use) it anywhere in your code.

                                                                              OR

A function is a block of code which only runs when it is called. You can pass data, known as parameters, into a function. A function can return data as a result. 

Key points about functions:

  • A function is defined once and can be called multiple times
  • Functions help divide a large program into smaller, manageable sub-problems
  • They make code more readable, reusable, and easier to debug
  • Python has built-in functions like print(), len(), etc., and also allows creating user-defined functions

2. Advantages of Using Functions

1.  Program development made easy and fast – Work can be divided among project members, so implementation can be completed faster.

2.  Program testing becomes easy – Easy to locate and isolate a faulty function for further investigation.

3.  Code sharing becomes possible – A function written once may be reused by many other programs; a programmer can use functions written by others instead of starting from scratch.

4.  Code re-usability increases – A function avoids rewriting the same block of code that is needed in two or more places in a program, especially when the code is long or complicated.

5.  Increases program readability – The length of the source program can be reduced by calling functions at appropriate places, making the program more readable.

6.  Function facilitates procedural abstraction – Once a function is written, the programmer only needs to know how to invoke it, not how it is coded internally.

7.  Functions facilitate the factoring of code – A function can be called inside another function, enabling code decomposition.

3. Types of Functions in Python

Type

Description

Examples

Built-in Functions

Pre-defined functions, always available without importing anything.

print(), len(), type(), int(), input(), range(), abs(), max(), min()

Functions in Modules

Predefined in specific modules; can only be used after importing the module.

math.sqrt(), math.ceil(), random.randint(), os.getcwd()

User-defined Functions

Created by the programmer to perform a specific task as needed by the program.

def greet(): ...   def calcSum(x,y): ...

Lambda Functions

Small anonymous functions defined using the lambda keyword for single expressions.

lambda x: x * 2   lambda a,b: a+b

3.1.Built In Functions

These functions are already built in the python library.

  • These are pre-defined functions and are always available for use
  • In order to use these functions , there is no need to import any module
  • We can directly use it , in our program.

                            Example: len()   ,  print()   ,  int()   , input()   , type()  , id() ,ord()