Do pure functions solve the "return const" problems?
Bruno Medeiros
brunodomedeiros+spam at com.gmail
Sun Apr 27 11:53:33 PDT 2008
Christopher Wright wrote:
> Janice Caron wrote:
>> On 27/04/2008, Christopher Wright <dhasenan at gmail.com> wrote:
>>> An out parameter is a return value. What else is there to get?
>>
>> An out parameter is global state. Sometimes literally:
>>
>> void foo(out int x, int y) { x = y; }
>>
>> foo(globalVariable, 3);
>
In this example, a side effect occurs because of the evaluation of
"foo(globalVariable, 3)".
> Then the calling function isn't pure!
> I could also do:
> int foo(int y) { return y; }
> globalVariable = foo(3);
>
In this example, no side effect occurs because of the evaluation of
"foo(3)". The side effect only occurs when globalVariable is assigned,
which happens outside of foo. Thus, foo remains pure. _And this little
difference allows for several compiler optimizations._
--
Bruno Medeiros - Software Developer, MSc. in CS/E graduate
http://www.prowiki.org/wiki4d/wiki.cgi?BrunoMedeiros#D
More information about the Digitalmars-d
mailing list