Computer Science
Describe the different types of functions in Python using appropriate examples.
Python Functions
2 Likes
Answer
The different types of functions in Python are as follows :
1. Built-in functions — These are pre-defined functions and are always available for use. For example:
name = input("Enter your name: ")
name_length = len(name)
print("Length of your name:", name_length)
In the above example, print(), len(), input() etc. are built-in functions.
2. User defined functions — These are defined by programmer. For example:
def calculate_area(length, width):
area = length * width
return area
length = 5
width = 3
result = calculate_area(length, width)
print("Area of the rectangle:", result)
In the above example, calculate_area is a user defined function.
3. Functions defined in modules — These functions are pre-defined in particular modules and can only be used when corresponding module is imported. For example:
import math
num = 16
square_root = math.sqrt(num)
print("Square root of", num, ":", square_root)
In the above example, sqrt is a function defined in math module.
Answered By
2 Likes
Related Questions
Write a function called print_pattern() that takes integer number as argument and prints the following pattern if the input number is 3.
* ** ***
If input is 4, then it should print:
* ** ***
What is the utility of : (i) default arguments, (ii) keyword arguments ?
What is scope ? What is the scope resolving rule of Python ?
What is the difference between local and global variable ?