Reputation: 33
Question: why and how to fix this error message about ADA? Thax
12:04 declarations must come before "begin" 29:01 statement expected
With Ada.Text_IO; Use Ada.Text_IO;
With Ada.Integer_Text_IO; Use Ada.Integer_Text_IO;
With Ada.Strings.Unbounded; Use Ada.Strings.Unbounded;
With Ada.Strings.Bounded;
procedure polynomial is
begin
function evaluate (x: Integer) return Integer is
type i is new Integer;
type p is new Integer;
type x is new Integer;
begin
for i in reverse 0..10 loop
i:= i-1;
p:= coef(i)+x*p;
end loop;
return p;
end evaluate;
end polynomial;
Upvotes: 1
Views: 4518
Reputation: 31689
As the error message says, declarations must come before begin
. Thus, if you have a declaration, it must come before the begin
of the construct that encloses it. For example, the type declarations of i
, x
, and p
(which should not be type declarations, but that's another problem) are directly enclosed in the evaluate
function. Therefore, they must appear before the begin
of the evaluate
function, as they are.
The thing is that this applies to function
and procedure
declarations, too--and a complete function body, such as evaluate
, counts as a declaration for this rule. evaluate
is directly enclosed in polynomial
. Therefore, it must appear before the begin
line belonging to polynomial
, whereas your code puts it right after the begin
line, which is illegal.
There are actually two ways to fix it: (1) Move evaluate
before the begin
line. (2) Add a block that begins with declare
:
procedure polynomial is
begin
declare
function evaluate (x: Integer) return Integer is
i : Integer;
-- etc.
begin
-- etc.
end evaluate;
begin -- this is the BEGIN line of the block
-- Now put some code here!
end; -- this is the end of the block
end polynomial;
Now the evaluate
function is directly enclosed in the block, not in polynomial
, and it must occur before the begin
line of the block, but it doesn't have to occur before the begin
line of polynomial
.
A couple other things: This declaration:
type i is new Integer;
does not mean that "the type of i
is an integer". It means you're declaring a new type named i
. You don't want that, you want a variable.
i : Integer;
Second: note the part of the block where I said "now put some code here". That's necessary. Declaring the function evaluate
does not run the function. You have to put in a statement that calls it, if you ever want your function to run. Your original incorrect code didn't have any statements that called evaluate
, which is why I am not sure whether you understood that concept.
Upvotes: 1