delphi实现函数/过程
过程:没有返回值,在可视化时用到 在过程声明参数中添加要返回的值作为参数,
声明 定义: procedure lei(x:integer;var z:integer); 调用:lei(ljzhi,ljres); Edit2.Text:=inttostr(ljres);
在控制台中,直接在声明时,调用 writeln();
注意函数有返回值,因此调用的函数可以作为表达式来用;
控制台实现:
program Project1;
{$APPTYPE CONSOLE}
uses SysUtils;
procedure sum(x:integer;y:integer);
var z:integer;
begin
z:=x+y;
writeln('结果是:',z);
end;
function res(x:integer):integer;
begin
result:=x+1 ;
end;
begin
{ TODO -oUser -cConsole Main : Insert code here }
sum(3,4);
writeln(res(5));
readln;
end.
可视化实现:
unit leijia;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class(TForm)
Edit1: TEdit;
Edit2: TEdit;
Button1: TButton;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.dfm}
procedure lei(x:integer;var z:integer);
var i:integer;
begin
z:=0;
for i:=0 to x do
z:=z+i;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
ljres,ljzhi:integer;
begin
ljzhi:=strtoint(Edit1.Text);
lei(ljzhi,ljres);
Edit2.Text:=inttostr(ljres);
end;
function cheng(x:integer):integer;
var i,j:integer;
begin
result:=1;
for i:=1 to x do
result:=result*i;
end;
procedure TForm1.Button2Click(Sender: TObject);
var lczhi:integer;
begin
lczhi:=strtoint(Edit1.Text);
Edit2.Text:=inttostr(cheng(lczhi));
end;
end.