[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-15 13:51:25
|
On Wed, 14 Nov 2012 02:31:09 +0100, joh...@so... wrote: > so can you assign to a member of a structured type passed as an actual > parameter, if using "in var"? if so, this is different from pascal "v: t" > which makes a value copy. "in var" does a value copy. For array and structured types this means that the whole array respectively struct is copied (it does a so-called deep copy). In the function the parameter can be changed, either by assigning a value to the parameter or (if it is structured) by assigning a value to a member of the structure. BUT: All the changes inside the function have no effect outside of the function. This means: The actual parameter stays unchanged. The following example shows the differences between "in var" and "inout": The type complex is defined in the library "complex.s7i". This library contains, beside other things, the definition of the following struct: const type: complex is new object struct var float: re is 0.0; var float: im is 0.0; end struct; Now we assume the following three variables were defined: var complex: a is complex(0.0, 1.0); var complex: b is complex(2.0, 3.0); var complex: c is complex(4.0, 5.0); The members re and im get the corresponding values from the initialization: | value | value variable | of re | of im ----------+-------+------- a | 0.0 | 1.0 b | 2.0 | 3.0 c | 4.0 | 5.0 The following function uses "in var" parameters: const proc: func1 (in var complex: par1, in var complex par2) is func begin par1 := c; # Assign new value par2.re := 6.0 # Assign new value to member # Now the parameters par1 and par2 have the new values. # The variables a, b and c stay unchanged. end func; Then the function func1 is called with the actual parameters a and b: func1(a, b); Afterwards the variables a, b and c still have the same values as before: | value | value variable | of re | of im ----------+-------+------- a | 0.0 | 1.0 b | 2.0 | 3.0 c | 4.0 | 5.0 With "inout" parameters this picture changes. The following function uses "inout" parameters: const proc: func2 (inout complex: par1, inout complex par2) is func begin par1 := c; # Assign new value par2.re := 6.0 # Assign new value to member # Now the parameters par1 and par2 have the new values. # The variables a and b were changed together with par1 and par2. # Variable c stays unchanged. end func; Then the function func2 is called with the actual parameters a and b: func2(a, b); Afterwards the values of the variables have changed: | value | value variable | of re | of im ----------+-------+------- a | 4.0 | 5.0 b | 2.0 | 6.0 c | 4.0 | 5.0 I hope this explanation helps. As you can see Seed7 parameters work in an abstract level. In many situations it is not necessary to think about implementation details like "by reference", "by value" or "address of". > yeah i think improving the FAQ entry is a good idea. just extend the table > to say what happens when you assign to a simple type, structured type, and > structure type members, for each of the four parameter types. I will look at that. Regards, Thomas Mertes |