Delphi初浅入门笔记之三:过程和函数(过程篇)

本文介绍了一个使用Delphi编写的简单程序,该程序能够计算两个整数的阶乘并返回它们的和。程序中定义了两个过程CalcFact和GetSum,分别用于计算阶乘和求和。

过程:

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    Edit1: TEdit;
    Edit2: TEdit;
    Label1: TLabel;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

{

在这里定义了两个过程:CalcFact,GetSum。过程CalcFact(Num:Integer;Var value:Int64)需要传入一个Integer类型的参数,然后通过形参Value来将值返回。由此可以知道,过程的定义是以Procedure关键字开头的,然后是过程的名字 然后是参数列表后面还需要一个”;”。

}
  procedure CalcFact(Num:Integer;Var value:Int64);
  var
  i:Integer;

  begin
  Value:=1;
  for i:=1 to Num do
  Value:=Value*i;
  end;

  procedure GetSum(V1,V2:Integer;Var Sum:Int64);
  var
  SumV1,SumV2:Int64;
  begin
     CalcFact(V1,SumV1);//在这里调用了CalcFact这个过程
     CalcFact(V2,SumV2);
     Sum:=SumV1+SumV2;
  end;

procedure TForm1.Button1Click(Sender: TObject);
Var
Sum:Int64;
Ed1,Ed2:Integer;
begin
   if(edit1.Text='') or (edit2.Text='') then
   begin
       Application.MessageBox('请输入整数','提示:',MB_OK);
       exit;
   end;
   Ed1:=StrToInt(edit1.Text);
   Ed2:=StrToInt(edit2.Text);
   GetSum(Ed1,Ed2,Sum);
   label1.Caption:=trim(edit1.Text)+'与'+trim(edit2.Text)+'的阶乘和为'+IntToStr(Sum);
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
close;
end;

end.

源代码下载

转载于:https://www.cnblogs.com/liszt/archive/2011/03/02/1969411.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值