Molten
Molten

Reputation: 27

"Cannot use function in a procedure call" compiler error

Recursion towers of Hanoi program in ADA.

So far I think I have most of it down, my problem is being in my solve function. I think I have the algorithm fine, but I am not sure how to implement it into the function, all examples I see of using this are using the function inside itself such as: Example

My errors are:

hanoi.adb:23:09: cannot use function "solve" in a procedure call
hanoi.adb:27:09: cannot use function "solve" in a procedure call
hanoi.adb:59:15: missing ")"

Here is my code so far.

with ada.text_io, ada.command_line;
use ada.text_io, ada.command_line;


procedure hanoi is

Argument_Error : EXCEPTION;
max_disks, min_disks : integer := 3;
moves : integer := 0;


verbose_bool : boolean;

function solve (N: in integer; from, to, using: in character) return integer is
begin


if N = 1 then
    if verbose_bool = true then
    put("Move disk " & integer'image(N) & " from " & character'image(from) & " to " & character'image(to));
    end if;
else
    solve(N - 1, 'A', 'B', 'C');
    if verbose_bool = true then
    put("Move disk " & integer'image(N) & " from " & character'image(from) & " to " & character'image(to));
    end if;
    solve(N - 1, 'B', 'C', 'A');
end if;
    moves := (2 ** min_disks) - 1;
    return moves;

end solve;

begin

while min_disks /= max_disks loop

IF Argument_Count > 1 THEN
      if Argument_Count = 1 then
      min_disks := integer'value("Argument(1)");
      elsif Argument_Count = 2 then
      min_disks := integer'value("Argument(1)");
      max_disks := integer'value("Argument(2)");
      elsif Argument_Count = 3 then
      min_disks := integer'value("Argument(1)");
      max_disks := integer'value("Argument(2)");
      if argument(3) = "v" or argument(3) = "V" then
      verbose_bool := true; -- if argument is V or v it is true
      end if;
      END IF;
END IF;

IF Argument_Count > 3 THEN
      RAISE argument_error;
END IF;


if (max_disks > 0) then
      solve (N: integer; from, to, using : character);
END IF;

min_disks := min_disks + 1;

end loop;
EXCEPTION
   WHEN Name_Error =>
      Put_Line("Please re-enter your arguments, check to see if you entered integers and characters. Max of 3 arguments.");
   WHEN OTHERS =>
      Put_Line("Please try to not break the program again, thank you.");

end hanoi;

Upvotes: 0

Views: 4512

Answers (3)

Eryndlia Mavourneen
Eryndlia Mavourneen

Reputation: 53

In addition to Marc's comment about the call to Solve's not being a proper Ada function reference, the syntax you have is that of a specification and not that of a invocation of Solve. You had it right in Solve's body just not in the initial invocation:

  if (max_disks > 0) then
        solve (N: integer; from, to, using : character);
  END IF;

Upvotes: 0

trashgod
trashgod

Reputation: 205785

As an aside, you may want to re-factor your display code into a nested subprogram. In the outline below, procedure Print can access the parameters of procedure Solve.

procedure Solve (N: in Integer; From, To, Using: in Character) is

   procedure Print is
   begin
      if Verbose then
      ...
      end if;
   end Print;

begin
   if N = 1 then
      Print;
   else
      Solve (N - 1, 'A', 'B', 'C');
      Print;
      Solve (N - 1, 'B', 'C', 'A');
   end if;
end Solve;

Upvotes: 1

Marc C
Marc C

Reputation: 8522

Functions return values, procedures do not, and you've defined Solve as a function.

Ada requires that you do something with a function's returned value, which you're not doing here. (You can't ignore the returned result as is done in other programming languages.)

As the error message states, your syntax is that of making a procedure call, i.e. invoking a procedure, but you've supplied the name of a function.

If the value being returned from a function is meaningful, then act on it in accordance with its purpose. If it is not providing any meaningful functionality, eliminate it and define Solve as a procedure.

Upvotes: 6

Related Questions