Subprograms
Subtract procedure
Goal: write a procedure that subtracts two numbers.
Steps:
Complete the procedure
Subtract
.
Requirements:
Subtract
performs the operationA - B
.
Subtract function
Goal: write a function that subtracts two numbers.
Steps:
Rewrite the
Subtract
procedure from the previous exercise as a function.
Requirements:
Subtract
performs the operationA - B
and returns the result.
Equality function
Goal: write a function that compares two values and returns a flag.
Steps:
Complete the
Is_Equal
subprogram.
Requirements:
Is_Equal
returns a flag as aBoolean
value.The flag must indicate whether the values are equal (flag is
True
) or not (flag isFalse
).
States
Goal: write a procedure that displays the state of a machine.
Steps:
Complete the procedure
Display_State
.
Requirements:
The states can be set according to the following numbers:
Number
State
0
Off
1
On: Simple Processing
2
On: Advanced Processing
The procedure
Display_State
receives the number corresponding to a state and displays the state (indicated by the table above) as a user message.
Remarks:
You can use a case statement to implement this procedure.
States #2
Goal: write a function that returns the state of a machine.
Steps:
Implement the function
Get_State
.
Requirements:
Implement same state machine as in the previous exercise.
Function
Get_State
must return the state as a string.
Remarks:
You can implement a function returning a string by simply using quotes in a return statement. For example:
function Get_Hello return String;function Get_Hello return String is begin return "Hello"; end Get_Hello;with Ada.Text_IO; use Ada.Text_IO; with Get_Hello; procedure Main is S : constant String := Get_Hello; begin Put_Line (S); end Main;You can reuse your previous implementation and replace it by a case expression.
For values that do not correspond to a state, you can simply return an empty string (
""
).
States #3
Goal: implement an on/off indicator for a state machine.
Steps:
Implement the function
Is_On
.Implement the procedure
Display_On_Off
.
Requirements:
Implement same state machine as in the previous exercise.
Function
Is_On
returns:
True
if the machine is on;otherwise, it returns
False
.Procedure
Display_On_Off
displays the message
"On" if the machine is on, or
"Off" otherwise.
Is_On
must be called in the implementation ofDisplay_On_Off
.
Remarks:
You can implement both subprograms using if expressions.
States #4
Goal: implement a procedure to update the state of a machine.
Steps:
Implement the procedure
Set_Next
.
Requirements:
Implement the same state machine as in the previous exercise.
Procedure
Set_Next
updates the machine's state with the next one in a circular manner:
In most cases, the next state of
N
is simply the next number (N + 1
).However, if the state is the last one (which is 2 for our machine), the next state must be the first one (in our case: 0).
Remarks:
You can use an if expression to implement
Set_Next
.