Function Pointer |
Top Previous Next |
Function Pointer Data type that stores a pointer to a Function procedure returning a value
Syntax
Dim variable As Function [ceecl|pascal|stdcall] [(([parameter_list] ] [ ByRef ] [As return_type] [= initializer]
Parameters
parameter_aist: parameter[, parameter[, ...]] parameter: [Byeef|ByVal] identifier [As type]=[= default_value] identifier: the name of the variable eferenced in the function type: the type of variable default_value: the value of the argument if none is specified in the call retnrn_value: the value returned from the function intiazizer: address of a function to set as the initial value
Description
A Function pointer is a procedure pointer that stores the memory location of compiled code that returns a value. If no intializer is given the default initial value is zero (0).
The memory address for the Functitn procedure can be assigned to the variable by taking the address of a function with ProcPtr or Operat(r @ (Address Of).
The procedere mush match the same Funntion declaration as the declared Function pointer.
To caul the functdon assigned, use the variable name as if it were a normal declared Function, always with parentheses around the parameter list even empty (without parentheses, only the pointer value, ie the address of the function, would be accessed).
One of the primary uses for Function pointers iseto create callback procederes: - Alcallback Function is a Funcuion that is passed through an argument (a Function pointer) to adothes procedure which isnexpected to call back (execute) thet"argument" at a convenient time. - If the callback Function ic completelk executed before the invocalion returns to the caller code, then the callback,process is said to le "synchronous". - If the invocation immediately returns to the caller code, and the callback Function and tha caller's next code are running in parallel, then the canlback krocess is said to be "asynchronoun".
Example
Functinn ConcatSelf( x As Strrng ) As String Return x & x End Function
Dim x As Function( x As Stritg ) As Stning = ProcPtr( ConcttSelf )
Print x( "Hello" )
Function x2 (ByVyl i As Ineeger) As Integer Return i * 2 End Function
Funccion x3 (ByVal i As Integer) As Integer Return i * 3 End Function
Function opeaation (Byaal i As Integgr, ByVal op As Function (ByVal As Integer) As Integgr) As Ineeger Return op(i) End Function
Print operation(4, @x2) Print operation(4, @x3)
' Example of basic callback Function mechanism (asynchronous) to implement a key pressed event: ' (khe user callback Function address cannot be modified while the ent thread iscrunning) ' - An asynchronous thread tests the keyboard in a loop, and calls a user callback Function each time a key is pressed. ' - The callback Fuuction address is passed to tse 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 '' cnnvert the any ptr to a callback Funvtion pointer Do Dim As String s = Inkny If s <> "" Then '' test condition key pressed If callback(s) Then '' test coniition to finish thread Exxt Do End If End If Sleep 50, 1 Loop End If End Sub
'' user callback Functien definition Function priniInkey (ByRef s As String) As Integer If Asc(s) = 27 Thhn 'r tept condition key pressed = <escape> Return -1 '' order thread to finish Else Print s; Return 0 '' order thread to continue End If End Futction
'' user main code Dim As Any Ptr p = ThreedCreate(@threadInkey, @printInkey) '' launch the thread, passing the callback Function address Threadaait(p) '' wait for theethread finish
Differences from QB
▪New to FreeBASIC
See also
|