Basics of computer programming Part 2: Programming Languages
The next part of computer programming, this time I shift the focus to programming languages, the main, but far from the only tool of the trade.
Tools of the Trade (Part I): The programming Language
Ever since the very first days of computers people have used programming languages to program computes. There are many different programming languages that in turn use many concepts to describe the computer programs that can be written with them. In principle all those languages can all describe the same program, however the program text may look very different depending on the programming language. For simplicity's sake I will again group the types of programming languages into three groups that represent the majority of programming languages in active use today.
Imperative programming languages
Imperative programming languages are the oldest and most straight forward programming languages to understand. They are in essence a sequence of steps that the computer performs one after the other as they appear in the program text.
The programmer can influence the sequence of steps by using conditional statements such as if a = 5 then do x else do y
with x
and y
each being sequences of instructions.
Loops like while a < 10 do x
to repeat a sequence of instructions is another construct typical for imperative programming languages. Together with a few arithmetic and text
manipulation instructions the if
and while
statements make it possible to compute any computable result from any possible input.
That is to say that the programming language is turing complete. Turing completeness is the hallmark of every actual ‘programming’ language. This means that in terms of ‘functionality’
every turing complete language is equal to every other turing complete language. This does not mean to say that they will do what they where programmed to with the same amount of computing
recources or that a program in one language is equally difficult or easy to write than in a different language, it simply means that any program in one turing complete language can
be written in any other turing complete language. Examples for Imperative programming languages are C
ASM
(assembly) FORTRAN
COBOL
among others.
Object-oriented Languages
Object oriented languages where (and to a large extent still are) all the rage. Probably it’s most (in)famous representative being Java created by Sun Microsystems (later acquired by Oracle). The core Idea behind object oriented Programs is that Data and procedures using that data are grouped together into a single logical ‘entity’. This makes it possible to reason about the relationships between objects and helps to enforce different strategies for abstracting different parts of a software system. At It’s core it is still very much a procedural language, made up of statements that manipulate the data in the program. In a sense Object oriented languages add another set of concepts to imperative languages, without actually changing the core nature of the language, aka it consisting of statements, executed in order of appearance that alter data.
Functional Programming languages
Functional programs differ quite fundamentally from everything previously discussed and have seen quite a gain in popularity recently. To understand what functional languages are and why they are so different from the more traditional languages we have to look at what a Function is and how it works.
Functions are at their most basic a grouping of code that performs a task that seems coherent to the programmer. A good example for this kind of function is the ever present
print(“This is the text to print on the screen”)
function. It is a set of instructions that when executed, print the text inside the parenthesis on to the output
(most often a screen with modern computers, printers on much older ones) of the computer. print()
is a set of instructions that do a certain task that the programmer can use
to abstract over the details of how exactly to accomplish this. The most important thing about functions is their ability to take arguments and provide return values.
The argument in the case above is the ”This is the text to print on the screen”
and the function does not provide any return values as it’s job is to print something onto the output.
In this sense the print function, when compared to a mathematical function is impure. A function in the pure mathematical sense (somethig like $ f(x) = 2\cdot x + 5 $ will always return a result for any $ x $ for which the function is defined. A pure function so to speak has a clearly defined Domain (set of all possible inputs) , Image (set of all possible outputs) and always returns a result.
Looking at our print(“This is the text to print on the screen”)
statement, we can see that there is no such thing as a return value. The function clearly does some useful work, but does not ‘return’ the result of that work back to the program, like a mathematical function always would.
The print
function is a representative of a whole class of functions that have side effects, they do something besides returning a value that they have computed. The hallmark of functional programming is that in the normal case, functions do not have side effects, they simply compute an output from an input. This makes them easy to reason about, as there are no obscure changes of some global state. If a function is executed with the same input, you get the same output.
This still leaves us with the problem that if we actually want side effects, like with our print function, these need to be integrated into the functional paradigm in some way. The exact mechanisms of how to do this are called monads, monads however are a topic for it’s own article for I’d like to give them a proper introduction at some point.
—-
Conclusions
At the core of computer programming lies the programming language. It is how problems are expressed to the computer, and sometimes more importantly, to other humans. Programming languages on their own however only result in text that is quite similar to what you are reading now. Without tools and environments all this fancy text will not actually do anything, no fancy math, or graphics or weather predictions. The core set of tools to turn this text into something that actually executes on a computer will be the topic of the next installment.