void get_factorial(gg_num f, gg_num *res)
{
*res = 1;
gg_num i;
for (i = 2; i <= f; i++) {
*res *= i;
}
}
void fact_msg (gg_num i, char **res)
{
// golf rule: outgoing string must NOT free or realloc its incoming value
// all else is allowed
char *r = gg_malloc (MEMSIZE);
gg_num f;
get_factorial (i, &f);
gg_num bw = snprintf(r, MEMSIZE, "Factorial value (message from C function) is %ld", f) + 1;
// reduce memory footprint to match the memory used (including the null byte)
// you can also use gg_mem_set_len() with bw as length for better performance
// (but also higher memory usage)
*res = gg_realloc (gg_mem_get_id(r), bw);
}
Copied!
extended-mode
begin-handler /example public
set-number fact
call-extended get_factorial (10, &fact)
@Factorial is <<print-out fact>>
set-string res
call-extended fact_msg (10, &res)
print-out res
@
end-handler
Copied!