3 ms·
I think the main difference is that, in a declarative statement like: x = (if c then a else b) it is guaranteed that only x is affected. With imperative p
by quietbritishjim 4y ago
I think the main difference is that, in a declarative statement like:
x = (if c then a else b)
it is guaranteed that only x is affected. With imperative programming, you have to look inside the conditional to find what is affected.
if c then:
x = a
else:
y = a
You could still make the above snippet work in a declarative way but, as I say, the targets are explicit
x, y = (if c then a, y else x, a)
If you have function calls involved then the problem is even worse. In an imperative language, the following code could mutate x, y and any other variables arbitrarily (if the scoping rules allow it):
x = 0
if c then:
f()
Whether this difference is enough to describe "if" as not "control flow" in declarative languages, I don't know, but that is not really an interesting question (just a matter of how you define the term).
Edit: another difference is that I think declarative languages usually only use immutable types, so changing one variable is guaranteed not to affect another. Contrast with this Python snippet, where mutating x affects y.
x = [1, 2, 3]
y = [x, x]
x.append(4)