Pointer to function in Euphoria

new topic     » topic index » view thread      » older message » newer message

Hi All, just coming to Euphoria planet and I am playing with it. I have this small example in C:

#include <stdio.h> 
#include <stdlib.h> 
 
double f(double x) 
{ 
  return x*x; 
}; 
 
double g(double x) 
{ 
  return 2*x; 
}; 
 
double h(double x) 
{ 
  return x*x*x; 
}; 
 
typedef double (*fp)(double); // Function pointer 
 
double doit(fp f, double a, double b) 
{ 
  return (*f)(a) + (*f)(b); 
} 
 
int main() 
{ 
    double y; 
    y = doit(&f, 3.14159, 2.71828); 
    printf( "y1 = %f\n", y); 
    y = doit(&g, 1.0,2.0); 
    printf( "y2 = %f\n", y); 
    y = doit(&h, 3.0,4.0); 
    printf( "y3 = %f\n", y); 
 
    return 0; 
} 

translated in Lua:

#!/usr/local/bin/lua 
 
function f(x) 
  return x*x 
end 
 
function g(x) 
  return 2*x 
end 
 
function h(x) 
  return x*x*x 
end 
 
 
function doit(f, a, b) 
 
  return f(a) + f(b) 
end 
 
sf=string.format 
y = doit(f,3.14159,2.71828) 
st = string.format("y1 = %f\n", y) 
print(st) 
y = doit(g, 1, 2) 
st = string.format("y2 = %f\n", y) 
print(st) 
y = doit(h,3,4) 
st = string.format("y3 = %f\n", y) 
print(st) 

How do you translate it in Euphoria? Thank You smile

Edit: Use three curly brackets {{{ and }}} to enclose non-Euphoria code or fixed-width content. -Greg

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu