Main Page About Template

Programming techniques

This page describes several programming techniques used in professional programming.

Programming constructs

There are three main programming constructs

Sequencing

Code is executed line by line from the top to the bottom.

Iteration

The same code is run multiple times. If the code is repeated a known number of times, this is called count controlled iteration. If not, then it is called condition controlled iteration.

Selection/ branching

Certain lines of code are only executed if a condition is met.

Recursion

Recursion is where a subroutine calls itself. Here is a visual to explain recursion:

Recursion is used when the depth of a problem is unknown. This can be useful for searching trees, querying databases or even mathematical functions. For example:

This function calculates the factorial of a number using recursion. It does this by calling itself until the terminating condition is met. A terminating condition is the condition that stops the recursion.

Global and local variables

Global variables are variables that can be referenced or edited anywhere in a program.

Local variables are variables that can only be referenced or edited within a certain code block.

Modularity

Modular programming is the process of splitting large or complex programs into smaller, self-contained modules. A modular design makes it easy to divide tasks between a team, test or modify parts of the program individually, and improves the reusability of components – once a module has been tested, it can be used again.

By reference and by value

When parameters are passed into a function or procedure, they can be passed by value or by reference. Passing a parameter by value makes a copy of the value to pass into the function, whereas passing by reference gives the address of the parameter.

Use of IDEs to develop and debug programs

An IDE (integrated development environment) is a program used to develop and debug code. Some examples include VS-Code, VS-Studio, IDLE, Vim, Atom, SublimeText, and Eclipse.