Result函数中的第一行定义了Result变量吗? [英] Is Result variable defined from first line in a function?
问题描述
根据我的测试,Result变量定义为:
Boolean = False,Integer = 0,String = '',Object = nil等从第一行。
但我从来没有看到官方的参考。
它也有意义,因为这给出了提示。
[DCC警告] Unit1.pas(35):H2077赋值给TForm1.Test的值从未使用
function TForm1.Test:Boolean;
begin
结果:= False;
//这里有一些任意代码
结果:= True;
结束
但是,如果我注释掉第一行,最后一行之前有异常,会发生什么?结果= False?
如果Result未定义,这意味着我始终必须通过定义Result来启动每个功能,以防以后出现异常。这对我来说没有意义。
不,结果
无(保证)默认值。它是未定义的,除非你给它一个价值。这是由文档所暗示的,其中说明
如果函数退出而不向Result或
函数名分配值,则函数的返回值未定义。
< blockquote>
我刚刚尝试过
function test:integer;
begin
ShowMessage(IntToStr(result));
结束
,并收到一条包含文本
35531136
。I need a clarification of this case.
According my tests the Result variable is defined to: Boolean=False, Integer=0, String='', Object=nil etc from the first line. But I have never seen an official reference for this. It also make sense as this gives the hint.
[DCC Warning] Unit1.pas(35): H2077 Value assigned to 'TForm1.Test' never used
function TForm1.Test: Boolean; begin Result := False; // Some arbitrary code here Result := True; end;
But what happens if I comment out the first line and there is an exception somewhere before last line? Is Result = False ?
If Result is undefined this means that I always have to start every function by defining Result in case of exception later. And this make no sense for me.
解决方案No,
Result
has no (guaranteed) default value. It is undefined unless you give it a value. This is implied by the documentation, which statesIf the function exits without assigning a value to Result or the function name, then the function's return value is undefined.
I just tried
function test: integer; begin ShowMessage(IntToStr(result)); end;
and got a message with the text
35531136
.这篇关于Result函数中的第一行定义了Result变量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!