You can learn about Functions in Python Programs with Outputs helped you to understand the language better.
Python Programming – Modular Programming
One way to improve the structure of a system is to break down the original problem to be solved into independent tasks called modules, and then execute each module in a pre-defined sequence. The result¬ing design would then contain a network of modules.
- Breaking down a problem into smaller pieces (modules) allows you to focus more easily on a particular piece of the problem without worrying about the overall problem.
In this way, the various tasks can be solved easily. They are more manageable since each module performs a very specific function. Typically, you can code each module independently of the others and test or debug each module separately. Once all modules are working properly, you can link them together by writing the coordinating module (gen¬erally called the root segment or the main module). The coordinating code activates the various modules in a pre-determined sequence (See Figure 10.2). Note that in this Figure, the resulting problem consists of five modules: the coordinating module and the four modules A, B, C, and D. It is, of course, conceivable that a particular module itself might be broken down into further sub-modules, down the line.
- By breaking down a problem into independent modules, a system designer in charge of a very complex problem can easily assign various members of a team the responsibility for developing one or more modules.
Such modules can be run and tested independently of one another. Making one change in one module is a very local intervention that does not require an understanding of all other modules.
In a modular environment, each module can be specified again as a sequence of smaller modules describing what is to be done at increasing levels of detail. The technique of expanding a problem-solving plan into several levels of detailed sub-plans and presenting the system structure as a hierarchy of tasks is also called top-down design. The hierarchy chart, also called a structured diagram, is a useful tool for illustrating module relationships and hierarchies. Figure 10.3 shows a hierarchy chart for the problem shown in Figure 10.2.
- A subroutine can also reduce the code required for writing tasks that are to be performed repeatedly at different places in a program.
In summary, modularization helps the problem solver to write better-structured solutions that are more compact and thus easier to work with and more readable.
In industry, the problems that are to be solved with the help of computers need thousands of lines of code. The importance of splitting a problem into a series of self-contained modules then becomes important. A module should not exceed about 100 or so lines and should preferably be short enough to fit on a single page or two.
Advantages of Modular Design
a. Rather than focusing on the entire problem at hand, a module typically focuses on one relatively small portion of the problem.
b. Since the module is small, it is simpler to understand it as a unit of code. It is therefore easier to test and debug, especially if its purpose is clearly defined and documented.
c. Program maintenance becomes much easier because the modules that are likely to be affected are quickly identified.
d. In a very large project, several programmers may be working on a single problem. Using a modular approach, each programmer can be given a specific set of modules to work on. This enables the whole project to be completed faster.
e. More experienced programmers can be given a more complex module to write, and the junior programmers can work on simpler modules.
f. Modules can be tested independently, thereby shortening the time taken to get the whole project working.
g. If a programmer leaves a project, it is easier for someone else to take over a set of self-contained modules.
h. A large project becomes easier to monitor as well as to control.