• Post Reply Bookmark Topic Watch Topic
  • New Topic

functional vs procedural programming  RSS feed

 
Grega Leskovšek
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a difference between the two? If what, when is one preffered over the other and what are the key differences, principles?
Thanks, Gregor
 
Piotr Kalinowski
Greenhorn
Posts: 6
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, there is very big difference. Procedural programming means that you break programs into smaller pieces - procedures. Computations are made, generally, in sequence, with control structures like conditional instructions, iterations etc. In functional programming, main computations are made by calling functions which return values. And as in functions in mathematics, generally you don't care, what happens inside the function - only the result is important - there are no side effects.
I think the key differences are immutability and recursion instead of iteration.
(This is an informal explanation. For more strict definition see wikipedia: http://en.wikipedia.org/wiki/Functional_programming).
 
Grega Leskovšek
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was referring mostly on Pascal, that had both functions and procedures and in fact procedures are just functions that do not return value.
So the difference in meta language would be:
procedure
function pa(a,b) {
print a+b;
return;
}

function
function fa(a,b) {
return a+b;
}

call
pa(1,2);
print fa(1,2)

Am I wrong? And is that not the same functionality? Thanks, Gregor
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's a quick overview of what makes FP different.
 
Grega Leskovšek
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Chris, the link is dead ... Please update it. Thanks, Gregor
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Grega Leskovšek wrote:Chris, the link is dead ... Please update it. Thanks, Gregor

Works for me, but here it is again: http://cs.lmu.edu/~ray/notes/functionalprogramming/
And with a fixed IP address: http://157.242.71.25/~ray/notes/functionalprogramming/
 
Grega Leskovšek
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not Found

The requested URL /~ray/notes/functionalprogramming/ was not found on this server.
Apache/2.4.10 (Ubuntu) Server at cs.lmu.edu Port 80

How is this possible? Love, Grega
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Grega Leskovšek wrote:Not Found

The requested URL /~ray/notes/functionalprogramming/ was not found on this server.
Apache/2.4.10 (Ubuntu) Server at cs.lmu.edu Port 80

How is this possible? Love, Grega

Dunno. Are you behind a firewall or something that might be affecting your HTTP calls? Have you tried copying the full URL into your browser? Sorry, but I can't replicate the problem so I can't really help.
 
Grega Leskovšek
Ranch Hand
Posts: 79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am at home, where I have firewall on Linux and router Linksys.
I've also tried to copy the url to browser but I always get 404.
And I am in Slovenia, Europe.
ping http://cs.lmu.edu/~ray/notes/functionalprogramming/
ping: unknown host http://cs.lmu.edu/~ray/notes/functionalprogramming/


What else can I do? Always, Grega
 
chris webster
Bartender
Posts: 2407
33
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Some kind of DNS problem? Here's my ping results from the UK:
 
Jack Dwaltz
Greenhorn
Posts: 17
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Two key ingredients of functional programming are:
  • to be able to pass functions as arguments to other functions
  • use functions that have no "side effects"

  • The ability to pass functions as arguments to functions enables you to write simple solution for some problems, e.g. based on recursion etc.
    I will not go into details here.

    For the second point: a function has a side effect when it changes a shared state.
    In OO a shared state is, e.g., the internal state of any object shared among different threads, in procedural programming the shared state is in global variables or variables shared with pointers among threads.

    To create a function with no side effects, instead of writing the results it computes in the objects or referenced variables it receives, simply put the results in new objects or variables.

    E.g.: a function that computes sqrt(x) can be easily written with no side effects: you simply use local variable in your computation and return the computed value in a new variable not affecting the input value x.
    Another example: if you want to write a "no side effect" sorting of a list you don't use the method sort() on the object representing the list, rather you define a function sort() that accepts a list object, copies its content in a new list, sorts that, and returns it.

    Why would you want to do that? Simply because in this way you can write concurrent code without doing heavy synchronization.
    On modern computers we have multi-core cpus, to use them at best we must multithread efficiently, so any code should try to minimize locking for synchronization as much as possible.
    If you return all your computed result as new objects or values, and never modify anything you receive in input, you can write algorithms that scale naturally with no need of locking to implement synchronization.
     
    Rico Felix
    Ranch Hand
    Posts: 411
    5
    IntelliJ IDE Java Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    One must realize that reality is based on numbers which implies relations to maths...

    A procedure is a well defined number of steps to accomplish some task...
    If we want to derive at say 3 items in a bag we can place an item into the bag three times resulting in 3 steps to accomplish the task...
    Here we are concerned about the side effect from performing such steps a specific number of times which affects the bag's contents... Our focus is on the modified contents of the bag...

    A function is a well defined operation to derive at a result...
    Focus is transitioned from modification of some entity to the actual operation and result...
    Example: We have the following functions

    f(x) = x * x
    g(y) = y + f(y)

    If you carefully observe these functions you'd realize that we are not concerned if they are modifying some external entity...
    Our only concern is on the actual operation and result from that operation... Therefore all we know about these functions is that f(x) is the square of x and g(y) is the square of y plus y...

    Observing these aspects about functions provides excellent employment in concurrent operations since we are not modifying an entity but only retrieving some result produced eliminating the need for synchronizations...
     
    chris webster
    Bartender
    Posts: 2407
    33
    Linux Oracle Postgres Database Python Scala
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Grega Leskovšek wrote:What else can I do? Always, Grega


    How about this article by Robert Martin?
     
    Ahsan Bagwan
    Ranch Hand
    Posts: 254
    1
    Java MySQL Database Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Well, I think Jack has put it nicely. I'm new to functional programming and that explanation is helpful.
     
    Sean Corfield
    Ranch Hand
    Posts: 312
    14
    Clojure Linux Mac OS X Monad
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    chris webster wrote:
    Grega Leskovšek wrote:Chris, the link is dead ... Please update it. Thanks, Gregor

    Works for me, but here it is again: http://cs.lmu.edu/~ray/notes/functionalprogramming/

    FWIW, it does not work for me either - it reflects to 127.0.0.1 somehow, despite ping showing 157.242.71.25 as the IP.

    If I use the URL with the IP address instead, it works: http://157.242.71.25/~ray/notes/functionalprogramming/
     
    chris webster
    Bartender
    Posts: 2407
    33
    Linux Oracle Postgres Database Python Scala
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Sean - good point.

    I've edited my links above to use the IP address, just in case people don't read this far down the thread.
     
    Deepak Lal
    Ranch Hand
    Posts: 561
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hi,
    I am unable to understand the below example about Java Programming (http://157.242.71.25/~ray/notes/functionalprogramming/)

    In Java:



    2> Can you convert the above example to Functional Programming style and explain it step by step?


    Regards,
    Deepak Lal

     
    Tim Cooke
    Marshal
    Posts: 3652
    184
    Clojure IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The article show how to write this in a functional style, even using a number of different languages. What part did you not understand?
     
    Deepak Lal
    Ranch Hand
    Posts: 561
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    @Tim, My Question is very clear.

    I feel you are finding it difficult to comprehend my question.

    Please read my question again.


     
    Tim Cooke
    Marshal
    Posts: 3652
    184
    Clojure IntelliJ IDE Java
    • Likes 4
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You say you don't understand the article, but don't say what exactly it is about the article that you don't understand.

    You ask for someone to provide you with the equivalent program written in a functional style, yet the article you link to does just that.

    So no, your question is not very clear. It's always best to ask a specific question about something you don't understand, as you tend to get more helpful responses that way.
     
    Deepak Lal
    Ranch Hand
    Posts: 561
    Eclipse IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Tim Cooke wrote:You say you don't understand the article, but don't say what exactly it is about the article that you don't understand..


    My Comments: I am in a process of learning Functional Programming,

    Tim Cooke wrote:You ask for someone to provide you with the equivalent program written in a functional style, yet the article you link to does just that.


    My Comments:The Article gives the Functional programming style for Solution 4: A solution using reduce which i am unable to follow so i had asked for the working example using Reduce in Java SE 8

    How do i convert the below code to be workable in Java SE 8 ??

    (defn change [amount]
    (reduce (fn [accumulation value]
    (let [left (peek accumulation)]
    (conj (pop accumulation) (quot left value) (rem left value))))
    [amount]
    [25 10 5]))


    Tim Cooke wrote:So no, your question is not very clear. It's always best to ask a specific question about something you don't understand, as you tend to get more helpful responses that way.


    My Comments:Hope i am asking specific questions this time


    Regards,
    Deepak Lal
     
    Tim Cooke
    Marshal
    Posts: 3652
    184
    Clojure IntelliJ IDE Java
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I don't have a working example of that program written in Java 8, so can't provide it.

    Why don't you take it on yourself as a challenge that you can work towards while learning functional programming with Java 8. You will have learned a whole lot on that journey and the feeling of satisfaction will be far greater than if someone just gave you the solution today.
     
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!