A:
天软函数中,参数是按参数位置来识别各个参数变量的
参数类型是弱类型,即数据类型是不定,可变的
参数是依次识别,后面的参数可省掉,注意程序中需要做相关的处理,避免导致运行异常
比如根据需求封装以下各类函数,可对函数调用时,进行缺省执行:
//定义最大参数个数
function funcA(a,b);//支持funcA()、funcA(a)、funcA(a,b)的调用
begin
if ifnil(a) then a:=0; //当第一个参数无时,赋默认值
if ifnil(b) then b:=3; //当第二个参数无时,赋默认值
return a+b;
end
//只有最后一个参数可省
function funcB(a,b);//只支持第二个参数不设置即funcB(a)、funcB(a,b)
begin
if ifnil(b) then b:=3; //当第二个参数无时,赋默认值
return a+b;
end
function funC(); //随意参数个数
begin
vLen:=PARAMCOUNT;//函数传入的参数个数
vars:=params; //params当前函数的参数值,是个序列
echo vLen;
a:=0; //返回参数序列中实数数据的总和
for i:=0 to vLen-1 do
if ifnumber(vars[i]) then
a+=vars[i];
return a;
{ 注意:若直接访问params,则需从下标1开始,即params[1]表示第一个参数 }
end
调用方式展示如下:
t1:= funcA(); //返回3
t2:= funcA(10);//返回13
t3:= funcA(10,10);//返回20
t4:= funcB(10);//返回13
t5:= funcB(10,10);//返回20
t6:= funC();//返回0
t7:= funC(10);//返回10
t8:= funC('test',10);//返回10
t9:= funC(1,2,3,4,10); //返回20
t10:= funC(1,'a',array(2,2),9);//返回10
return array(t1,t2,t3,t4,t5,t6,t7,t8,t9,t10);
返回结果: