Thread overview
C style function pointers
Aug 26, 2014
nikki
Aug 26, 2014
Marc Schütz
Aug 26, 2014
nikki
Aug 26, 2014
ketmar
August 26, 2014
I am trying top port this code :

float interpolate( float from, float to, float amount, float
(*easing)(float) )
{
     return from + ( to-from )*( easing( amount ) );
}

float linear_interpolation( float p )
{
     return p;
}


the "float (*easing)(float) " part needs to be rewritten as
"float function(float) easing" as far as I know and could find
here http://dlang.org/deprecate.html#C-style function pointers.

so my code is written as:

import std.stdio;

float interpolate(float from, float to, float amount, float
function(float) easing)
{
   return from + (to - from) * (easing(amount));
}

float lineair_interpolation(float p)
{
   return p;
}

void main()
{
   writeln(interpolate(100,100,10, lineair_interpolation));
}

this errors witha : Error: function test.lineair_interpolation
(float p) is not callable using argument types (), but I don't
really know where to go from here.
August 26, 2014
On Tuesday, 26 August 2014 at 12:26:45 UTC, nikki wrote:
> void main()
> {
>    writeln(interpolate(100,100,10, lineair_interpolation));
> }
>
> this errors witha : Error: function test.lineair_interpolation
> (float p) is not callable using argument types (), but I don't
> really know where to go from here.

You need to put an `&` before the function name:

    writeln(interpolate(100,100,10, &lineair_interpolation));
August 26, 2014
On Tue, 26 Aug 2014 12:26:43 +0000
nikki via Digitalmars-d-learn <digitalmars-d-learn@puremagic.com> wrote:

> this errors witha : Error: function test.lineair_interpolation
> (float p) is not callable using argument types (), but I don't
> really know where to go from here.
you need to use '&' to get function pointer. i.e.

  writeln(interpolate(100,100,10, &lineair_interpolation));


August 26, 2014
thanks, that worked, I need to grow a feeling for those * and &