2

如果 Ada 中的一个参数是无模式的,会发生什么?

有什么区别

procedure my_func ( param_1 : in param_type )

procedure my_func ( param_1 : param_type )

我是 ada 的新手,我的大部分程序都是后者。程序按预期编译并运行。

4

2 回答 2

6

没有区别 - 如果没有给出参数模式,编译器假定“in”。

请参阅http://www.ada-auth.org/standards/12rm/html/RM-6-1.html 18/3 开始的行。

——马丁

于 2013-07-09T14:35:17.623 回答
0

正如 Martin 所建议的,如果未提供,则默认模式为“in”。

我想补充一点,如果可能的话,你有时可以尝试一些你怀疑的事情。就像看下面的简单代码一样,我没有为争论“no_1”提供任何模式。正如所见,我正在为其分配“no_2”的值。

with Ada.Text_IO; use Ada.Text_IO;

procedure just_testing is

    procedure get_value (no_1 : Integer);

    procedure get_value (no_1 : Integer) is
        no_2 : Integer := 2;
    begin
        no_1 := no_2;
    end get_value;

begin

    Put("auto mode");

end just_testing;

当我编译这段代码时,看看我们得到了什么错误。

>gnatmake just_testing.adb
gcc -c just_testing.adb
just_testing.adb:10:09: assignment to "in" mode parameter not allowed
gnatmake: "just_testing.adb" compilation error

因此,编译器明确表示默认模式为“in”,因为我们不能为模式为 in 的参数分配任何值。

于 2020-04-29T12:56:54.060 回答