3

I am learning functional programming and I can understand why immutability is preferred over mutable objects.

This article also explains it well.

But I am unable to understand why assignments should be performed inside of pure functions.

One reason that I can understand is variable mutability leads to locking and since in a pure function in scala we mostly tail recursion and this creates variables/objects on the call stack rather than a heap.

Is there any other reason why one should avoid variable assignment in functional programming.

9
  • 1
    A pure function depends only on the parameters given to it. This means that the function returns the same result every time you call it with the same parameters. If the function uses a variable defined outside the function, then it will not return the same result every time. It depends on the variable! Commented Feb 13, 2017 at 17:45
  • take a look at stackoverflow.com/questions/1791408/… Commented Feb 13, 2017 at 17:46
  • What if the variable is local to the function and is not used elsewhere? Commented Feb 13, 2017 at 17:46
  • 2
    If the function returns the same result every time (with no side effects) given the same parameters, it is (externally) pure. Commented Feb 13, 2017 at 17:48
  • 2
    I found this question that answers your question: softwareengineering.stackexchange.com/questions/196112/… Commented Feb 13, 2017 at 17:49

1 Answer 1

8

There is a difference between assignments and re-assignments. re-assignments are not allowed in functional programming because its mutability is not allowed in pure functions.Variable assignment is allowed.

val a = 1 //assignment allowed

a = 2 //re-assignment not allowed

Reading in a impure fashion (changing state) from the external world is a side-effect in functional programming.

So, function accessing a global variable which can potentially be mutated is not pure.

Just makes life easy.

Generally

When you are disciplined life is less chaotic. Thats exactly what functional programming advocates. When life is less chaotic you can concentrate on better things in life.

So, the main reason for immutability

It becomes hard to reason about the correctness of the program with mutations. In case of concurrent programs this is very painful to debug.

that means it becomes hard to keep track of changes variables undergo in order to understand the code/program or to debug the program.

Mutation is one of the side effect where which makes program hard to understand and reason about.

Functional programming enforces this discipline (use of immutability) so that code is maintainable, expressive and understandable.

Mutation is one of the side effects

Pure function is that one which does not have side effects.

Side effects:

  1. Mutation of variables
  2. Mutation of mutable data structures
  3. Reading or writing to a file/console (external source)
  4. Throwing exceptions to the halt program

Avoiding above mentioned side effects makes a function depend only on the parameters of the function rather than any outside values or state.

Pure function is the most isolated function which neither reads from the world nor writes to the world. It does not halt or break the program control flow.

The above properties make the pure function easy to understand and reason about.

Pure function is mathematical function

Its a mapping from co-domain to range where every value in co-domain is mapped to exactly one value in range.

That means if f(2) is equal to 4 then f(2) is 4 irrespective of what the state of the world is.

Pure function is a relation between a set of inputs and a set of permissible outputs with the property that each input is related to exactly one output.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.