Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Function 帕斯卡。指定使用变量而不是同名函数>;_Function_Variables_Pascal - Fatal编程技术网

Function 帕斯卡。指定使用变量而不是同名函数>;

Function 帕斯卡。指定使用变量而不是同名函数>;,function,variables,pascal,Function,Variables,Pascal,我在写长数字算术。这是一个用于添加长二进制数字的函数。我需要在函数中输出和,以调试它。我如何才能做到这一点,而不创建新的变量 function add(var s1,s2:bindata;shift:longint):bindata; var l,i:longint; o:boolean; begin writeln(s1.len,' - ',s2.len); o:=false; l:=max(s1.len,s2.len); add.len:=0;

我在写长数字算术。这是一个用于添加长二进制数字的函数。我需要在函数中输出和,以调试它。我如何才能做到这一点,而不创建新的变量

function add(var s1,s2:bindata;shift:longint):bindata;
var l,i:longint;
    o:boolean;
begin
    writeln(s1.len,' - ',s2.len);
    o:=false;
    l:=max(s1.len,s2.len);
    add.len:=0;
    for i:=1 to l do begin
        if o then Begin
            if s1.data[i+shift] then Begin
                if (s2.data[i]) then add.data[i+shift]:=true
                Else add.data[i+shift]:=false;
            End
            else if s2.data[i] then add.data[i+shift]:=false
            else Begin
                add.data[i+shift]:=true;
                o:=false;
            End;
        End
        Else Begin
            if s1.data[i+shift] then Begin
                if s2.data[i] then 
                Begin
                    add.data[i+shift]:=false;
                    o:=true;
                End
                Else add.data[i+shift]:=true;
            End
            else if s2.data[i] then add.data[i+shift]:=true
            else add.data[i+shift]:=false;
        End;
        output(add);  //Can I output a variable?
    end;
    add.len:=l;
    if o then Begin
        inc(add.len);
        add.data[add.len]:=true;
    End;
end;

您正在函数结果变量中累积函数的结果,这通常很好,但使用了过时的样式,并导致了您在这里所面临的问题。您正在尝试报告函数结果的中间值,为此,您正在尝试引用函数名,
add
。但是,当您这样做时,编译器会将其解释为试图报告函数本身,而不是函数的此特定调用的预期返回值。如果
output
被定义为接受函数地址,您将获得函数的地址;否则,您将得到一个编译器错误

如果编译器提供某种公共语言扩展,则应使用隐式
结果
变量引用中间返回值,而不是继续通过函数名引用它。由于
Result
是隐式声明的,因此不必创建任何其他变量。编译器自动识别
结果
,并将其用作函数返回值的别名。只需在函数中找到您编写的
add
的每个位置,并将其替换为
Result
。例如:

if o then begin
  Inc(Result.len);
  Result.data[Result.len] := True;
end;
Turbo-Pascal、Free-Pascal、GNU-Pascal和Delphi都支持隐式
Result
变量,但是如果您试图使用不提供该扩展的编译器,那么您别无选择,只能声明另一个变量。您可以将其命名为
Result
,然后在末尾附加一行来实现您的函数,如下所示:

function add(var s1, s2: bindata; shift: longint): bindata;
var
  l, i: longint;
  o: boolean;
  Result: bindata;
begin
  {
    Previous function body goes here, but with
    `add` replaced by `Result`
  }
  { Finally, append this line to copy Result into the
    function's return value immediately before returning. }
  add := Result;
end;

创建变量有什么问题?您还可以创建一个
过程WriteNumber(x:bindata)
的另一个优点是,您可以使用它调试任何使用大数字的程序。