Joshua Segal
Joshua Segal

Reputation: 573

What's the difference between calling a function and functional programming

I'm referring to this article https://codeburst.io/a-beginner-friendly-intro-to-functional-programming-4f69aa109569. I've been confused with how functional programming differs from procedural programming for a while and want to understand it.

function getOdds2(arr){
       return arr.filter(num => num % 2 !== 0)
    }

"we simply define the outcome we want to see by using a method called filter, and allow the machine to take care of all the steps in between. This is a more declarative approach."

They say they define the outcome we want to see. How is that any different from procedural programming where you call a function (a sub routine) and get the output you want to see. How is this "declarative approach" any different than just calling a function. The only difference I see between functional and procedural programming is the style.

Upvotes: 0

Views: 392

Answers (2)

Will Ness
Will Ness

Reputation: 71065

Procedural programming uses procedures (i.e. calls functions, aka subroutines) and potentially (refers to, and/or changes in place aka "mutates") lots of global variables and/or data structures, i.e. "state".

Functional programming seeks to have all the relevant state in the function's arguments and return values, to minimize if not outright forbid the state external to the functions used (called) in a program.

So, it seeks to have few (or none at all) global variables and data structures, just have functions (subroutines) passing data along until the final one produces the result.

Both are calling functions / subroutines, yes. That's just structured programming, though.

"Pure" functional programming doesn't want its functions to have (and maintain) any internal state (between the calls) either. That way the programming functions / subroutines / procedures become much like mathematical functions, always producing the same output for the same input(s). The operations of such functions become reliable, allowing for the more declarative style of programming. (when it might do something different each time it's called, there's no name we can give it, no concept to call it for).

Upvotes: 1

Strikegently
Strikegently

Reputation: 2441

With functional programming, everything revolves around functions. In the example you gave above, you are defining and executing a function without even knowing it. You're passing a lambda (anonymous function) to the filter function. With functional programming, that's really the point - defining everything in terms of functions, passing functions as arguments to functions, etc.

So yes, it is about style. It's about ease of expressing your ideas in code and being additionally terse.

Take this for example:

function getOdds(array) {
    odds = [];
    for(var i = 0; i < array.length; i++) {
        if(isOdd(array[i]) array.push(i);
    }
}

function isOdd(n) {
    return n % 2 != 0;
}

it gets the job done but it's really verbose. Now compare it to:

function getOdds(array) {
    array.filter(n => n % 2 != 0)
}

Here you're defining the isOdd function anonymously as a lambda and passing it directly to the filter function. Saves a lot of keystrokes.

Upvotes: 0

Related Questions