Delphi 函数的重载和作用域

1.在delphi 中,我们可以使用相同的函数名来调用不同的函数,我们称这个函数为重载,函数的参数类型和参数的个数可以不同,用到的关键字overload;格式如下:

function  addInt(x,y:integer):string;overload;
begin
  result := intToStr(x + y);
end;

function addInt(x,y,z:integer):string;overload;
begin
  result := intToStr(x + y + z);
end;

function addInt(x,y:integer;z:string):string;overload;
begin
  result := intToStr(x + y + strToInt(z));
end;

注意点:每个函数后面都需要加上:overload;

2.函数的作用域(局部和全局函数):

在单位文件下声明的函数只能应用于本单元,可以通过其他单元引用来使用函数;但是必须在被引用单位的接口部分声明函数,否则还是无法找到

如:

Unit1.pas文件

unit  Unit1;

interface

uses
  windows, Messages, SysUtils;

uses 
  unit2;  //可以和上面的uses一起连写

implementation

procedure TForm1.Button1Click(Sender : TOBJect);
begin
  showmessage(addint(1,2,3));
end;

end.

Unit2.pas文件:

unit  Unit2;
interface

uses
  windows, Messages, SysUtils;

function addint(x,y,z:Integer):string;  //必须在这里声明函数,才能被其他单位去引用

implementation

function addInt(x,y,z : integer):string;
begin
  Result := intToStr(x+y+z);
end;

end.

转载于:https://www.cnblogs.com/delphiclub/p/4168004.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值