Modular Programming and Databases: A Comprehensive Overview
Modular Programming
Modular programming is a programming paradigm that involves dividing a program into modules or applets to improve readability and manageability. This approach allows decomposing a problem into a set of mutually independent subproblems, which are easier to solve and can be treated separately. Modules can be tested independently, allowing for refinement before integration into the main program. They can also be stored and reused as needed. In modular programming, the main program coordinates calls to secondary modules, passing necessary data as parameters. Each module can contain its own data and call other modules or functions.
Modules
A module is a part of a program that solves one of the subproblems of the original complex problem. Each module has a well-defined task and may require interaction with other modules. A key element in representing a module is the subroutine: a set of instructions that perform a specific task. A module can be a segment, routine, subroutine, subalgorithm, or procedure defined within an algorithm. Modules have input and output, and decisions within a module can impact the overall flow, but jumps should return to the main program.
When is Modularization Useful?
Modularization is beneficial in two scenarios:
- When a set of instructions or a task needs to be performed multiple times.
- When a problem is complex or extensive, dividing it into modules simplifies the solution.
Benefits of Modular Programming
- Independent Modules: Modules can be modified without affecting others.
- Improved design and understanding of the underlying logic for both programmers and users.
- Simplified maintenance and modification of programs.
- Modular Development: Identifying main tasks and designing/testing individual procedures for each.
Data Lifetime
Variables have two types based on their declaration:
- Global: Remain active throughout the program.
- Local: Created within a function or procedure and disappear afterward.
- Formal Parameters: Special variables in a procedure that receive external values, declared in the procedure header.
- Parameters by Value: Receive values but cannot return them. A global variable sends its value to a local variable without further connection.
- Parameters by Variable: Both receive and return values. Global and local variables are linked, affecting each other.
Functions and Procedures
Procedures
These subprograms, or modules, perform specific tasks within a program and can have their own local variables, accessible only within the procedure.
Functions
Similar to procedures but return a single value of a simple data type. Functions are categorized as:
- Standard: Provided by high-level programming languages (arithmetic and alphabetical).
- User-Defined: Created by programmers for specific calculations, often used for repeated computations.
Standard Features
Provided by high-level languages and divided into arithmetic, alphabetical, and user-defined functions.
Similarities Between Procedures and Functions
- Both have a header and a body of instructions.
- Both are independent program units.
- Parameters, constants, and variables declared within are local.
- The number and types of actual and formal parameters must match (except integer parameters can be associated with real parameters).
Differences Between Procedures and Functions
- Procedures are called by a procedure call instruction; functions are called by name in an expression.
- Functions have a type identifier specifying the result type; procedures do not.
- Functions typically return a single value; procedures can return multiple or none.
- Procedures return values through variable parameters; functions return values by assigning a value to the function name.
Database Applications
A database is a systematically stored set of data within the same context for later use. Database Management Systems (DBMS) store and provide structured, quick access to this data.
Definition of Database
A database is a collection of organized and interrelated data in tables, used by information systems to produce information.
Features
- Logical and physical data independence.
- Minimal redundancy.
- Concurrent user access.
- Data integrity.
- Optimized complex queries.
- Secure access and audit.
- Backup and recovery.
- Standard programming language access.
A DBMS provides an interface between the database, users, and applications. It includes a data definition language, data manipulation language, and a query language.
Advantages of Databases
- Redundancy Control: Prevents multiple copies of the same data.
- Data Consistency: Reduces inconsistencies and ensures immediate availability to all users.
- Data Sharing: Authorized users can share the company’s database.
- Standard Maintenance: Easier to enforce data format standards.
- Improved Data Integrity: Enforces integrity constraints and rules.
- Improved Security: Protects against unauthorized access.
- Improved Data Accessibility: Query languages and report generators simplify data retrieval.
- Improved Productivity: Standard DBMS functions reduce programmer workload.
- Improved Maintenance: Better backup services and competitive advantages.
Disadvantages of Databases
- Complexity: DBMS can be complex with extensive functionality.
- Cost of additional equipment.
- Vulnerability to failures.
Database Types
Examples include MySQL, PostgreSQL, and Oracle.
Structured Query Language (SQL)
SQL is a declarative language for accessing and manipulating relational databases. It manages relational algebra and calculus, enabling data retrieval and modification.
Fundamentals
- Data Manipulation Language (DML): Accesses, creates, modifies, or deletes data.
- Data Definition Language (DDL): Creates, modifies, or deletes database objects and privileges.
- Transaction Control: Ensures data consistency through transactions.
- Session Control: Manages user session properties.