This blog post is meant for anyone that wants to get a better understanding over what a computer program actually is and how it works, on a beginner level.
Imagine you want to make some pancakes, but you don’t know exactly how. Naturally, you look up a recipe on the internet. That recipe is literally a series of steps that you need to follow in order to make the pancakes. This means that we could call it an algorithm.
According to the dictionary, the definition for algorithm is: “a process or set of rules to be followed in calculations or other problem-solving operations.” This fits the idea of a recipe. In order for you to make pancakes, you used a set of rules in order to solve a problem (your desire for pancakes).
A computer program is another type of algorithm, and the idea is basically the same. It is a set of instructions meant specifically for computers. When that set of instructions is executed, some data is processed in some way (it’s impossible to make a computer program that does not process any data whatsoever).
So, from a logical point of view, this is what a computer program is. However, this doesn’t exactly explain the whole picture. For this, we need to also understand how computers work.
How do computers work?
For a simplified explanations of how computers work, we could identify three big components that we use whenever we use a computer:
- The computer hardware
- The operating system
- The programs that we use
Computer Hardware
The Operating System
The Operating System (OS) is the computer program that manages all of the physical devices on a computer. Whether you have 2 or 4 sticks of RAM, whether you have an SSD and an HDD or two SSDs, the operating system is the one that makes sure that your computer’s components can interact with each other and work like a team.
The operating system is basically a middle man between us, the users, and the computers and every time we want to run a program, the OS is the one that loads the program into the RAM and then starts the execution. It does that by actually telling the CPU what to do.
Now you might be wondering what does it mean that the operating system is responsible with loading the program in the RAM. What is being loaded into the memory?
This brings us to today’s topic.
What is a computer program?
A computer program, as we discussed above, is a sequence of logical instructions that, when executed, performs an action. This was the description from a logical point of view.
For a computer, a program is a bunch of 1s and 0s, where different sequences of ones and zeroes represent certain instructions that can be executed by the computer. Every single program that we run on a computer of any kind is just a huge sequence of 1s and 0s.
But how do we get that sequence? Do we have to write it ourselves?
Fortunately, the answer to that last question is NO.
In order to create a computer program, we use a programming language. A programming language is a pre-defined set of instructions that can be used to describe a program in a logical form. The instructions are usually pretty simple by themselves, but they can be pieced together to form complex programs that solve difficult tasks.
Let’s take a small example, to get a better understanding:
print("Hello World!")
This code is a single instruction written in Python, a really simple and popular programming language (technically it is a scripting language, but for simplicity’s sake, let’s not go into further details).
The instruction print is used to display some text when running the program from a Terminal or Command Line Interface (CLI).
To be more precise, print is a function. It is a piece of code that does something with some data that we pass to it. When we pass some data to a function for it to do something with it, we call that a parameter and we use a set of parentheses to encapsulate the parameters that we pass to a function. In this case, our parameter is the string “Hello World!”. Thus, if we were to execute this program, knowing what the ‘print’ function does, it would simply display the text “Hello World!” in the Terminal.
As a side note, the “Hello World!” program is the first program everyone writes when they first start to learn a new programming language, even if you already have prior knowledge of other programming language.
Now let’s make the program a bit more complicated:
print("Hello World!")
s = input()
print(s)
This new program is a bit more complex.
In the previous paragraphs we mentioned the fact that a program is a set of instructions that, when executed, does something. To add to that definition, the instructions need and are always executed sequentially.
In the case of our program, the first instruction that is executed is the one on line 1. Then, the instruction on line 2 is executed and then the one on line 3. This is important.
There are multiple things happening at once on line 2. First, we create a variable called ‘s’. Then, we assign a value to ‘s’, using the ‘=’ operator. I know that in math the ‘=’ sign is used to symbolize equality, but in programming it is the assignment operator. It’s purpose is to assign the value to its right to whatever is to its left. So, in this case, our variable ‘s’ will get assigned the value on the right of the ‘=’. This is where things get a bit more difficult.
A variable is an abstract concept that we use to name a location in the computer’s memory (RAM), which is used to store some kind of data that can vary. For instance, we can have a variable x that holds the number 10 and then we can assign to it the number 20. In our program,
On line 2 we have another function, called input. You can think of it as the opposite of ‘print’. While the function ‘print’ is used to output some text to the Terminal, the ‘input’ function is used to read data from the Terminal. When the ‘input’ function is called, it blocks the program and literally waits for the user to write some data in the Terminal and then press the Enter key. After that key was pressed, the function reads that data and returns its value to whatever called it.
In our case, we are calling the ‘input’ function to read some data from the Terminal and then that data is assigned as a value for our variable ‘s’.
So, after this instruction is executed, the value that the variable ‘s’ will take will be whatever the user enters in the Terminal. If the user enters “test”, then value of ‘s’ will be “test”.
Finally, on line 3 use the ‘print’ function again to display the value of ‘s’ back to the Terminal.
This is what the previous code does: It displays the text “Hello World!”, then waits for the user to enter some data, reads it and then prints it back in the Terminal. It’s not a very exciting program, but it’s useful to show how every single program on a computer is a bunch of instructions that are executed one after the other.
So a program is just some text?
If you want to oversimplify the problem, then the answer is ‘no always, but you can think of it like this…’.
The instructions that you write in order to create the program are basically just some text that follows a syntax and some rules, but a program can contain other kinds of data. If you unlock your phone and go through your photo gallery, that app is actually a program that can display the images that are stored on your phone. This means that, even though the program, the logical part, is just a bunch of instructions, it also has to work with visual data.
With everything that we discussed so far, we still need to cover one subject in order to fully understand what a computer program is and how it works.
Using a program
Writing a program is not enough. You also need to actually use it, to run it. Whenever you open a browser on your computer, or when you are watching a movie, you are using a program that was created by some people, but you are not using the actual code that was written by those people. What you are using is an executable file.
In order to actually run the program that you have written, you need to compile it. This will give you an executable file that you can then run and see your program in action.
You might think that this is the end, but we have yet another thing to talk about.
When it comes to programming languages, you can split them into two big categories: compiled languages and interpreted languages. In some aspects, they are identical. For instance, both compiled languages and interpreted ones are used to write a program that you can then run, and that program is, from the computer’s point of view, a bunch of 1s and 0s. However, each type of programming language has its own advantages and disadvantages.
A compiled programming language is a programming language where you first write the code, then you compile the program and get an executable and only after that you can run it and see how it works. In this case, the compiler is the ones that translates the code that you wrote into the ones and zeroes that the computer can understand and you, the programmer, have to wait for the compiler to finish creating the executable. For simple programs, this process takes less then a second, but for more complex projects you might have to wait a long time. Although this might sound like a hassle, it has one huge advantage: You get a complete executable that you can run directly, without needing anything else. This makes the programs much faster and much more portable.
An interpreted programming language is a language where you write the code and, instead of having a compiling stage where the compiler does the work and you wait, you use an interpreter. The interpreter is similar to a compiler, but it translates the code into ones and zeroes in real time and then automatically executes it. As a consequence, you eliminate the compiling stage entirely, so you don’t have to wait for the program to compile every time you test it. However, since the translation happens in real time, it makes the process a lot slower, which will obviously decrease the program’s performance. Furthermore, with an interpreted language you can’t just generate an executable and then run it on another computer. In order to run the program, you are required to have a correct interpreter on the computer on which you want to run the program.
Let’s take Python for instance, the language that we used in the examples above. Python is an interpreted language. If I want to run a Python program on my computer, I need to have the Python interpreter installed. If you have a computer identical to mine, but you don’t have the Python Interpreter, the code will simply not work and nothing will happen.
On the other hand, with a compiled language like C++ or Rust, you have a compiler that you use to generate an executable. If you have a computer identical to mine and I give you that executable, you will be able to run it without needing anything else. Not only that, but the program will also be significantly faster than the one written in Python.
That's it!
And that’s about it. A program is a set of logical instructions, written using a programming language, that are executed sequentially by a computer in order to perform an action. This would be the oversimplified definition.
If you want to create a program, you first need to choose a programming language (and make sure you choose either a compiled language or an interpreted one). Then you can write the program using that language and then you need to run your program and see how it works.
This entire process doesn’t involve a lot of steps, but you need to spend quite some time understanding each and every one of them.
Thank you for reading this far! I’ll see you in the next blog posts!
For a more formal description of a computer program, I highly recommend reading the following page: Wikipedia – Computer Program
