1. call_func()
- Posted by DonCole Jan 22, 2016
- 1710 views
Hello All,
Can anyone give me a clear example of call_func().
I can't seem to find one in the examples.
I found:
E:\EU\win32lib\Demo\hash.e(547):
lCallBackResult = call_func(sMode, {sKey, f_Fetch(), sNewEntry})
But I don't understand this.
If someone could parse this line, I would appreciate it.
The following is the code I'm trying convert to call_func()
function Routine(integer x) SOURCE=mytrim(getText(EditText2)) TARGET=mytrim(getText(EditText3)) too=TARGET&Dir[x] printf(1,"too=>>%s<<\n",{too}) from=sprintf(SOURCE&"%s",{Dir[x]}) printf(1,"from=>>%s<<\n",{from}) printf(1,"SOURCE%s\n",{SOURCE}) return {from,too} end function
Any help on this would also be appreciated.
Thank you, Don Cole
2. mentsRe: call_func()
- Posted by SDPringle Jan 23, 2016
- 1680 views
Then routine call_func, takes an integer which represents a routine as the first argument and a sequence of all of its arguments you pass to it:
integer y = Routine(4)
becomes
integer r_id = routine_id("Routine") integer y = call_func(r_id, {4})
3. mentsRe: call_func()
- Posted by DonCole Jan 23, 2016
- 1685 views
Then routine call_func, takes an integer which represents a routine as the first argument and a sequence of all of its arguments you pass to it:
integer y = Routine(4)
becomes
integer r_id = routine_id("Routine") integer y = call_func(r_id, {4})
Thank you SDPringle,
This seems to do the trick.
I wish the docs were clearer on this.
Don Cole
4. mentsRe: call_func()
- Posted by andi49 Jan 23, 2016
- 1651 views
Then routine call_func, takes an integer which represents a routine as the first argument and a sequence of all of its arguments you pass to it:
integer y = Routine(4)
becomes
integer r_id = routine_id("Routine") integer y = call_func(r_id, {4})
Thank you SDPringle,
This seems to do the trick.
I wish the docs were clearer on this.
Don Cole
Hi
what's wrong with the manual?
http://openeuphoria.org/docs/std_machine.html#_5813_call_proc
http://openeuphoria.org/docs/std_machine.html#_5805_call_func
Andreas
5. mentsRe: call_func()
- Posted by SDPringle Jan 24, 2016
- 1635 views
I think sometimes gaping holes in detail can be missing but the authors who write it will see it as complete. What seems understandable for the author might not be for the reader. Okay, call_func is missing an example and the example for call_proc has a function x which only complicates things. And it would be good to have a comment that says this is equivalent to yada yada above the call_proc call.
6. Re: call_func()
- Posted by petelomax Jan 25, 2016
- 1614 views
Suggestion:
call_proc(integer rid, sequence params)
Description:
Dynamically invoke the user-defined procedure with routine_id rid.
rid must be a valid routine id returned by routine_id(), and params must be a sequence of argument values of length n, where n is compatible with the number of arguments required by procedure rid.
Comments:
Calling a routine directly is known as a static call: it is determined at compile-time and cannot be changed at run-time. Conversely, invoking a routine via a routine_id is known as a dynamic call: the value of the routine_id can be altered at run-time and hence the routine being called can be altered at run-time. While the trivial example below shows no benefits, the ability to store routine_ids in tables (etc) is much more flexible, and often quite necessary for gui programming. Historically, a routine_id was the only way to effect a forward call, but that is no longer the case.
If procedure rid does not take any arguments then params should be {}.
Example:
procedure foo(integer a, sequence s) puts(a, s) end procedure integer r_foo = routine_id("foo") foo(1,"Hello World\n") -- direct/static call call_proc(r_foo,{1,"Hello World\n"}) -- indirect/dynamic call (equivalent)
Improvements welcome
7. Re: call_func()
- Posted by _tom (admin) Feb 01, 2016
- 1587 views
MiniGuide: Calling a Routine
A Routine is a Sub-Program
Routines are sub-programs found inside your program code. Euphoria has three kinds of routines: procedure, function, and type. These routines are how we break up large complex programs into a collection of smaller, easier to understand, and easier to use blocks of code. A routine is a "named block of code that exists aside from your main program, and can only execute when a call to that routine is made."
Routines are written with similar syntax and differ based on the return value. A procedure can have an optional parameter list, executes a block of code, but does not return a value. A function can have an optional parameter list, executes a block of code, and must return a value. A type, a specialized function, has one parameter and must return either 0 False or 1 True. You can make a function behave like a procedure by quietly ignoring the returned value.
Where Routines are Found
Routines are found:
- Top-Level (user written code) in your main program, included files, and standard library files.
- Shared (compiled C code) found in .dll or .so library files.
- Machine ( internal code) found in the Euphoria Interpreter.
Machine routines are only used by expert Euphoria programmers in a few rare cases. A routine identifier is found by reading source/execute.h from the Interpreter source-code. Examples of machine routines can be found in the standard library.
Routines are "Called"
Execute the code in a routine by making a call in a program statement. In a direct call "you write the name of the routine in a program statement." In an indirect call "you first get a rid (routine identifier) value for the routine, and then use the rid in a special calling routine." A direct call is static because the routine selection if fixed before the program runs, while an indirect call is dynamic because it is possible to select a routine as the program runs. Your choice of calling style does not change how a routine executes its code.
Indirect Procedure Call
Top-Level | Shared | Machine | |||
---|---|---|---|---|---|
create |
procedure show() printf(1,"hello") end procedure |
create |
// mylib.c include <stdio.h> void show() { printf("hello") } |
||
compile | gcc -shared mylib.c -o mylib.so | ||||
open |
include std/dll.e integer mylib = open_dll("./mylib.so") |
||||
rid |
integer r_id = routine_id("show") |
rid |
atom r_id = define_c_proc(mylib,"show",{}) |
rid | source/execute.h |
call |
call_proc(r_id) |
call |
c_proc(r_id,{}) |
call |
machine_proc(r_id, arguments) |
result | hello | result | hello |
For a call to a shared routine write {} when there is an empty parameter list.
Indirect Function Call
Top-Level | Shared | Machine | |||
---|---|---|---|---|---|
create |
procedure Sqr(atom x) return x*x end procedure |
create |
// mylib.c double Sqr(double x) { return x*x; } |
||
compile | gcc -shared mylib.c -o mylib.so | ||||
open |
include std/dll.e integer mylib = open_dll("./mylib.so") |
||||
rid |
integer r_id = routine_id("Sqr") |
rid |
atom r_id = define_c_func(mylib,"Sqr",{C_DOUBLE},C_DOUBLE) |
rid | source/execute.h |
call |
? call_func(r_id,{5}) |
call |
? c_func(r_id,{5}) |
call |
result = machine_func(r_id, arguments ) |
result | 25 | result | 25 |
When defining the rid to a shared routine provide exact 'C' data-types in the parameter list, and a data-type for the return value.
_tom