Re: [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 14:08:37
|
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 Sorry. My last mail contained a little typo in the third variable table (The re and im members of the variable b must be swapped). To avoid confusion I resend the example: -------------------------------------------------- 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 | 6.0 | 3.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". -------------------------------------------------- Sorry for the inconvenience. Regards, Thomas Mertes |