1. Home
  2. /
  3. Modula
  4. /
  5. Modula-2 Array Sum

Modula-2 Array Sum - Modula Typing CST Test

Loading…

Modula-2 Array Sum — Modula Code

Sums elements of an integer array.

# modula2/array_sum.m2
MODULE ArraySum;
IMPORT InOut;
VAR arr: ARRAY 1..5 OF INTEGER := (1,2,3,4,5);
	sum, i: INTEGER;
BEGIN
	sum := 0;
	FOR i := 1 TO 5 DO
		sum := sum + arr[i];
	END;
	InOut.WriteInt(sum,0);
	InOut.WriteLn;
END ArraySum.

Modula Language Guide

Modula is a procedural programming language and modular systems language designed by Niklaus Wirth. It emphasizes strong typing, modularity, and simplicity, supporting the development of reliable, maintainable software systems.

Primary Use Cases

  • ▸Teaching structured and modular programming
  • ▸Systems and embedded programming
  • ▸Research in programming languages and compilers
  • ▸Developing reliable software with modular architecture
  • ▸Prototyping software with clear separation of concerns

Notable Features

  • ▸Module-based program structure
  • ▸Strong typing with explicit type declarations
  • ▸Separate compilation units for modularity
  • ▸Structured control statements (loops, conditionals)
  • ▸Explicit import/export mechanism for modules

Origin & Creator

Modula was created by Niklaus Wirth in 1975 at ETH Zurich as a successor to Pascal with modular programming support.

Industrial Note

Modula is preferred in contexts where strong type safety, modular design, and structured programming are valued, particularly in education and system programming.

Quick Explain

  • ▸Modula was designed to improve upon Pascal by introducing modules for encapsulation.
  • ▸Supports separate compilation of modules, enabling better code organization.
  • ▸Strong typing and strict type checking reduce runtime errors.
  • ▸Focuses on simplicity and clarity for teaching and systems programming.
  • ▸Used historically for operating systems, embedded systems, and educational purposes.

Core Features

  • ▸Procedural programming with modular design
  • ▸Strong type system with compile-time checking
  • ▸Modules with defined interfaces and implementations
  • ▸Control structures: IF, CASE, WHILE, FOR, REPEAT
  • ▸Procedures and functions with parameter passing

Learning Path

  • ▸Learn basic Modula syntax and procedures
  • ▸Understand modules and separate compilation
  • ▸Practice type-safe programming
  • ▸Implement modular projects
  • ▸Explore Modula-3 advanced features and system programming

Practical Examples

  • ▸Modular calculator with separate arithmetic modules
  • ▸File processing system with input/output modules
  • ▸Simple operating system kernel modules
  • ▸Data structure library with stack/queue modules
  • ▸Educational examples for teaching programming concepts

Comparisons

  • ▸Modula vs Pascal: Modula adds modules and separate compilation
  • ▸Modula vs C: Modula enforces stronger typing and modularity
  • ▸Modula vs Ada: Both support reliability; Ada is more feature-rich
  • ▸Modula vs Java: Modula procedural, Java object-oriented
  • ▸Modula vs Python: Python is dynamic, Modula is strongly typed and compiled

Strengths

  • ▸Encourages clean and maintainable code
  • ▸Modules enable code reuse and encapsulation
  • ▸Strong typing reduces runtime errors
  • ▸Suitable for teaching structured programming concepts
  • ▸Clear separation of interface and implementation

Limitations

  • ▸Not widely adopted in modern industry
  • ▸Limited libraries and tooling compared to modern languages
  • ▸Mostly of historical and educational interest
  • ▸Verbose syntax compared to contemporary languages
  • ▸Concurrency and modern paradigms not inherently supported

When NOT to Use

  • ▸Modern web or mobile development
  • ▸Projects requiring extensive libraries
  • ▸Rapid prototyping or scripting tasks
  • ▸High concurrency applications
  • ▸When community support is needed

Cheat Sheet

  • ▸MODULE Name; -> start module definition
  • ▸IMPORT ModuleName; -> import module
  • ▸PROCEDURE ProcName(...); -> define procedure
  • ▸BEGIN ... END -> main program or procedure body
  • ▸VAR x: INTEGER; -> declare variable

FAQ

  • ▸Is Modula free?
  • ▸Yes - compilers are often open-source or educational.
  • ▸Can Modula be used for modern development?
  • ▸Not commonly - mostly historical or educational.
  • ▸Does Modula support OOP?
  • ▸Modula-2 is procedural; Modula-3 adds limited OOP.
  • ▸How does Modula compare to Pascal?
  • ▸Modula introduces modules and separate compilation.
  • ▸Is Modula suitable for system programming?
  • ▸Yes - it was designed for modular and reliable system-level code.

30-Day Skill Plan

  • ▸Week 1: Basic syntax and control structures
  • ▸Week 2: Procedures and functions
  • ▸Week 3: Modules and interfaces
  • ▸Week 4: Data structures and arrays
  • ▸Week 5: System-level programming and separate compilation

Final Summary

  • ▸Modula is a strongly typed, modular procedural language.
  • ▸Designed by Niklaus Wirth for reliable and maintainable software.
  • ▸Focuses on modules, interfaces, and separate compilation.
  • ▸Historically used in education and system programming.
  • ▸Encourages clean code, modularity, and type safety.

Project Structure

  • ▸main.mod - main program entry
  • ▸module1.mod - first module implementation
  • ▸module1.int - interface file for module1
  • ▸module2.mod / module2.int - additional modules
  • ▸libs/ - optional library modules

Monetization

  • ▸Educational software
  • ▸Legacy system maintenance
  • ▸Specialized embedded applications
  • ▸Teaching programming principles
  • ▸Research in language design

Productivity Tips

  • ▸Use modular structure to simplify development
  • ▸Write clear interfaces for modules
  • ▸Separate compilation reduces build time
  • ▸Test modules independently
  • ▸Document all modules for maintainability

Basic Concepts

  • ▸Modules - encapsulate related procedures, types, and variables
  • ▸Procedures - executable code blocks
  • ▸Data types - strong typing with integer, real, boolean, arrays, records
  • ▸Interfaces - specify what a module exports
  • ▸Import statements - access other modules

Official Docs

  • ▸https://www.modula.org/
  • ▸https://en.wikipedia.org/wiki/Modula-2
  • ▸https://en.wikipedia.org/wiki/Modula-3

More Modula Typing Exercises

Simple Modula-2 ProgramSimple Modula-3 ProgramModula-2 Fibonacci SequenceModula-3 Factorial ProgramModula-3 While Loop ExampleModula-2 Conditional ExampleModula-3 Simple FunctionModula-2 Nested LoopsModula-3 Record Example

Practice Other Languages

CReactPythonC++RustTypeScriptKotlinPHPJavaC#RubyMqlCqlN1qlCypher