首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >“无法在过程调用中使用函数”编译器错误

“无法在过程调用中使用函数”编译器错误
EN

Stack Overflow用户
提问于 2013-02-06 09:12:49
回答 3查看 4.1K关注 0票数 0

用ADA实现汉诺塔程序的递归。

到目前为止,我想我已经解决了大部分问题,我的问题是在我的solve函数中。我想我有很好的算法,但是我不确定如何将它实现到函数中,我看到的所有使用它的例子都是在函数内部使用,比如:Example

我的错误是:

代码语言:javascript
运行
复制
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 ")"

到目前为止,这是我的代码。

代码语言:javascript
运行
复制
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;
EN

回答 3

Stack Overflow用户

发布于 2013-02-06 09:42:03

函数返回值,而过程不返回值,并且您已经将Solve定义为函数。

Ada要求您对函数的返回值执行某些操作,而这里没有这样做。(您不能像在其他编程语言中那样忽略返回结果。)

正如错误消息所述,您的语法是进行过程调用的语法,也就是调用过程的语法,但是您提供了函数的名称。

如果从函数返回的值是有意义的,则根据其用途对其执行操作。如果它没有提供任何有意义的功能,则将其删除并将Solve定义为一个过程。

票数 7
EN

Stack Overflow用户

发布于 2013-02-06 10:46:13

顺便说一句,您可能希望将显示代码重新分解为嵌套子程序。在下面的概要中,procedure Print可以访问procedure Solve的参数。

代码语言:javascript
运行
复制
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;
票数 1
EN

Stack Overflow用户

发布于 2013-02-06 23:26:39

除了Marc关于调用Solve不是正确的Ada函数引用的评论之外,您拥有的语法是规范的语法,而不是调用Solve的语法。你把它放在Solve的主体中,只是没有在最初的调用中:

代码语言:javascript
运行
复制
  if (max_disks > 0) then
        solve (N: integer; from, to, using : character);
  END IF;
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14719935

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档