Search code examples
referencefunctional-programmingparameter-passingpurely-functional

Why purely functional languages allow passing parameters only by value?


I'm new to functional languages and I was wondering why we can't pass a parameter by reference. I found anserws saying that

you are not supposed to change the state of objects once they have been created

but I didn't quite get the idea.


Solution

  • I think you have misunderstood the concept. Both Scheme and C/C++ are pass by value languages and most values are addresses (references).

    Purely functional languages can have references and those are passed by value. What they don't have is redefining variables in the same scope (mutate bindings) and they don't have the possibility to update the object the reference points to. All operations return a fresh new object.

    As an example I can give you Java's strings. Java is not purely functional but its strings are. If you change the string to uppercase you get a new string object in return and the original one has not been altered.

    Most languages I know of are pass by value. Pass by name is alien to me.