gudii9
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.
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.
ASKER
any simple examples on these three approaches with sample code or links?
please advise
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:
Functional:
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 ;
}
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 ;
}
}
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 ;
}
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 classplease advise
public class Factorial {
private int total = 1 ;
public int factorial(int n) {
for (int i = 1 ; i <= n ; i++) {
total = total * i ;
}
return total ;
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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