[Seed7-users] parameter types
Interpreter and compiler for the Seed7 programming language.
Brought to you by:
thomas_mertes
From: Thomas M. <tho...@gm...> - 2012-11-13 15:06:30
|
On Tue, 13 Nov 2012 13:15:04 +0100, joh...@so... wrote: > hi, > > i have a very basic question about parameter types. i don't understand > them completely from your description in the FAQ. > > "val", "ref" and "in" are obvious. what about the difference between "in > var" and "inout"? > > i made an assumption here, and that is that "in var" is similar to "var v: > t" in pascal, ... No. "in var" is similar to "v: t" in Pascal. It is a call-by-value parameter, which can be changed inside the function. An assignment to a formal "in var" parameter is allowed, but it has only an effect inside the function. The actual parameter can be an expression or a variable and it cannot be changed from inside the function. A "val" parameter is also a call-by-value parameter, but it cannot be changed in the function (no assignment allowed). > while "inout" is similar to "var v: ^t" in pascal. am i > right? or are the semantics different? if so, could you explain them in > details? For simple types like integer, float and char an "inout" parameter is similar to "var v: t" in Pascal. For structured types like string, struct, hash and array an "inout" parameter is similar to "var v: ^t". But this difference ("var v: t" vs. "var v: ^t") is hidden from the user (there is no need to dereference explicitly). For an "inout" parameter the actual parameter must be a variable. It is allowed to do an assignment to the formal parameter in the function and this change takes an effect on the actual parameter. Summary: Assignments to formal "in var" and "inout" parameters are allowed. For "in var" the actual parameter stays unchanged. For "inout" the actual parameter changes immediately. Assignments to formal "val", "ref" and "in" parameters are not allowed. It seems that I need to improve the FAQ and the manual. Regards, Thomas Mertes |