Difference between C and functional programming

Hi,

I would like to know the difference between c and functional programming languages. I read that c is imperative language in the sense that it sequential executes the instructions, changing the state (memory?). And functional language is more like a math function f(x). Functional programming and formal systems seem to be connected. I tried grasping about formal systems, but could not get a clear picture. So what is functional programming and what is so "formal" about it.

Thank you.
 
If you want to know the basics of theory, you should look up "lambda calculus" (it's like the turing machine of functional languages, means no one is programming like this, but it's good for understanding). Shortly explained, all you need to get a language turing complete are three concepts:
  1. variables
  2. application (+parentheses)
  3. abstraction

Nothing more really matters. You can easily show that you can do recursion with these 3 rules above, e.g. by constructing the so-called omega or the Y combinator. So you can call functions, use parameters and do recursion (implicitly also iteration). Now you just need numbers to be happy. And you can do this with "church numerals". This is quite inefficient, but shows that the full concept can be done with the restricted set of rules above.

Functional programming is simply a different programming paradigm. Like object oriented programming (when you think about an if statement as a function that returns the result of the then or else branch, you are already thinking like someone who is doing functional programming). You can also do functional programming in JavaScript. But I would recommend to write a few small programs in the languages:
  • ocaml (not purely functional, but a very nice language, ancestor of ML)
  • lisp
  • haskell
 
In my probably-flawed mental picture, it's the difference between a script and a spreadsheet, both adding up a column of numbers. In the script, there's a series of sequential steps. In the spreadsheet, there's a function in a cell. In the script, all the steps have to be in the right order. In the spreadsheet, time doesn't matter, functions just refer to data and other functions.
 
Functional programming, in its purest form, represents program execution as an evaluation of a certain expression. In other words, a program is just a very complicated expression which is evaluated at run-time and the result of that evaluation is the program's output.

Functions in functional programming are "pure", i.e. they can't have side-effects. In that respect they resemble mathematical functions, e.g. sin, cos, etc.

This is somewhat impractical, since all this means that you cannot have an assignment, and I/O operations are at least cumbersome (Monads in Haskell). Some functional languages do have an assignment (e.g. SML), but they lose the "pure" functional character.
 
Back
Top