Programs and Modules#

Introduction#

This lecture introduces computer programs and the concept of modules. A program is a set of instructions that a computer follows to perform a specific set of tasks. Program usually consist of one or more modules that are used to solve specific problems or accomplish particular tasks. On the other hand, module are a collection of related functions, variables, and other types of data organized with some intent, e.g., a module that provides types and functions to work with probability; modules organize code and make it easier to reuse and maintain.

Modules can be imported and used in many programs, which makes it easier to reuse code and avoid repeating the same functionality in multiple programs. This helps to make programs more efficient and easier to maintain. For example, imagine you have a program that needs to perform mathematical calculations. Instead of writing all of the necessary code from scratch, you could create a module containing functions for performing the calculations and importing that into your program. This way, you can reuse the code in the module in multiple programs, saving time and making it easier to maintain the code.


Programs#

A computer program is a set of instructions that tells a computer what to do. It is written in a programming language, such as Julia, Python, and the C-programming language, and can be compiled into machine code that a computer can execute.

The structure of a computer program typically includes the following elements:

  • Input: This is the data that the program processes. It can come from various sources, such as a user inputting data through a keyboard or the program reading data from a file.

  • Output: This is the result of the program’s processing. It can be displayed to the user on a screen or written in a file.

  • Algorithm: This is the set of steps that the program follows to solve a problem or accomplish a task. The algorithm defines how the input is transformed into the output.

  • Control flow: This is the order in which the program executes its instructions. It can include loops and conditional statements, allowing the program to make decisions based on certain conditions.

  • Data structures are the structures used to store and organize data within the program. Examples include arrays, lists, and dictionaries

  • Functions: These are blocks of code that perform a specific task and can be called from other parts of the program. Functions can take arguments (input) and return a value (output).

Overall, the structure of a computer program is designed to take in data, process it in some way, and produce output. The specific details of the structure will depend on the problem or task the program is trying to solve.

Anatomy of an executable program#

The structure of programs in modern languages such as Julia and Python, and older foundational languages such as the C-programming language are remarkably conserved. Let’s look at a skeleton of an executable program in various languages and compare and contrast the structure:

# 0. copyright/licensing information
# 1. import functions from external modules here
# ...

# 2. include your custom functions here
# ...

# main
function main()
    # 3. do stuff here ...
end

# call main 
main()
# 0. copyright/licensing information
# 1. import functions from external modules here
# ...

# 2. include your custom functions here
# ...

# main
def main():
    # do stuff here ...

if __name__ == "__main__":
    main()
/* 0 copyright/licensing */
/* 1 includes */
/* 2 defines */
/* 3 global variable declarations */
/* 4 function prototypes */

/* main */
int main(int argc, char *argv[]) {
    /* 5 command-line parsing */
    /* do stuff ... */
    return 0;
}

/* 6 function declarations */

Let’s develop an executable script in Julia to compute the factorial \(n!\) (Example 11):

Modules#

A module is a collection of related functions, variables, and other types of data that can be imported and used in other programs. Modules are used to organize code and make it easier to reuse and maintain.

Julia#

Modules in Julia are defined using the module keyword, followed by the name of the module and a block of code that defines the functions and variables that make up the module. To use a module in a Julia program, you can use the using keyword followed by the name of the module.

Modules in Julia are delimited syntactically inside module NameOfModule ... end, and have the following features:

  • Modules are separate namespaces, each introducing a new global scope. This is useful, because it allows the same name to be used for different functions or global variables without conflict, as long as they are in separate modules.

  • Modules have facilities for detailed namespace management: each defines a set of names it exports, and can import names from other modules with using and import (we explain these below).

  • Modules can be precompiled for faster loading, and contain code for runtime initialization.

Let’s look at a Julia module:

module MyExampleModule

    # module body: include files, define types or functions, etc.
    # export: make data, e.g., types or function visible using the export keyword

end

Python#

A Python module is a file containing Python definitions and statements. The file name is the module name with the suffix .py added to the filename. Other Python programs can import Python modules, and the functions or types defined in the module can be used by the importing program:

def multiply(x, y):
    return x * y

if __name__ == '__main__':
    result = multiply(3, 4)
    print(result) # prints 12

The if name == ‘main’: block at the bottom allows you to test the function by running the module as a script. For example, you could save the above code to a file named mymodule.py, then run it from the command line like this:

python mymodule.py

Of course, you can also import this module in another python script or program and use your implementation of the multiply function using the import keyword:

import mymodule
result = mymodule.multiply(5, 6)
print(result) # prints 30

Are there differences between Julia and Python modules?#

Modules in Python and Julia offer a mechanism to organize and reuse code, but the two have minor syntactical and semantic differences. A Python module is a file containing Python definitions and statements, which can be imported and used by other Python scripts. Julia modules are similar because they also organize code and data, which can be imported into another Julia script. However, there are some subtle differences in how modules are defined and imported in Julia.

  • Privacy: One key difference is that in Julia, all the functions, types, and variables defined inside a module are private by default, unlike python, where everything described inside the module is public and can be imported and used. Julia has the export keyword to make the functions, types, and variables inside the module public and accessible from the outside.

  • Management: Another difference is that Julia has a concept of a package, a collection of modules and external libraries that can be easily installed and imported using a single unified package management system. Julia’s package manager Pkg allows you to install, update, and manage packages and their dependencies, so you can easily use and share code between different projects. However, python has several package installers/managers, e.g., the package installer for python (pip) or conda; thus, while the package ecosystem in Python is vibrant, it can seem fractured and overly complicated.


Summary#

This lecture introduced computer programs and modules. A program is a set of instructions that a computer follows to perform a specific set of tasks. Program usually consist of one or more modules that are used to solve specific problems or accomplish particular tasks. On the other hand, module are a collection of related functions, variables, and other types of data organized with some intent, e.g., a module that provides types and functions to work with probability; modules organize code and make it easier to reuse and maintain.

In summary, programs are sets of instructions that are used to solve specific problems or accomplish specific tasks while modules are used to organize code and make it easier to reuse.