Link to home
Start Free TrialLog in
Avatar of gudii9
gudii9Flag for United States of America

asked on

Procedural Programming vs Object Oriented Programming vs Functional Programming (Java 8)

Hi,

What are differences between Procedural Programming vs Object Oriented Programming vs Functional Programming (Java 8).

When to use which one. Advantaged and disaadvantages of each. Please provide any sample examples on each approach.
Avatar of dpearson
dpearson

That's a huge question as each group covers a family of programming languages - so it's pretty massive in scope.

The short answer is:
 - Procedural programming is where you write a collection of methods that operate on data.
 - Object programming is where you group those methods into objects with private data
 - Functional programming is where you work with functions instead of methods - the difference being a function takes an input and returns an output, it doesn't change the state of an object.

Functional is the most different of the three.  In a pure functional programming language you cannot change the value of a variable - once it is assigned a value, it keeps it forever.  At first glance you'd think that makes it impossible to write programs like that, but it can be done.

Doug
Avatar of gudii9

ASKER

any simple examples on these three approaches with sample code or links?

please advise
Pretty hard to find simple examples to cover entire programming paradigms.

To restate what I posted with a little bit of pseudo-code (please don't spend time trying to get each to run and asking for help if they don't - the idea is the concepts):

Procedural:

// Just a method
public static int factorial(int n) {
    int result = 1 ;
    for (int i = 1 ; i <= n ; i++) {
       result = result * i ;
    }
    return result ;
}

Open in new window

Object oriented:

// A method as part of a class
public class Factorial {
   private int total = 1 ;

   public int factorial(int n) {
    for (int i = 1 ; i <= n ; i++) {
       total = total * i ;
        }
    return total ;
    }
}

Open in new window


Functional:

// A function that avoids assignment statements (changing the value of an existing variable)
public static int factorial(int n) {
    if (n == 1)
      return 1 ;
    return factorial(n-1) * n ;
}

Open in new window

Avatar of gudii9

ASKER

takes an input and returns an output, it doesn't change the state of an object.

Even object oriented as below does same thing as above right?

// A method as part of a class
public class Factorial {
   private int total = 1 ;

   public int factorial(int n) {
    for (int i = 1 ; i <= n ; i++) {
       total = total * i ;
        }
    return total ;
    }
}
please advise
ASKER CERTIFIED SOLUTION
Avatar of dpearson
dpearson

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial