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.
LVL 7
gudii9Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

dpearsonCommented:
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
0
gudii9Author Commented:
any simple examples on these three approaches with sample code or links?

please advise
0
dpearsonCommented:
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

0
gudii9Author Commented:
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
0
dpearsonCommented:
The object oriented code is changing the state of the object.  It's a bit contrived, but 'total' is modified in the call.

So you could have:

public class Factorial {
   private int total = 1 ;

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

    // This method relies on the state of the object
    public int getLastFactorialResult() {
        return total ;
    }
}

Open in new window


which neither the procedural nor the functional supports (at least not directly since they don't generally have objects).

Doug
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.