14. Introduction to Programming Languages
The Raspberry Pi repositories include support for COBOL, as well as a rich set of more modern programming languages such as Python, C, C++, and Java. It has support more niche and legacy pro‑ gramming languages. Generally, the paradigms for programming languages fall into three general categories:
• procedural programming languages
• object oriented programming languages
• functional programming languages
14.1 The Python Programming Language
Python was invented in the early 1990’s by Guido van Rossum. Python can be written using either a procedural or object oriented paradigm. It is an open source project that is widely available, uses simple syntax and includes rich libraries and offers a variety of programming tools, Python source code is run on a virtual machine which translates code into the specific machine‑code executed by the processor. Because Python is interpreted by a virtual machine, it is platform independent.
The Raspberry Pi should have Python installed by default and can run Python programs directly from the command line. To enter a program, you first need a plain text editor. If you are using a desktop environment, there is a friendly, graphical, integrated development environment (IDE) for Python suitable for beginners called Thonny. To install Thonny, type:
sudo apt install thonny
For more advanced users in a graphical environment, the vscode program provides an excellent editor for coding in a variety of different languages, including Python. As described earlier, vscode can also be run to edit files remotely. If you are using the command line, you can use any of the command line editors described in the preceding sections, including vi, emacs, or nano. For example, to edit a Python source file called hello.py, type the following:
nano hello.py
Next, enter the following code into the source file:
name = input('What is your name? ')
print('Hi', name,' welcome to the Raspberry Pi!')
print('Good Bye')
Next, save and exit nano and run the file by typing:
python3 hello.py
The program should run as expected.
14.2 Compiling and Running a C/C++ Program
Linux has a variety of tools to support software development in both C and C++. In fact, the Linux operating system itself is written in C. The C programming language is a procedural language, and C++ builds on C to provide support for object oriented programming. The compilers which we will use under Linux are the GNU C Compiler (gcc) and the GNU C++ compiler (g++). To ensure the GNU C/C++ compiler tools are installed, type:
sudo apt install gcc g++ gdb build-essential
For example, to enter a simple C program named hello.c using the nano editor, type the following:
nano hello.c
Using the editor, enter the following code into the source file:
/* A Raspberry Pi C program */
#include <stdio.h>
int main(void)
{
printf("Hello world.\n");
printf("Compiled and run on a Raspberry Pi.\n");
return 0;
}
Save and exit the editor. To compile your source code type the following at the prompt in a terminal window. For example, to compile the hello.c program above, you can enter:
gcc -Wall -o hello hello.c
The gcc compiler has numerous other command line options which you may use. For more information consult the man pages. Note that the C++ compiler can be invoked by using g++ in place of gcc. To run the compiled program in the current working directory, do not forget to specify a ./ in front of the program name to specify the path as the current directory. For example, to run the hello.c program after compiling it as ini, type:
.\hello
If no output filename was provided to the compiler the default output filename will be a.out.
14.3 Compiling and Running Java Program
It is also possible to develop and run Java programs using Linux on the Raspberry Pi. There are two different packages which can be installed: on provides the Java Runtime Environment (JRE) and another provides the Java Development Kit (JDK). The JRE just allows you to run Java programs, but the JDK enables one to compile and run Java programs. To install the OpenJDK Java development kit, type the following:
sudo apt install default-jdk
Once this is installed you can compile a Java program. For example, enter the following simple Java program named hello.java using a plain text editor:
/* A Java program */
class Main {
public static void main(String args[]) {
System.out.println("Hello world.\n");
}
}
Compile the program by typing the following at the prompt:
javac hello.java
where myprogram.java is the name of a Java source file. To run a Java program, type the following at the prompt: java Main where Main is the name of the class where the program begins.