KnowledgeBoat Logo

Computer Science

Describe the different styles of functions in Python using appropriate examples.

Python Functions

8 Likes

Answer

The different styles 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.

  1. 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.

  1. 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

5 Likes


Related Questions