Re: [Seed7-users] Pointer to function as argument
Interpreter and compiler for the Seed7 programming language.
Brought to you by:
thomas_mertes
|
From: Thomas M. <tho...@gm...> - 2020-11-22 17:33:11
|
In the example below the arguments 'a' and 'b' are read at runtime:
-------------------- begin l_skov.sd7 --------------------
$ include "seed7_05.s7i";
const proc: tester (in var integer: a, in var integer: b, ref func integer: fun) is func
begin
writeln("Value of fun: " <& fun);
writeln("end");
writeln("inside: a = " <& a);
writeln("inside: b = " <& b);
end func;
const proc: main is func
local
var integer: a is 0;
var integer: b is 0;
begin
write("a? ");
readln(a);
write("b? ");
readln(b);
tester(a, b, a+b);
end func;
-------------------- end l_skov.sd7 --------------------
The function 'tester' can also work without the parameters 'a' and 'b':
-------------------- begin l_skov2.sd7 --------------------
$ include "seed7_05.s7i";
const proc: tester (ref func integer: fun) is func
begin
writeln("Value of fun: " <& fun);
end func;
const proc: main is func
local
var integer: a is 0;
var integer: b is 0;
begin
write("a? ");
readln(a);
write("b? ");
readln(b);
tester(a+b);
tester(a*b);
end func;
-------------------- end l_skov2.sd7 --------------------
Regarding your 2nd question:
You can write
boolean parse "TRUE"
because the syntax of the 'parse' operator is defined in the file syntax.s7i with:
$ syntax expr: .().parse.() is <- 1;
The syntax of function calls like
writeln("something")
is not defined explicit (in e.g. syntax.s7i).
Generally the syntax of all function calls
function_name(parameter1, parameter2)
is hard coded. The hard coded syntax for function calls
requires parentheses around the parameters.
If there is a syntax definition then parentheses can be omitted.
E.g.: The 'not' operator is defined in syntax.s7i with:
$ syntax expr: .not.() is <- 13;
This allows you to write
not some_expression
without parentheses.
Regards,
Thomas Mertes
--
Seed7 Homepage: http://seed7.sourceforge.net
Seed7 - The extensible programming language: User defined statements
and operators, abstract data types, templates without special
syntax, OO with interfaces and multiple dispatch, statically typed,
interpreted or compiled, portable, runs under linux/unix/windows.
|