Having trouble learning function pointers [SOLVED]

New to FreeBASIC? Post your questions here.
Post Reply
elsairon
Posts: 207
Joined: Jul 02, 2005 14:51

Having trouble learning function pointers [SOLVED]

Post by elsairon »

I'm trying to make a function that accepts some values and another function as a parameter, to learn about function pointers.

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
Last edited by elsairon on May 08, 2011 1:17, edited 1 time in total.
MichaelW
Posts: 3500
Joined: May 16, 2006 22:34
Location: USA

Post by MichaelW »

Something like this ?

Code: Select all

'===========================================================
function Iterate( _
                count as integer, _
                value as integer, _
                func as function( as integer ) as integer _
                ) as integer
    dim as integer accum
    for i as integer = 1 to count
        accum += func( value )
    next
    return accum
end function

function Inc( i as integer ) as integer
    return i + 1
end function

function X2( i as integer ) as integer
    return i * 2
end function

print Iterate( 2, 4, @Inc )
print Iterate( 2, 4, @X2 )
sleep

elsairon
Posts: 207
Joined: Jul 02, 2005 14:51

Post by elsairon »

MichaelW wrote:Something like this ?
Yes. Thank you Michael.
Post Reply