做网站什么商品好/bt磁力猪
文章目录
- 一般函数
- 带返回值的函数
- 一处返回
- 多处返回
- 参数为引用的函数
一般函数
//函数定义
function Hello(name as string)Print("Hello " + name + "!")
endfunction//函数调用
Hello("Tom")
使用 exitfunction 可以直接跳出函数,和c++中的return类似
带返回值的函数
一处返回
function Add(x as integer, y as integer)z as integerz = x + y
endfunction z
可简写如下:
function Add(x,y)
endfunction x+y
多处返回
function Check(score as float)if score>=90exitfunction "A"elseif score>=80exitfunction "B"elseif score>=60exitfunction "C"endif
endfunction "D"
参数为引用的函数
选择排序定义
- 第一个参数为数组类型,只有加上
ref
关键字时才会改变原数组的值 - 第二个参数表示排序方式,值为0时升序,非0时降序
//选择排序
function SelectSort(_array ref as integer[],_type as integer)for i=0 to _array.lengthk as integerk = ifor j = i to _array.lengthif _type = 0 //升序if _array[k] > _array[j] then k = jelseif _array[k] < _array[j] then k = jendifnext jif k<>i //交换两元素tem as integertem = _array[i]_array[i] = _array[k]_array[k] = temendifnext i
endfunction
函数调用
nums as integer[4] = [2,1,4,5,3]
SelectSort(nums,1) //降序//输出数组元素
for i=0 to nums.lengthPrintC(Str(nums[i])+" ")
next i
Print("") //换行
Sync() //同步到屏幕
运行效果
目前函数不支持函数重载,不支持参数列表带默认值。希望The Game Creators公司后续能加入吧!