CSC/ECE 517 Fall 2010/ch1 1e bb: Difference between revisions
Line 56: | Line 56: | ||
Thus an object oriented approach can be used to maintain state information of a variable, which is completely opposite to the functional approach. | Thus an object oriented approach can be used to maintain state information of a variable, which is completely opposite to the functional approach. | ||
==Mixing functional and object oriented code== |
Revision as of 18:33, 8 September 2010
Functional Programming
Functional programming treats computation as the evaluation of mathematical functions and avoids state and mutable data. It emphasizes on the application of functions, also called applicative programming. Functional programming decomposes a problem into a set of functions. Ideally, functions only take inputs and produce outputs, and don't have any internal state that affects the output produced for a given input. Well-known functional languages include the ML family (Standard ML, OCaml, and other variants) and Haskell. In a functional program, input flows through a set of functions. Each function operates on its input and produces some output A more practical benefit of functional programming is that it forces you to break apart your problem into small pieces. Small functions are also easier to read and to check for errors, thus debugging is easier. Functional Programming discourages the use of variables.
fun factorial (0 : int) : int = 1 | factorial (n : int) : int = n * factorial (n-1)
The above code snippet is a functional code written in ML to compute factorial of an integer recursively. In the first line, the fun factorial keyword defines a function. The notation (0: int) can be read as ‘0 is of type int’ and : int =1 means that the default return value is 1 when 0 is passed as an argument.
In the second line, (n: int) sets the argument n to integer. int = n * factorial (n-1) means the return value of recursive computation is an integer.
We can observe here that no additional variables are used here, no state information is maintained and there are no side-effects to any variables.
Object Oriented Programming
Object-oriented programming (OOP) is a programming language model that uses objects which are discrete bundles of functions and procedures, often relating to a particular real-world concept such as a bank account holder or hockey player, together with they relate to each other.
Java, Python, C++, Visual Basic .NET and Ruby are the most popular OOP languages today.
public class Factorial { public int fact; public int factorial(int n) { if (n <= 1) { return 1; } else { return n * factorial(n-1); } } }
The above code is written in JAVA to compute factorial of an integer. The method public int factorial (int n) computes the factorial of variable n. If the value of argument n<=1 then 1 is returned. Otherwise, the statement return n* factorial(n-1) calls the function recursively.
A point to note here is that we can maintain various states of the variable private int fact in the above code. If we create 3 instances of the above class (f1, f2 and f3), then the below code in main can maintain 3 states of the variable private int fact.
public static void main(String argv[]) { Factorial f1 = new Factorial(); //instance 1 Factorial f2 = new Factorial(); //instance 2 Factorial f3 = new Factorial(); //instance 3 f1.fact = f1.factorial(5); //state 1 of int fact f2.fact = f2.factorial(9); //state 2 of int fact f3.fact = f3.factorial(12); //state 3 of int fact }
Thus an object oriented approach can be used to maintain state information of a variable, which is completely opposite to the functional approach.