FiveTech Support Forums

FiveWin / Harbour / xBase community
Board index FiveWin para Harbour/xHarbour Pasar una funcion como parametro a otra funcion en un PRG...
Posts: 817
Joined: Sun Jun 15, 2008 07:47 PM
Pasar una funcion como parametro a otra funcion en un PRG...
Posted: Sat May 26, 2018 09:14 AM
Alguien sabe si se puede crear un puntero a una función y pasarlo como parámetro a otra donde será ejecutada...

En C es fácil pero en harbour alguien sabe cómo hacerlo?

Algo así:
Code (fw): Select all Collapse
procedure main

    local pFunc, c
    
    pFunc := miFuncion()
    
    c := ejecutaFunc( pFunc, "Otra prueba" )
    
    Alert( c )

return

//------------------------------------------------------------------------------

static function ejecutaFunc( pf, cValue )

    local c := "Funcion ejecutaFunc() -> "
    
    c += pf( cValue )
    
return c


static function miFuncion( cParam )

    local cCadena := "Funcion miFuncion() -> "
    
    cCadena += cParam

return cCadena

//------------------------------------------------------------------------------


No sé si está entendido...
Ojo, sin usar codeblock ni macros...
______________________________________________________________________________

Sevilla - Andalucía
Posts: 1515
Joined: Thu Oct 30, 2008 02:37 PM
Re: Pasar una funcion como parametro a otra funcion en un PRG...
Posted: Sat May 26, 2018 10:47 AM
Hola Manuel,

"Another interesting innovation in the Harbour - pointers to functions. They can be created on the stage of building the application, using the expression @<funcName>(), or dynamically during execution with the help of the macro: &("@<funcName>()"). Valtype() returns S for such pointers."

Code (fw): Select all Collapse
proc main()
   local funcSym
   funcSym := @str()
   ? funcSym:name, "=>", funcSym:exec( 123.456, 10, 5 )
   funcSym := &( "@upper()" )
   ? funcSym:name, "=>", funcSym:exec( "Harbour" )
return


Desde: http://www.kresin.ru/en/hrbfaq_3.html

Salu2
Posts: 10733
Joined: Sun Nov 19, 2006 05:22 AM
Re: Pasar una funcion como parametro a otra funcion en un PRG...
Posted: Sun May 27, 2018 10:27 AM
hmpaquito wrote:Hola Manuel,

"Another interesting innovation in the Harbour - pointers to functions. They can be created on the stage of building the application, using the expression @<funcName>(), or dynamically during execution with the help of the macro: &("@<funcName>()"). Valtype() returns S for such pointers."

Code (fw): Select all Collapse
proc main()
   local funcSym
   funcSym := @str()
   ? funcSym:name, "=>", funcSym:exec( 123.456, 10, 5 )
   funcSym := &( "@upper()" )
   ? funcSym:name, "=>", funcSym:exec( "Harbour" )
return

Desde: http://www.kresin.ru/en/hrbfaq_3.html

Salu2


Very good.
But this approach works only if the function is already linked to the application. Otherwise we get link errors.

There are times when we want a function to be executed, only if the function is already linked to the application and if not not to execute or do some other action.

Code (fw): Select all Collapse
pMyFunc := HB_FUNCPTR( "MYFUNC" )
if Empty( pMyFunc )
   // not linked
else
  HB_ExecFromArray( pMyFunc, { param1, param2, ..., paramN } )
endif
Regards



G. N. Rao.

Hyderabad, India

Continue the discussion