[FUNCTION]
M3-R5.1 · Chapter 6: Functions · 5 min read
[FUNCTION]
Definition: 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.
Advantages of Using functions:
- Code Reusability: We can write a function once and reuse it multiple times without rewriting the same code and We can call a function wherever you need it, without rewriting the same logic.
- Modularity: Functions break a large program into smaller, manageable pieces (modules), making it easier to understand and work with
- Improved Readability: Functions with descriptive names make code easier to read and understand
- Easy Debugging and Maintenance: If there's a bug in your program, it's easier to isolate and fix when the logic is encapsulated in functions
- Abstraction: We don’t need to understand the internal workings of a function to use it—just know what input it expects and what output it returns.
- Support for Recursion: Functions can call themselves, which is useful for solving problems like tree traversal or factorial calculation
- Increases program readability: The length of the source program can be reduced by using/calling functions at appropriate places so program become more readable.
Types of Functions:
There are three types of functions in python:
- Built-in Functions:
- Built-in Library Functions
- User-defined Functions
- Lambda Functions (Anonymous Functions)
- Recursive Functions
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()
Built-in Library Functions: These functions defined in particular modules. When we want to use these functions in program, we have to import the corresponding module of that function.
- These functions are pre-defined in particular modules
- These can only be used when the corresponding module is imported (like math.sqrt() from the math module or random.randint() from random)
Example: math module: sin() cos() sqrt() fabs() ceil() floor()
|
Sr |
Function |
Description |
Example |
Output |
|
1 |
sqrt( ) |
Returns the square root of a number |
>>>math.sqrt(49) |
7.0 |
|
2 |
ceil( ) |
Returns the upper integer |
>>>math.ceil(81.3) |
82 |
|
3 |
floor( ) |
Returns the lower integer |
>>>math.floor(81.3) |
81 |
|
4 |
pow( ) |
Calculate the power of a number |
>>>math.pow(2,3) |
8.0 |
|
5 |
fabs( ) |
Returns the absolute value of a number |
>>>math.fabs(-5.6) |
5.6 |
|
6 |
exp( ) |
Returns the e raised to the power i.e. e3 |
>>>math.exp(3) |
20.085536923187668 |
>>> sqrt(144) NameError: name ‘sqrt’ is not defined
>>> import math
>>> math.sqrt(144)
12.0
Example:
|
import calendar year = 2024 print(calendar.calendar(year)) |
import calendar year = 2024 month = 4 print(calendar.month(year,month)) |
random module: random() uniform() randrange() choice() randint() shuffle()
randint( ) function generates the random integer values including start and end values.
Syntax: randint(start, end)
ØIt has two parameters. Both parameters must have integer values.
Example:
import random
n=random.randint(3,7)
*The value of n will be 3 to 7.