Programming Concepts: Programming paradigms

From Wikibooks, open books for an open world
Jump to navigation Jump to search

PAPER 1 - ⇑ Fundamentals of programming ⇑

Programming paradigms Procedure-oriented programming →


Programming paradigms[edit | edit source]

Paradigm - a typical example or pattern of something; a pattern or model

You should have experience of programming solutions to problems. This experience is probably in a language such as: VB.NET, PHP, Python, Java or Pascal and you should be able to quickly understand what code like this would do:

numBananas = 2
numApples = 5
numFruit = numBananas + numApples

So far you have been using Structured programming techniques. Taking a look at the example above, structured languages move from the program line by line, starting at 1, then 2, then 3.

There are many other paradigms in programming, but in this course you will be learning the following types:

  • Structured programming techniques
  • Functional programming
  • Logic Programming
  • Event-driven programming
  • Procedure-orientated programming
  • Object-orientated programming

Structured programming techniques[edit | edit source]

Structured programming techniques involve giving the code you write structures, these often involve writing code in blocks such as:

  • Sequence - code executed line by line
  • Selection - branching statements such as if..then..else, or case.
  • Repetition - iterative statements such as for, while, repeat, loop, do, until.

It also involves breaking down problems into smaller problems until routines can be written that execute single tasks. The routines operate on data passed to them through parameters and these routines can be re-used and are often packaged into libraries to save time in development.

dim x as integer
x = 7
If x = 2 then
  x = x + 4
else
  x = x - 2
end if

Procedure-oriented programming[edit | edit source]

Sharing the same features as structured programming techniques, Procedure-oriented programming implement procedures/subroutines to execute common functionality

dim x as integer
x = 7
x = doubleIt(x)
console.writeline(x)

Object-oriented programming[edit | edit source]

Object-oriented programming takes the techniques used in structured programming further and combines routines and the data they use into Objects. The data items stored for an Object are called Attributes and the routines that operate on these fields are called Methods.

class Building
{
	private int height = 50;

	public int getHeight()
	{
		return height;
	}

	public void demolish()
	{
		System.out.println( "3 .. 2 .. 1 .. BOOM!" );
		height = 0;
	}
}

class Program
{
	public static void main( String[] args )
	{
		Building house1 = new Building();
		Building house2 = new Building();

		house2.demolish();
		System.out.println( "House 1 height = "+house1.getHeight() );
		System.out.println( "House 2 height = "+house2.getHeight() );
	}
}

Functional programming[edit | edit source]

In functional programming programs define mathematical functions. A solution to a problem consists of a series of function calls. There are no variables or assignment statements, but instead there are lists and functions that manipulate these lists. An example of a functional programming language is Haskell.

Logic programming[edit | edit source]

A logic program consists of a set of facts and rules. A knowledge base is built up about a specific subject and an inference engine uses the knowledge base to answer queries which are presented in the form of a goal. Logic programming is often used for artificial intelligence systems.

Event-driven programming[edit | edit source]

Event Driven programming refers to your standard Windows Form idea, the program waits in a loop until an event(e.g. the click of a button, or a keystroke). The program then runs the code associated with this event and then returns to its loop, providing that the code did not instruct it to close.

If more than one event occurs, code is queued by the program and run in the order the events were triggered.

Exercises[edit | edit source]

Exercise: Programming paradigms

List 4 different programming paradigms

Answer:


  • Procedure-orientated programming
  • Structured programming techniques
  • Event-driven programming
  • Object-orientated programming

What is meant by a programming paradigm

Answer:


a model or pattern; a template, defining how a programming language functions

Describe how Event-driven languages work

Answer:


the program waits in a loop until an event(e.g. the click of a button, or a keystroke). The program then runs the code associated with this event and then returns to its loop, providing that the code did not instruct it to close.

Describe 2 events that you might expect a computer game to handle

Answer:


  • button presses
  • network communication, e.g. someone sending you a message

Describe 2 events that you might expect a word processor to handle

Answer:


  • keyboard input
  • mouse buttons