Re: call_func()

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

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

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

Search



Quick Links

User menu

Not signed in.

Misc Menu