Re: [Seed7-users] Functions & Procedures Confusion
Interpreter and compiler for the Seed7 programming language.
Brought to you by:
thomas_mertes
From: Thomas M. <tho...@gm...> - 2021-11-12 20:20:25
|
There are two things that use the keyword 'func'. 1. There are types like 'func integer' or 'func boolean'. 2. There is the 'func ... begin ... end func' construct. The type 'proc' is defined as 'func void'. The type 'void' describes that there is no value. In fact 'void' has a value, but there is just one value: 'empty'. So the type 'func void' aka 'proc' means: A function that returns nothing. In Pascal and other languages this is called procedure. In Seed7 the name function refers also to procedures as they are functions that return 'void'. The 'func ... begin ... end func' construct comes in several variants. An example of a procedure declarations is: const proc: main is func begin writeln("hello world"); end func; The 'func ... begin ... end func' construct is used to initialize the 'main' procedure. A variant of this func construct is used to define a procedure with local variables. E.g.: const proc: main is func local var integer: number is 0; begin for number range 10 downto 0 do writeln(number); end for; end func; Functions (that do not return void) are initialized with another variant of the construct: 'func result ... begin ... end func'. const func string: foo is func result var string: stri is ""; begin for 3 do stri &:= rand('a', 'z'); end for; end func; A function with a local variable is: const func string: bar is func result var string: stri is ""; local var integer: number is 0; begin for number range 1 to rand(1, 10) do stri &:= str(number); end for; end func; So 'func begin ...' and 'func local ...' are used to define procedures (aka proc). And 'func result ...' is used to define functions (except func void aka proc). As you figured out the 'func ...' construct is overloaded. There is another way to define a function. This way is used when an expression can describe the function body: const func integer: random10 is return rand(1, 10); I hope this helps. Regards Thomas |