17: Modules
- Page ID
- 54226
\( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)
For larger programs, using a single source file for the entire program becomes more difficult. Fortunately, large programs can be split into multiple source files, each file can contain a subset of subroutines and/or functions. There must be a main or primary source file that includes the main program. The secondary file or files is referred to as a module or modules. Additionally, the modules can then be more easily used in other, different programs ensuring that the code can be easily re-used. This saves time and money by not re-writing routines. This section provides a description of the formatting requirements and an example of how to set-up the modules.
- 17.1: Module Declaration
- The secondary source file or module must be formatted in a specific manner.
- 17.2: Use Statement
- Once the module is defined, the routines from the module can be included by using the use statement.
- 17.3: Updated Compilation Commands
- For a large program that is split between multiple source files, the compilation process must be updated. The compilation process refers to the steps required to compile the program into a final executable file.
- 17.4: Module Example Program
- The following is an example program to compute the surface area and volume of a sphere.