Monday, 4 March 2013

3.2.2 Recursion

Recursion, in programming, is the ability for a function to call its self. Practically this means that a recursive function mentions its self within its own function. For example say we have a function called "Factorial" it finds the factorial of a number and we put in 3. Firstly we check that is has a base case that will stop it forming an endless loop. In this case it's that "if n = 1 then Result = 1" this means that once it hits one it stops opposed to continuing for ever and causing a stack overflow error. Next we input 3 and this happens "
"Factorial(3)
Factorial = 3 * Factorial(2)"

As you can see it references its self so next we look at Factorial(2)

"Factorial(2)
Factorial = 2 * Factorial(1)"

so now we're onto Factorial(1) Thankfully due to the clause about n = 1 we don't go any further and it outputs 1, so then it goes up to the next level and times 2 by 1, this results 2 which it then raises again to times 3 by 2 outputting 6 and giving us our answer. This is the principal of recursion.

3.2.1 Programming Paradigms

A programming paradigm is the methodology of a programming language. There are 5 main programming paradigms:
Imperative programming manipulates variables and data structures in the program to do what the programmer wants in the series of steps he has stated in the program with no variation. Imperative is most known for Machine code which is the basic code of all computers and what hardware is programmed by.

Functional programming treats programming as a form of mathematics and pushes an emphasis on functions opposed to variable and data manipulation like imperative. Functional uses a lot of recursion in it's programming to make iteration possible. It is most well known for the language Lisp, however is mostly used academically opposed to Imperative which sees more commercial use.

Logic programming is done by declaring a set of facts and rules about the program and using them as a knowledge base to answer questions asked of it. This is done but an inference engine which backtracks through the information to gather the relevant data to the question. Prolog is the best known logic based programming language and it's heavily used in attempts to create AIs and expert systems.

Event-driven programming is a language that instead of the programmer telling the program what to to it tells it what to do when 'x' happens, x can be in response to something the user does or when a certain time has passed. Visual Basic is well known for event-driven programming.

And finally we have object-orientated programming is a language that breaks problems down to a single operations to be solved then labels them as a class. Classes can be based on other classes allowing code to be re-used easily and effectively, this is called a sub-class, it has all the features of the main class and then sub-class specific instructions. Python is a well known object-orientated programming language.