DECLARE
- 来源: https://www.freebasic.net/wiki/wikka.php?wakka=KeyPgDeclare
- 最后更新: 2019-10-20
声明模块级或成员过程。
语法
vb
Declare Sub name [ param_list ]
Declare Function name [ param_list ] [ Byref ] As return_type
Declare Operator op_symbol param_list [ [ Byref ] As return_type ]
Type T
Declare Constructor [ param_list ]
Declare Destructor
Declare Sub name [ param_list ]
Declare Function name [ param_list ] [ Byref ] As return_type
Declare Operator name [ param_list ] [ [ Byref ] As return_type ]
Declare Property name [ ( [ param_list ] ) ] [ [ Byref ] As return_type ]
End Type参数
param_list
括号内以逗号分隔的参数列表。
return_type
Function、Operator 或 Property 过程的返回类型。
name
过程的名称或符号。
op_symbol
运算符的名称或符号。
T
新用户定义类型的名称。
描述
Declare 语句声明 Sub、Function、Operator、Constructor 或 Destructor。
无需看到过程定义即可在代码中引用该过程,但必须在某处定义它。本质上,Declare 语句引入一个过程,并声明其定义在其他地方。例如,函数可以在源模块顶部声明,然后调用,最后在源文件底部定义,如下方示例所示。
过程的声明与其定义的第一行几乎相同,只是声明前有 Declare 关键字且没有函数体。此外,声明中省略 Export 等属性。
FreeBASIC 与 QB 一样,不要求声明函数,除非函数定义在不同的源文件中,或在同一文件中定义于调用位置之后。但对于 Type 主体内声明的过程,必须始终先在 Type 主体中声明,再使用。若不声明 Type 过程,将收到错误。
由于每个使用函数的文件都必须有其声明,声明通常保存在一个或多个 包含文件 中,使任何需要它的模块都能通过 #include 语句使用该函数。
示例
模块级函数:
start GeSHi
vb
'' declare the function sum which takes two integers and returns an integer
Declare Function sum( As Integer, As Integer ) As Integer
Print "the sum of 420 and 69 is: " & sum( 420, 69 ) '' call the function sum
'' define the function sum which takes two integers and returns an integer
Function sum( a As Integer, b As Integer ) As Integer
Return a + b
End Functionend GeSHi
类型级子程序:
start GeSHi
vb
Type my_type
my_data As Integer
Declare Sub increment_data( )
End Type
Sub my_type.increment_data( )
my_data += 1
End Sub
Dim As my_type an_instance
an_instance.my_data = 68
an_instance.increment_data( )
Print an_instance.my_dataend GeSHi
方言差异
- 在 -lang fb 方言中,
Byval为默认参数传递方式。 - 在 -lang qb 和 -lang deprecated 方言中,
Byref为默认参数传递方式。 - 类型级的
Sub/Function/Operator/Constructor/Destructor仅在 -lang fb 中可用。
与 QB 的区别
- 在 FreeBASIC 中,参数名是可选的。
另请参阅
SubFunctionOperatorPropertyConstructorDestructorConstructor (module)Destructor (module)TypeDimAlias (Name)Alias (Modifier)
返回 目录