Procedural Programming + Ada, Lesson 2 – An Imperative Language

Tram Ho

Ada is a language that supports logic implementations of many different programming paradigms, including OOP Object Oriented Programming and some elements of FP Functional Programming. However, the core design of Ada is to be an Imperative Programming language central to C or Pascal .

There is one important difference between Ada and most other programming languages, which is that statements and expressions are very clearly distinguished in Ada . If we try to use an expression where there should be a statement, we will definitely get an error message when compiling the code.

This principle complements one of the language design criteria mentioned earlier: That is, expressions are used to convey values, not to produce any other side effects. This will help screen out some programming errors in some situations, for example when we mistakenly use equivalence comparison where there should be a statement that assigns a value to a variable. there.

It is an example of forking logic in JavaScript , where we intend that: If the shoulDoIt also agrees with the subconscious fromMySoul , then the intention will be executed. In this case when we misrepresent by the above value assignment, obviously the result will always be "Ok. We'll do it." . And that’s something Ada won’t let happen.

Hello, Ada!

Before we talk about installing and preparing the environment for Ada , let’s take a quick look at the Hello World program just like when we start learning any other programming language.

There are a few points worth noting in this program:

  • We have a language that uses indentation style syntax with statements that are considered to be in the same block if they have the same indentation at the beginning, instead of block style with curly braces {} like C or JavaScript .
  • A sub-program in Ada can be a procedure or a function . And a procedure like in the example code above will never return a value at the called location.
  • The with keyword is used to refer to external module . This element is equivalent to import in many other languages, and #include in C or C++ . We will learn more about the with operation later.
  • Unlike languages ​​like C or C++ , the main program in Ada can be named arbitrarily, as in the example above Greet , instead of Main .
  • comment are prefaced with the symbol -- and extend to the end of that line. We will not have multi-line comment syntax and if necessary we will have to create multiple single line comments.

In terms of expressive meaning, procedure are equivalent to sub-program in other languages ​​if they return meaningless values. For example void in C and C++ , or undefined in JavaScript , etc. We will go through the operation of defining function function in Ada in the following section. For now, we will modify the Hello World program a bit.

This time we use the additional use feature that comes with with . The syntax to use is use package-name . And in the place of calling the Put_Line procedure, we were able to reduce the package reference name up front.

Environment Settings

Although used mainly in the fields of high-tech science and enterprise applications, Ada ‘s application development toolkit offers a free community edition with a custom compiler. Full-featured translation equivalent to the commercial pro edition for businesses.

You can download GNAT Community for Windows here:

  • GNAT Community – integrated toolkit with IDE code editor for Ada and C
  • GTA-Ada – plug-in that supports creating project with GNOME.org GUI libraries for cross-platform application development needs. Write code only once and compile package for Windows, Linux, BSD, Mac, etc..

If you are using a certain version of Linux, you can find in the default repository library the gnat-12 package (the latest version is Ada 2012) and the gprbuild project management tool (GNAT Project Build). In it, gnat-12 is the core of the development environment and we will learn how to immediately use gprbuild to set up the project and manage the code folders.

Currently I am using Ubuntu 22.10 and so I can guarantee the install command below will work fine. Other typical Linux distributions such as Fedora and OpenSUSE also list the above package in the library lookup pages.

In particular about usage, in the tutorial series here, I will use gprbuild using the command line window and edit the project declaration file manually. You can also save the built-in IDE editor until you want to create an actual application project . And so here I need to pay attention to the installation operation on Windows.

All operations with the installer are Next , Next , and Next with the options already set by default. However, the GNAT Community installer for Windows will not automatically declare environment variables for operations on the CMD command line. Therefore, after running the installer, we will need to open the installation directory of GNAT and navigate to the bin directory. Then copy/paste the full path of the C:GNAT2021bin directory into the Path environment variable.

Environment Variables > System Variables > Path > Edit > New

Or open a CMD command line window with Admin rights:

Then in the command line window, we can type the command to check the installed version of GNAT :

Ok.. everything is ready, let’s start learning the details of the Ada language and the gprbuild management tool.

Procedural Programming + Ada, Lesson 3 – Imperative Syntax

Share the news now

Source : Viblo