Skip to content

FUNCTION 指针


存储指向返回值的 FUNCTION 过程指针的数据类型

语法

dim variable as Function [CDecl|Pascal|StdCall] [( [parameter_list] )] [ ByRef ] [As return_type] [= initializer]

参数

parameter_list:parameter[, parameter[, ...]]

parameter[ByRef|ByVal] identifier [As type] [= default_value]

identifier:函数中引用的变量名称

type:变量的类型

default_value:调用时若未指定参数则使用的默认值

return_value:函数返回的值

initializer:用于设置初始值的函数地址

说明

Function 指针是一种过程指针,存储返回值的已编译代码的内存地址。若未给出初始化器,则默认初始值为零(0)。

可以通过 ProcPtrOperator @ (Address of) 取函数的地址,并将其赋值给变量来设置 Function 过程的内存地址。

该过程必须与声明的 Function 指针具有相同的 Function 声明签名。

要调用已赋值的函数,像调用普通声明的 Function 一样使用 variable 名称,参数列表必须始终用括号括起来(即使为空)(不带括号时,只会访问指针值,即函数的地址)。

Function 指针的主要用途之一是创建回调过程:

  • 回调 Function 是通过参数(Function 指针)传递给另一个过程的 Function,该过程预期会在适当时机调用(执行)该"参数"。

  • 若回调 Function 在调用返回给调用方代码之前完全执行完毕,则称该回调过程为"同步"的。

  • 若调用立即返回给调用方代码,且回调 Function 与调用方的后续代码并行运行,则称该回调过程为"异步"的。

示例

start GeSHi

vb
Function ConcatSelf( x As String ) As String
    Return x & x
End Function

Dim x As Function( x As String ) As String = ProcPtr( ConcatSelf )

Print x( "Hello" )

end GeSHi

start GeSHi

vb
Function x2 (ByVal i As Integer) As Integer
  Return i * 2
End Function

Function x3 (ByVal i As Integer) As Integer
  Return i * 3
End Function

Function operation (ByVal i As Integer, ByVal op As Function (ByVal As Integer) As Integer) As Integer
  Return op(i)
End Function

Print operation(4, @x2)
Print operation(4, @x3)

end GeSHi

start GeSHi

vb
' Example of basic callback Function mechanism (asynchronous) to implement a key pressed event:
' (the user callback Function address cannot be modified while the event thread is running)
'   - An asynchronous thread tests the keyboard in a loop, and calls a user callback Function each time a key is pressed.
'   - The callback Function address is passed to the thread.
'   - The callback Function prints the character of the key pressed,
'       but if the key pressed is `<escape>` it orders the thread to finish by using the function return value.
'   - As the user callback address is passed to the thread as argument, it cannot be modified while the thread is running.

'' thread Sub definition
  Sub threadInkey (ByVal p As Any Ptr)
    If p > 0 Then                                                '' test condition callback Function defined
      Dim As Function (ByRef As String) As Integer callback = p  '' convert the any ptr to a callback Function pointer
      Do
        Dim As String s = Inkey
        If s <> "" Then                                          '' test condition key pressed
          If callback(s) Then                                    '' test condition to finish thread
            Exit Do
          End If
        End If
        Sleep 50, 1
      Loop
    End If
  End Sub

'' user callback Function definition
  Function printInkey (ByRef s As String) As Integer
    If Asc(s) = 27 Then                                        '' test condition key pressed = `<escape>`
      Print
      Return -1                                                '' order thread to finish
    Else
      Print s;
      Return 0                                                 '' order thread to continue
    End If
  End Function

'' user main code
  Dim As Any Ptr p = ThreadCreate(@threadInkey, @printInkey)   '' launch the thread, passing the callback Function address
  ThreadWait(p)                                                '' wait for the thread finish

end GeSHi

与 QB 的区别

  • FreeBASIC 新增

另请参阅

  • Function
  • ProcPtr
  • Operator @ (Address of)

返回 目录

基于 FreeBASIC 官方文档翻译 如有侵权请联系我们删除
FreeBASIC 是开源项目,与微软公司无隶属关系