Types of Sub Programs in Python
They are very important. These are called as Functions.
1. Built-in Functions
- Python provides a lot of built-in functions that are available to use without the need for additional definitions.
- These functions cover a wide range of tasks and are an integral part of the Python language.
Examples
print(), len(), input(), range(), and max()
Taken from python.org
Built-in Functions in Python Example
1. len function in python
You can find the length of a string using the len() function in python
Example
str = “Hello World!”
print(len(str))
Output
12
2. User-Defined Functions
- User-defined functions (sub programs) are functions created by the programmer to perform specific tasks.
- They are defined using the def keyword and can have parameters and return values.
- User defined functions allow you to encapsulate and reuse code.
5 main Advantages of User Defined Functions
- User defined functions allow you to break down a complex program into smaller, manageable modules. Each function can be designed to perform a specific task, making the code easier to understand and maintain.
- Functions can be reused across different parts of a program or even in different programs. So that reusability is another advantage of user defined functions
- Debugging is very easy. When a bug or error occurs in a program, by having well-structured functions can simplify the debugging process.
- Functions can accept parameters (arguments), allowing you to customize their behavior based on input values.
- Functions have their own local scope, which means variables defined within a function are typically limited to that function’s scope.