Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Defining point of functional programming

Tags:

I can enumerate many features of functional programming, but when my friend asked me Could you define functional programming for me? I couldn't.

like image 228
sumek Avatar asked Oct 18 '08 11:10

sumek


People also ask

Which is a key feature of functional programming?

Functional programming supports higher-order functions and lazy evaluation features. Functional programming languages don't support flow Controls like loop statements and conditional statements like If-Else and Switch Statements. They directly use the functions and functional calls.

What is defining a function in programming?

A function is simply a “chunk” of code that you can use over and over again, rather than writing it out multiple times. Functions enable programmers to break down or decompose a problem into smaller chunks, each of which performs a particular task.

What is functional programming in simple words?

Functional programming is a paradigm of writing code and is eloquently put in the introduction of this Wikipedia article: “ -a style of building the structure and elements of computer programs — that treats computation as the evaluation of mathematical functions and avoids changing-state and mutable data.


2 Answers

I would say that the defining point of pure functional programming is that all computation is done in functions with no side effects. That is, functions take inputs and return values, but do not change any hidden state, In this paradigm, functions more closely model their mathematical cousins.

This was nailed down for me when I started playing with Erlang, a language with a write-once stack. However, it should be clarified that there is a difference between a programming paradigm, and a programming language. Languages that are generally referred to as functional provide a number of features that encourage or enforce the functional paradigm (e.g., Erlang with it's write-once stack, higher order functions, closures, etc.). However the functional programming paradigm can be applied in many languages (with varying degrees of pain).

like image 151
John Stauffer Avatar answered Oct 01 '22 01:10

John Stauffer


A lot of the definitions so far have emphasized purity, but there are many languages that are considered functional that are not at all pure (e.g., ML, Scheme). I think the key properties that make a language "functional" are:

  1. Higher-order functions. Functions are a built-in datatype no different from integers and booleans. Anonymous functions are easy to create and idiomatic (e.g., lambdas).
  2. Everything is an expression. In imperative languages, a distinction is made between statements, which mutate state and affect control flow, and expressions, which yield values. In functional languages (even impure functional languages), expression evaluation is the fundamental unit of execution.

Given these two properties, you naturally get the behavior we think of as functional (e.g., expressing computations in terms of folds and maps). Eliminating mutable state is a way to make things even more functional.

like image 28
Chris Conway Avatar answered Oct 01 '22 03:10

Chris Conway