www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - C style function pointers

reply "nikki" <nikkikoole gmail.com> writes:
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.
Aug 26 2014
next sibling parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
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));
Aug 26 2014
parent "nikki" <nikkikoole gmail.com> writes:
thanks, that worked, I need to grow a feeling for those * and &
Aug 26 2014
prev sibling parent ketmar via Digitalmars-d-learn <digitalmars-d-learn puremagic.com> writes:
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));
Aug 26 2014