[Type A] Working with Functions – Chapter 3 Sumita Arora

|
Table of Contents

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

Solution: There are three types of functions in Python.

  1. Built-in Functions: Functions provided by Python.
  • Explanation: Built-in functions are part of Python’s standard library and are available for use without importing any modules. They perform common tasks and operations.
  • Available Functions: len(), print(), input(), etc.
  • Example Code:
Python
result = len("Hello")
print("Length of 'Hello':", result)
  1. Module Functions: Functions from external libraries/modules.
  • Explanation: Module functions are defined in external Python files known as modules. They extend Python’s functionality by providing additional features and capabilities.
  • Available Functions: math.sqrt(), random.randint(), etc.
  • Example Code:
Python
from math import sqrt

result = sqrt(25)
print("Square root of 25:", result)
  1. User-Defined Functions: Functions created by users.
  • Explanation: User-defined functions are defined by users to perform specific tasks as needed. They offer customization and abstraction, allowing users to encapsulate reusable logic.
  • Available Functions: Any function created by the user as needed.
  • Example Code:
Python
def greet(name):
    print("Hello,", name)

greet("Mohan")
An example of a user-defined function which greets the client with the set argument.

There are many more styles or types of functions in Python. You’ll be able to find more of them here or separately, soon. For reference, some of them are listed below.

  • void Function (non-fruitful function)
  • Function in Python with Arguments (non-fruitful function)
  • Function in Python with return type (fruitful function)
"Spread the light within, illuminate the hearts around you. Sharing is not just an action, but a sacred journey of connecting souls."~ Anonymous

Leave a Reply

Your email address will not be published. Required fields are marked *