Monday, February 29, 2016

Programming Paradigms(Approaches)

Dear Folks,
Let's discuss different programming paradigms used in software development and list programming languages falling under those paradigms.

The literal meaning of paradigm is 
paradigm
ˈparədʌɪm/
noun
  1. 1.
    a typical example or pattern of something; a pattern or model.
    "society's paradigm of the ‘ideal woman’"
  2. 2.
    LINGUISTICS
    a set of linguistic items that form mutually exclusive choices in particular syntactic roles.
    "English determiners form a paradigm: we can say ‘a book’ or ‘his book’ but not ‘a his book’"

It should pronounced keeping 'g' silent like paradime('para' -- as in parameter and 'dime' as in dimension).

Here in our programming context, it's meaning is programming model, approach, or methodology. Before we explore more about different programming paradigms, let's prepare a yummy pizza. Our first task is to find which type of pizza, then, depending on the type, we choose the method/approach/model of preparation .

 There are different methods or approaches available for preparing a yummy pizza. Look at the collage for mouth-watering pizzas,all have been prepared using different methods,but finally it's a Pizza...

Similarly, a program(software) can be developed using different approaches, each approach having it's own set of unique characteristics. Here it is called Programming Paradigm..
Currently the IT industry is using following four common programming paradigms... 

1. Imperative Paradigm
2. Functional Paradigm
3. Logic Paradigm
4. Object Oriented Paradigm

Let's discuss these in brief, and will have elaborate discussion on object oriented programming paradigm in our next post.

1. Imperative Paradigm: 

"First do this and next do that" is the slogan of this model.
We know imperative means,something very important, crucial or urgent, So here focus will be on order and discipline. It implements the traditional Von Neumann Concept.Control flow in imperative programming is explicit, and commands show how  the computation takes place, step by step. 
Fortran, Baisc, Pascal and C are the programming languages of this paradigm.

2. Functional Paradigm:

" Evaluate an Expression and use the result " is the slogan of this paradigm.
Functional programming languages define programs and subroutines as mathematical functions. Many so-called functional languages are "impure", containing imperative features. 
The salient features of this paradigm are...
  • There are no commands, only side-effect free expressions
  • Code is much shorter, less error-prone, and much easier to prove correct
  • There is more inherent parallelism, so good compilers can produce faster code
Note:In computer science, a function or expression is said to have a side effect if it modifies some state or has an observable interaction with calling functions or the outside world. For example, a particular function might modify a global variable or static variable, modify one of its arguments, raise an exception, write/read data to a file etc.
The examples of programming language implementing this model are, Java(Java version 8 onward),Python,Scala, R, Ruby and Haskel being the purest.

3. Logic Paradigm: 

Logic Paradigm is one in which programs are built by setting up relations that specify facts and inference rules, and asking whether or not something is true (i.e. specifying a goal.) 
Languages that emphasize this paradigm are Prolog, GHC, Parlog, Vulcan, Polka, Mercury, Fnil.


4. Object Oriented Paradigm: 

This the most widely and commonly used paradigm, that focuses on data(objects) rather than procedure or logic. Through this paradigm one can encapsulate data.
OOP is based on sending of messages to objects. Objects respond to messages by performing operations. Messages can have arguments, so "sending messages" is nothing but calling a function.
This approach makes the software development process simple, there by allowing easy development and up gradation of software. Data protection, Reuasbility of code and dynamism are the key advantages of this paradigm.
It is identified with following key features
i.   Data Abstraction
ii.  Data Encapsulation
iii. Classes and Objects
iv. Inheritance
v.  Polymorphism
A detailed discussion on this paradigm will be made in the next post.
Smalltalk,Ada, Eiffel, Objective C, C++ ,C# and Java are the examples of this paradigm.
Let me summarize these four paradigms with help this image


Please, Comment and give your feed back about this post. If you have any suggestions , we can incorporate in this post.
References:

1.http://cs.lmu.edu/~ray/notes/paradigms/
2.http://people.cs.aau.dk/~normark/prog3-03/html/notes/paradigms_themes-paradigm-overview-section.html

Wednesday, February 24, 2016

Use of keyword static in C,C++ and Java

Dear Folks, 
If any one knows C,C++ and Java then a clear understanding of keyword static is must for its proper use. 
Let's first see it's use in C.
In C language, this keyword is used to create variables for static storage class. In C, there are four basic storage classes, Namely
 1.Automatic 2.Register 3. Static and 4. External


The behavior of static storage class is that, it's default initial value is zero, scope is local to the block in which it is declared. The interesting feature of this type of variable is that, the life of this variable persists between function calls. It means, when the control exits the function in which it is declared, the variable will not be destroyed from memory, it exists and when the function is visited  next time, this variable will not be recreated, instead the old one, which has been created in the first call is used with old value.


  Following C code snippet will make you understand this concept.
void display()
{
 static int k;
 printf("%d\n", ++k);
}

main()
{
 display();
 display();
 display();
}
The output here is
1
2
3
As, k is a static variable , it's default initial value is zero, in the first call it gets incremented to 1, and  then for second call, k is not recreated, instead existing k is used with old value. So the increment operator  when applied on k, it becomes 2, and so also for third call.

Let's understand, its use in C++ and Java ...
Normally, in C++ and Java keyword static is used to create class variables and 
class methods.


A static variable or class variable is one, which has a common state(value) for a particular attribute, for all objects of a class.Similarly a static method(or member function) is  one, which is used to describe common behavior of all objects.



// This Java code snippet make you understand how static keyword is used... 
class Account
{
  private static double minbal;

  static void setMinBal( double x)
  {
      minbal=x;
  }
 static void dispalyMinBal()
  {
     Systm.out.println(minbal);
  }
}

class DemoStatic
{
  public static void main(String arg[])
  {
     Account.setMinBal(10000);
     Account.displayMinBal(10000);
  }
}


In this example, minbal is static variable, which represents a common value for all accounts(objects), and setMinBal() and displayMinBal() are static methods which are common for all accounts. So they have been invoked with the help class name, instead of object.


Do you find this post useful, Help me in improving this, by sending comments and feedback.

  



What is static and non-static concept in Java?

Before you read this post, read my post " meaning of static and dynamic in the context of programming".

In Java, we deal with objects, the concept of static and nonstatic is related to objects itself.

The other term used in Java for static is class variable, for nonstatic it is instance variable.

In a nutshell, nonstatic property describes instance property, i.e. object property,and it is accessed with object, where as static property describes class property(i.e common for all objects) and it is accessed with class name in which it is declared.

// Example
class Account 
{

 double balance; // instance property
 static double min_balance; // class property
 ----
 ----
}
In the above example, for every account the state(value) of balance changes, hence it is nonstatic(i.e changing) where as for every account the state of min_balance is same(not changing), i.e common for all object. So it is static.

// Accessing static and nonstatic data/method

class Bank
{
 public static void main(String arg[])
 {
  Account a1=new Account();
  a1.balance=5000; // Accessing nonstatic data
  System.out.println("Balance is"+a1.balance);
  System.out.println("Minimum Balance is"+Account.min_balance);
}

Can you tell me what's the point in doing so? please reply...


What is the meaning of dynamic and static in the context of programming?

Dear Folks !


The literal meaning of dynamic is something in motion.That's why anything we do at run-time of the program, we call it as dynamic. For instance dynamic initialization, it is the process if assigning a initial value to a variable at run-time.
Dynamic:                                
          

Where as static is, any thing that is stationary(not in motion or at rest),  Here in programming, anything done before run-time is called static. That is normally compile-time of the program.
Static:


In C we use :
 scanf(): to read values for variables at run-time of the program
 gets(): to read strings at run-time and getch() to read characters at run-time.

In C++ we use:
 cin object to read at run-time(console in that is reading from keyboard)

Similarly, dynamic allocation of memory, is allocating memory to variable at run time of the program. This has the advantage of using exact required amount of memory(No wastage, No shortage of memory).

In C we use malloc() function to allocate memory at run-time of the program.For instance if we want to allocate memory for N integers at runtime, then we use following code snippet in C language.

int *p;
int n;
printf("For how many integers do you want to allocate memory\n");
scanf("%d",&n);
p=(int*)malloc(n*sizeof(int));

In C++, same job is done, using new operator, as shown below.

int *p;
int n;
cout<<"For how many integers do you want to allocate memory\n";
cin>>n;
p=new int[n];

This allocates memory at run-time, so it is dynamic initialization.




The examples for compile time activities are,static initialization of variables, which is the compile-time initializaton  of variables. The other examples could be, static allocation, static binding/linking etc.

Static allocation, is the scheme of memory allocation, in which the amount of memory to be allocated must be specified before run-time, i.e it should be known in advance, before run-time of the program. 

In C/C++, arrays use static allocation of memory. The major disadvntage of static allocation is that, we may fall short of memory , or else we may end with waste of memory. 
If you have any queries, you are most welcome... Please give your feedback about this post and reply through the blog itself.