# Programming Fundamentals/Modular Programming
## Overview

Modular programming is a software design technique that emphasizes separating the functionality of a program into independent, interchangeable modules, such that each contains everything necessary to execute only one aspect of the desired functionality.

## Concept of Modularization

One of the most important concepts of programming is modularization- grouping lines of code together to create an interchangeable piece that can be used and removed from our program as needed. The original wording for this was a sub-program. Other names include: macro, sub-routine, procedure, module and function. Functions are important because they allow us to take large complicated programs and to divide them into smaller manageable pieces. Because the function is a smaller piece of the overall program, we can concentrate on what we want each piece (function) to do and perform tests and edit code. In this way, functions help to organize code. Generally, functions fall into two categories:
* Program Control – Functions used to simply sub-divide and control the program. These functions are unique to the program being written. Other programs may use similar functions, maybe even functions with the same name, but the content of the functions are almost always very different.
* Specific Task – Functions designed to be used with several programs. These functions perform a specific task and thus are usable in many different programs because the other programs also need to do the specific task. Specific task functions are sometimes referred to as building blocks. Because they are already coded and tested, we can use them with confidence to more efficiently write a large program.
The main program must establish the existence of functions used in that program. Depending on the programming language, there is a formal way to:
* define a function (its definition or the code it will execute)
* call a function
* declare a function (a prototype is a declaration to a compiler)
Note: Defining and calling functions are common activities across programming languages. Declaring functions with prototypes is specific to certain programming languages, including C and C++.
Program Control functions normally do not communicate information to each other but use a common area for variable storage. Specific Task functions are constructed so that data can be communicated between the calling program piece (which is usually another function) and the function being called. This ability to communicate data is what allows us to build a specific task function that may be used in many programs. The rules for how the data is communicated in and out of a function vary greatly by programming language, but the concept is the same. The data items passed (or communicated) are called parameters. Thus the wording: parameter passing. The four data communication options include:
* no communication in with no communication out
* no communication in with some communication out
* some communication in with some communication out
* some communication in with no communication out

### Program Control Function

The main program piece in many programming languages is a special function with the identifier name of main. Function control programs are unique to one program. "Main" is a special area of the program where code execution begins, where functions are called, and where the program's execution ends. It is often the first function defined in a program, and it is the area where many of the program's important settings are defined. These settings include items such as declaration of prototypes, listing global constants and variables, as well as many other technical items. The code to define the function main is provided; however, it is not prototyped or usually called like other functions within a program.

### Specific Task Function

We often have the need to perform a specific task that might be used in many programs. Specific task functions are designed to be used in many programs.
General layout of a function in a statically-typed language such as C++, C#, and Java:
General layout of a function in a dynamically typed language such as JavaScript and Python:
In some programming languages, functions have a set of braces used for identifying a group or block of statements or lines of code. Other languages use indenting or some type of begin and end statements to identify a code block. There are normally several lines of code within a function.
Programming languages will either have specific task functions defined before or after the main function, depending on coding conventions for the given language.
When you call a function you use its identifier name and a set of parentheses. You place any data items you are passing inside the parentheses. After our program is compiled and running, the lines of code in the main function are executed, and when it gets to the calling of a specific task function, the control of the program moves to the function and starts executing the lines of code in the function. When it’s done with the lines of code, it will return to the place in the program that called it (in our example the function main) and continue with the code in that function.

## Program Layout

Most programs have several items before the functions, including:
* Documentation – Most programs have a comment area at the start of the program with a variety of comments pertinent to the program.
* Include or import statements used to access standard library functions.
* Language-specific code such as namespace references or function prototypes.
* Global or module-level constants and variables, when required.

## Key Terms
### braces

    Used to identify a block of code in languages such as C++, C#, Java, and JavaScript.

### function

    What modules are called in many predominant programming languages of today.

### function call

    A function’s using or invoking of another function.

### function definition

    The code that defines what a function does.

### function prototype

    A function’s communications declaration to a compiler.

### identifier name

    The name given by the programmer to identify a function or other program items such as variables.

### modularization

    The ability to group some lines of code into a unit that can be included in our program.

### parameter passing

    How the data is communicated in to and out of a function.

### program control

    Functions used to subdivide and control the program.

### specific task

    Functions designed to be used with several programs.

## References

* cnx.org: Programming Fundamentals – A Modular Structured Approach using C++
* ↑ Wikipedia: Modular programming