CSC/ECE 517 Fall 2013/ch1 1w25 avam

From Expertiza_Wiki
Revision as of 16:53, 14 September 2013 by Avrohama (talk | contribs)
Jump to navigation Jump to search

Hi, Coming soon!

<mainpage-leftcolumn-start />

What is functional programming?

Functional programming is a style of programming in which all the computations are modeled as evaluation of mathematical functions avoiding any state or mutation of data. The end result of a program depends on the input given to that function and not on the state of the program.

Examples of functional programming languages:

  • Haskell
  • Scala
  • Erlang

The functions here are different from those that we see in imperative programming languages like C, BASIC, etc. where functions do exist but they are in the form of subroutines and not based as pure mathematical functions.

What is Object Oriented Programming?

Object Oriented Programming is a programming language model which concentrates on real world “objects” and their interactions rather than simple “actions” and on “data” rather than logic. In the OOP paradigm, objects have data fields, which are the attributes that describe the object along with some associated procedures known as functions/methods. The “objects” in OOP are primarily instances of “classes” which are defined by the programmer. These objects interact with one another through methods to eventually implement applications and computer programs. To perform Object oriented programming, one needs an object oriented programming language. Examples of well-known object-oriented programming languages:

  • Objective-C
  • Smalltalk
  • Java
  • C#

Features of Functional Programming

Pure Functions

OOP has pure functions and expressions. A function is said to be purely functional if it has no side effects with regards to memory or I/O. As a result, purely functional functions and expressions can be used for code optimization.

Following are some of the aspects of having purely functional functions:

  • A pure expression can be removed without having any effect on other expressions, if the result of the pure expression is not used.
  • The compiler has the freedom to reorder or combine the evaluations of expressions in a program.
  • When a pure function is called with parameters that cause no side-effects, then on calling the same function with the same parameters, the same result is returned. This enables programmers to utilize caching optimizations, viz. memorization.
  • The order of two pure expressions can be reversed if there is no data dependency between them. These expressions can also be performed in parallel without any interference with each other’s functioning. This property is also referred to as being thread-safe.


Following aspects of block structure are viewed in OOP languages:-

  • Locality:- The major advantage of block structure is locality. This makes it possible to restrict the existence of an object and its description to the environment where it has meaning.
  • Scope Rule :- These are following aspects of scope rules for names declared within an object:
    • They only exist when the object exist. This is a consequence of locality.
    • Access to global names and re-declaration of names.
    • Access to names within a block from “outside” the block may be restricted. Like in Simula language.

Comparison in a Nutshell

Let us compare both the programming paradigms with respect to several disctinction attributes.

Point of Comparison Functional Languages Object-Oriented Languages
  • Primary focus
Functional programming emphasizes on functions that produce results which depend only on their inputs and not on the program state - i.e. pure mathematial functions. Focus on identifying and representing the problem in terms of an 'object' which has its own data, sub-routines and state. Different objects in the problem interact by sending messages to each other and thus result in change in its internal state. The final state and values of the objects refer to the solution. It is data-centric.
  • Problem Solving Approach
Primarily Top-down design Identification and design of necessary objects. Close to being 'better models of the way the world works'.
  • Program Flow
Often sequential with program having single point of entry and exit. Complex program flow. Can sometimes depend on the internal state of the objects.
  • Modularity
Limited modularity. Program is divided into modules or per say procedures independent of each other but are constrained due to uniqueness to that particular problem. Extremely modular due to the presence of objects which contain their own data and sub-routines.
  • Data Protection/Hiding
No concept of data-hiding. Variables local to one method cannot be accessed by other method. But, Global variables can be accessed anywhere within the program. One of the main fundamentals of O-O languages. Access specifiers like 'public', 'private' and 'protected' dictate the rules of data-hiding. Data which is private is confined to one object and cannot be directly changed by any other method except its own. This places the responsibility of managing data with the object itself This is called as ownership. Thus, data can be accessed ( read/write/modified ) only through the object's own interfaces.
  • Ease of Understanding
Smaller programs are easy to understand but as the program increases in size; understanding the code becomes more and more difficult. Easy to understand due to its real world-like design and flow.
  • Reuse of Code
Very Limited or no code re-usability. Highly re-usable code as the code developed can be easily modified or extended to suit a problem's need.
  • Support for declaring new data types/classes
Difficult due to limited in-built functionality. Easily possible due to the concept of classes. Generic classes can be built as per the required specifications.
  • Efficiency
Efficient for solving small problems. Efficient for solving large problems which have a complex structure and require complex data-types, abstraction and data-security.
  • Maintenance
Maintenance is easy for smaller programs but can consume a-lot of effort for larger program size as it requires the programmer to know and understand the dependencies of every module in the program. This makes it difficult to debug and test the program. Extremely simple as O-O languages aim for high modularity. Secondly, programmer is not concerned with the details of how the data is stored and represented. Thirdly, they also tend to keep low coupling which makes it easy to debug and test different modules in the program.
  • Extensibility
Less Extensible as modules developed need to be re-organised and re-structured heavily in order to meet different needs. High extensibility is one of the most important advantages of OOP. Code can be easily modified and 'plugged-in' to a different program. Methods can be exteneded due to many properties such as polymorphism, inheritance and support for multiple inheritance through interfaces.
  • Flexibility
Less flexible. Sometimes, certain problems do not fit into the 'top-down design' approach. High flexibility. The modelling of problems into world-like objects makes it easy to solve any practical problem.
  • Examples
Haskell, Scala, Erlang C++, Java, Ruby, Python.