I have read several of the posts about function pointers and I'm not understanding the correct syntax for dimensioning and assigning function pointers.
I'm not sure how to fix the following errors/warnings (code follows)
error 56: Illegal specification, at parameter 3 (process) of iterate() in 'byref process() as integer as function ptr _'
error 63: Expected explicit result type, found 'some_process' in 'dim as function some_process( byval n as integer ) as integer'
error 68: Array access, index expected, before '=' in 'some_process = @_increment()'
warning 4(1): Suspicious pointer assignment
error 68: Array access, index expected, before ')' in 'answer = iterate( test_value, times, some_process )'
error 68: Array access, index expected, before '=' in 'some_process = @_double'
warning 4(1): Suspicious pointer assignment
error 68: Array access, index expected, before ')' in 'answer = iterate( test_value, times, some_process )'
Code: Select all
' function pointer with params test
' to learn about function pointers
' I am trying to make an iterator function
' that repeats r times
' on a given number g
' with process p
' and returns the result
function iterate _
( _
byval repeat as integer, _
byval given as integer, _
byref process() as integer as function ptr _
) as integer
? "Iterating " & repeat & " times with value " & given & "."
?
dim as integer value, result, accumulator = 0
value = given
dim as integer i
i = repeat
for i = 1 to repeat
result = process( value )
accumulator += result
next i
return accumulator
end function
function _increment( byval n as integer ) as integer
? "incrementing"
return n+1
end function
function _double( byval n as integer ) as integer
? "doubling"
return n*2
end function
dim times as integer
times = 5
dim test_value as integer
test_value = 2
dim as function some_process( byval n as integer ) as integer
some_process = @_increment()
dim answer as integer
answer = iterate( test_value, times, some_process )
? "the answer is " & answer
some_process = @_double
answer = iterate( test_value, times, some_process )
? "the answer is " & answer
sleep
end