delphi怎么实现递归
delphi如何实现递归?
delphi貌似没有return 吧?那是如何实现递归的呢?
谁能把这C翻译成delphi
------解决方案--------------------
哎...
delphi貌似没有return 吧?那是如何实现递归的呢?
谁能把这C翻译成delphi
- C/C++ code
#include <stdio.h> int func(int x) { if(x==0) return 1; else return x*func(x-1); } int main() { printf("%d",func(4)); return 0; }
------解决方案--------------------
哎...
- Delphi(Pascal) code
function func(x: Integer); begin if x = 0 then Result := 1 else Result := func(x - 1); end;
------解决方案--------------------
- Delphi(Pascal) code
function func(x:integer):integer; begin if x=0 then result:=1 else result:=x*func(x-1); end;
------解决方案--------------------
退出用exit
------解决方案--------------------
Delphi中可以用result代表函数的返回值(如果你显式定义了叫result的局部变量则不能用作返回值),而且比return更好用,result既能作为左值也能作为右值,但是既不退出函数也不递归,可以反复用。Delphi函数中,本函数名作为左值代表返回值(但是也并不退出函数),作为右值代表递归调用。