Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Referential transparency

Name: Anonymous 2009-07-28 15:02

A function is RT <=> it can be substituted by its value in any place of the code, right? So what's the difference between a function being referentially transparent and not having side effects? Can I get an example of a function that is one but isn't the other one?

Name: Anonymous 2009-07-28 17:11

>>1
It's simple actually, a function could have side-effects during its execution(internally), but still be referetially transparent.

I'll give an example in C, as I don't know what languages you know:


Referentially transparent and pure:
int add1(int x)
{
    return (x+1);
}

Referentially transparent, has side-effects:

int add1(int x)
{
    return ++x; // x is incremented then returned,
                // however it does not affect anything
                // outside the context of this function
}

The second function has internal side-effects, but is referentially transparent.

The book "On Lisp" can show you more examples of this if you know some Lisp.
[/code]

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List