Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Fix and improve Ada" #531

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
114 changes: 27 additions & 87 deletions ada.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,94 +2,33 @@
title: Ada
description: Ada Programming language cheatsheet contains useful code syntax with examples which is handy while coding.
created: 2020-06-24
updated: 2024-02-05
updated: 2020-06-24
---

## Data types

* Strongly typed

| Types | Data-type|
|----|----|
|Basic | Integer, Character, Float, Long_Float, Short_Integer, Long_Integer, etc |
|Pointer | access |
|Composite | array, String |
|User Defined Data Type | record, enumeration, tagged type, variant record, range types, etc. |


### Naming convention
* First character in identifier is a letter, then letters (both uppercase and lowercase letters), digits and underscore(`_`).
* Case insensitive

## Arrays

```ada
array-name : array (Fist..Last) of element-type; -- one-dimensional Array
array-name : array (Fist1..Last1, First2..Last2) of element-type; -- two-dimensional Array
```
## String

It is an array of Character.

```ada
My_String := String := "abcde"; -- string declaration
```

## Literals or Constants
|Literal | Example|
|----|----|
|Integer Literal- decimal|255|
|Integer Literal- octal|8#377#|
|Integer Literal- hexadecimal|16#FF#|
|Float point Literal|53.0, 79e-6|
|Character literals| 'a', '1'|
|String literals| "OneCompiler", "Foo"|
|Boolean literals| True, False|

### Special characters
Special characters are defined as constans in `ASCII` or `Ada.Characters.Latin_1` package.
|Constant| Description|
|----|----|
|LF | New line|
|CR | Carriage Return|
|HT | Horizontal tab|
|VT | Vertical tab|
|NUL | Null character|

## Operators

| Operator type | Description|
|----|-----|
| Arithmetic Operator|+ , - , * , / , `**` (exponentiation)|
| Relational Operator| < , > , <= , >=, /= , =|
| Logical Operator| `and` , `or`, `xor`, `not` |
| Assignment Operator| := |
| If expression| (if a then b else c) |

## Sample program

```ada
with Ada.Text_IO; use Ada.Text_IO;
procedure Hello is
begin
Put_Line ("Hello, World!");
Put_Line ("Hello, World!");
end Hello;
```
* `Ada.Text_IO` : package that contains routines for I/O of text.
* `with` : tells compiler that the package will be used.
* `use` : tells compiler that the package should be automatically searched and allows access to routines without using the fully qualified name.
* `procedure` : used to declare a procedure named Hello here.
* `Put_Line` : is a procedure for string output.
* `--` : comment.
* **ada.text_io** : package that contains routines for i/o of text
* **with** - tells compiler that the package will be used
* **use** : tells compiler that the package should be automatically searched and allows access to routines without using the fully qualified name
* **procedure** : used to declare a procedure named Hello here.
* **Put_line** : is a procedure for string output.
* **--** : comment

## Variables

```ada
varName : DataType := value;
varName : DataType := value
```
## Constants
```ada
varName : constant DataType := value;
varName : Constant DataType := value
```
## Conditional Statements

Expand Down Expand Up @@ -118,48 +57,49 @@ end case;
This is the simplest loop

```ada
begin
Index := 1; --initialization
loop
--code
exit when Index = n;
end loop;
loop
--code
exit when Index = n;
end loop;
```
### 2. while loop

```ada
Count := 1; --initialization
while Count < n loop
--code
--code
end loop;
```

### 3. for loop

```ada
for Index in 1..n loop
--code
--code
end loop;
```
## Sub programs

Functions and procedures are collectively called as sub-programs.

### Syntax for procedures
### Syntax for procedure

```ada
procedure proc_name
(X : in Integer ; Y : out Integer ; Z : in out Integer ) is
procedure proc-name
(X : in Integer ; Y : out Integer ; Z : in out Integer ) is
begin
X := 10; −− it's an Error as you can’t modify an in parameter.
Y := X; −− can modify Y as it's an out parameter.
Z := Z + 1; −− can read and write as it's an in out parameter.
end proc_name;
X := 10; −− it's an Error as you can’t modify an in parameter.
Y := X; −− can modify Y as it's an out parameter.
Z := Z + 1; −− can read and write as it's an in out parameter.
end proc-name;
```
### Syntax for functions

```ada
function function_name(parameter : parameter_type) return value is
function function_name(declaration) return value is
begin
--code
end function_name;
--code
end function-name;
```