Google

Thursday, March 20, 2008

Programming paradigms


Programming Paradigms
Programming paradigms is a new way of thinking about the programming languages.
we attempt to trace the basic discipline and the idea behind each of the main 4 programming paradigms.
  • Imperative paradigm
  • logic paradigm
  • functional paradigm
  • object-oriented paradigm

1. Overview of imperative programming
First do this and next do that
The 'first do this, next do that' is a short phrase which really describes the spirit of the imperative paradigm. The basic idea is the command, which has a measurable effect on the program state. The phrase also reflects that the order to the commands is important. 'First do that, then do this' would be different from 'first do this, then do that'.
In the itemized list below we describe the main properties of the imperative paradigm.

Characteristics:
· Discipline and idea : Digital hardware technology and the ideas of Von Neumann
· Incremental change of the program state as a function of time.
· Execution of computational steps in an order governed by control structures
· We call the steps for commands
· Typical commands offered by imperative languages :Assignment, IO, procedure calls
· Language representatives : Fortran, Algol, Pascal, Basic, C
· The natural abstraction is the procedure
· Abstracts one or more actions to a procedure, which can be called as a single command.
· "Procedural programming"
We use several names for the computational steps in an imperative language. The word statement is often used with the special computer science meaning 'a elementary instruction in a source language'. The word instruction is another possibility; We prefer to devote this word the computational steps performed at the machine level. We will use the word 'command' for the imperatives in a high level imperative programming language.
A procedure abstracts one or more actions to a procedure, which can be activated as a single action.

2. Overview of the functional paradigm
Functional programming is in many respects a simpler and more clean programming paradigm than the imperative one. The reason is that the paradigm originates from a purely mathematical discipline: the theory of functions. The imperative paradigm is rooted in the key technological ideas of the digital computer, which are more complicated, and less 'clean' than mathematical function theory.
Below we characterize the most important, overall properties of the functional programming paradigm.

Evaluate an expression and use the resulting value for something

Characteristics:
· Discipline and idea : Mathematics and the theory of functions
· The values produced are non-mutable
· Impossible to change any constituent of a composite value
· As a remedy, it is possible to make a revised copy of composite value
· Atemporal : Time only plays a minor role compared to the imperative paradigm
· Applicative : All computations are done by applying (calling) functions
· The natural abstraction is the function
· Abstracts a single expression to a function which can be evaluated as an expression.
· Functions are first class values
· Functions are full-fledged data just like numbers, lists, ...
· Fits well with computations driven by needs
· Opens a new world of possibilities

3. Overview of the logic paradigm
The logic paradigm is dramatically different from the other three main programming paradigms. The logic paradigm fits extremely well when applied in problem domains that deal with the extraction of knowledge from basic facts and relations. The logical paradigm seems less natural in the more general areas of computation.

Answer a question via search for a solution
Below we briefly characterize the main properties of the logic programming paradigm.

Characteristics:
· Discipline and idea : Automatic proofs within artificial intelligence
· Based on axioms, inference rules, and queries.
· Program execution becomes a systematic search in a set of facts, making use of a set of inference rules

4. Overview of the object-oriented paradigm
The object-oriented paradigm has gained great popularity in the recent decade. The primary and most direct reason is undoubtedly the strong support of encapsulation and the logical grouping of program aspects. These properties are very important when programs become larger and larger.
The underlying, and somewhat deeper reason to the success of the object-oriented paradigm is probably the conceptual anchoring of the paradigm. An object-oriented program is constructed with the outset in concepts, which are important in the problem domain of interest. In that way, all the necessary technicalities of programming come in second row.

Send messages between objects to simulate the temporal evolution of a set of real world phenomena
As for the other main programming paradigms, we will now describe the most important properties of object-oriented programming, seen as a school of thought in the area of computer programming.

Characteristics:
· Discipline and idea : The theory of concepts, and models of human interaction with real world phenomenon
· Data as well as operations are encapsulated in objects
· Information hiding is used to protect internal properties of an object
· Objects interact by means of message passing
· A metaphor for applying an operation on an object
· In most object-oriented languages objects are grouped in classes
· Objects in classes are similar enough to allow programming of the classes, as opposed to programming of the individual objects
· Classes represent concepts whereas objects represent phenomena
· Classes are organized in inheritance hierarchies
· Provides for class extension or specialization
This ends the overview of the four main programming paradigms.



























Wednesday, March 19, 2008

Dont use the function gets( )

Never, never, NEVER use the function gets(). It is the most dangerous function in the entire C standard library because there is there is no way to use it safely!
Consider this example:

int main(void)
{
char name [25];
printf("Enter your name: ");
fflush(stdout);
if (gets(name) != NULL)
printf("Hello %s\n", name);
return 0;
}

What will happen if the user types fifty characters into twenty-five character array?
The answer is that gets() will fill up array and then keep on going, trying to write to memory past the end of the array which the program does not have the right to access. A program crash is likely.


You might also have heard that you should use the fgets() function, with stdin as the FILE * parameter, instead of gets(). Most people stop after saying that, but that doesn't actually give you the same result. gets() removes the '\n' character from the input but fgets() does not. That means you must manually remove the '\n' before passing the string to fopen(), or for many other uses.


Here is getsafe() function. Like gets() and fgets() both, it returns a pointer to char. This is either the pointer which was passed to it, or NULL if end of file or an error occurred. Like gets(), it removes the '\n' at the end of the string, if there is one. The prototype is:
char *getsafe(char *buffer, int count);
Here is the function:
char *getsafe(char *buffer, int count)
{
char *result = buffer, *np;
if ((buffer == NULL or count <>result = NULL;

else if (count == 1)

*result = '\0';

else if ((result = fgets(buffer, count, stdin)) != NULL)

if (np = strchr(buffer, '\n'))

*np = '\0';

return result;

}


That's all it takes to safely get input strings from the standard input with the '\n' removed.

Why int main( ) ? but not void main( ) ?

The ANSI/ISO standards for C and C++ define the languages.
ANSI's International Standard For C
The function called at program startup is named main.
The implementation declares no prototype for this function. It can be defined with no parameters:
int main(void) { /* ... */ }
or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared):
int main(int argc, char *argv[ ]) { /* ... */ }

This function shall not be overloaded.
It should have a return type of type int.

Practical Reasons To Return An int from main()
On many operating systems, the value returned by main() is used to return an exit status to the environment. On Unix, MS-DOS, and Windows systems, the low eight bits of the value returned by main() is passed to the command shell or calling program. This is often used to change the course of a program, batch file, or shell script.
Many compilers will refuse to compile a source code file containing a definition of main() which does not return an int.
On some platforms a program starting with void main() may crash on startup, or when it ends.
A program which contains a main() function that is not defined to return an int is just plain not real C or C++..

What should my "int main()" return?
As pointed out in the section above, it is extremely common for a program to return a result indication to the operating system. Some operating systems require a result code. And the return value from main(), or the equivalent value passed in a call to the exit() function, is translated by your compiler into an appropriate code.
There are three and only three completely standard and portable values to return from main() or pass to exit():
The plain old ordinary integer value 0.
The constant EXIT_SUCCESS defined in .
The constant EXIT_FAILURE defined in .

If you use 0 or EXIT_SUCCESS your compiler's run time library is guaranteed to translate this into a result code which your operating system considers as successful.

If you use EXIT_FAILURE your compiler's run time library is guaranteed to translate this into a result code which your operating system considers as unsuccessful.

C++ Note
In a C++ program you do not have to return anything from int main()! The language standard guarantees that if your int main() function "falls off the end" by reaching the closing brace, the compiler will automatically return 0 for you indicating success.
Warnings
It is not good programming practice to take advantage of this C++ feature. Programs should always specifically indicate a return status.
C++ does not provide this automatic return for any function except int main().
C does not provide an automatic return value for main() or any other function. It is up to the program to specify a return value or the status returned to the operating system is undefined.