Answer the question
In order to leave comments, you need to log in
What is the difference between procedural and functional programming?
As a typical "dinosaur", I thought about the question of what is the difference between the two programming styles. From my dinosaur point of view, it's the same thing.
Or are there any such differences?
Answer the question
In order to leave comments, you need to log in
Example in JavaScript context:
Procedural style:
const array = ['first', 'second'];
let output;
function allOdd(words) {
let result = true;
for (let i = 0; i < words.length; ++i) {
const len = words[i].length;
if (len % 2 !== 0) {
result = false;
break;
}
}
return result;
}
output = allOdd(array);
alert(output);
function length(string) {
return prop('length', string);
}
function odd(number) {
return equals(modulus(number, 2), 0);
}
function allOdd(...words) {
return every(compose(odd, length), words);
}
alert(allOdd('first', 'second'));
FP does not imply mutable state.
Procedures change some general state.
In declarative programming, you make it clear: I want the factorial of n to be n times the factorial of n-1 (as in the definition of factorial in mathematics). Declarative is what.
const factorial = (n) => {
return (n === 0) ? 1 : n * factorial(n-1);
}
const factorial = (n) => {
const iter = (counter, acc) => {
return (counter === 0) ? acc : iter(counter - 1, counter * acc);
}
return iter (n, 1);
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question