Call a foreign-language function: Difference between revisions

m
→‎{{header|Mercury}}: #include <string.h> rather than declrare strdup
m (→‎{{header|Mercury}}: #include <string.h> rather than declrare strdup)
Line 741:
 
% The actual FFI code begins here.
:- pragma foreign_decl("C", "char#include *strdup(const char *s);<string.h>").
 
:- func strdup(string::in) = (string::out) is det.
Line 756:
Only the lines wrapped in comments matter for this. The rest is an application skeleton so this can be compiled and tested.
 
The first pragma, foreign_decl, inserts C code into the output of the compiler. Here the C function's type is declared. Other things that can be put in this pragma include globally-accessed macros, functionsfunction declrations, variablesvarabile declrations, etc.
 
After this the Mercury strdup/1 function itself is declared. For purposes of exposition it has been declared fully with types and modes. The modes, however, are redundant since by default functions in Mercury have all input parameters and an output return value. Also, the determinism is declared which is again redundant. By default Mercury functions are deterministic. That line could easily have been written thusly instead:
Anonymous user