Programming paradigm facts for kids
Programming paradigms are like different styles or ways of thinking about how to build computer programs. Imagine you're building with Legos; you could follow a step-by-step instruction manual, or you could just decide what you want the final model to look like and let someone else figure out the steps. Programming paradigms are similar ideas for writing code.
A programming language can fit into more than one paradigm. Some paradigms focus on how the code runs, like if it needs to do things in a specific order. Other paradigms look at how code is organized, such as putting everything into one big piece or breaking it into many smaller parts.
There are two main groups of paradigms: imperative and declarative. A language can even be both at the same time!
Contents
Imperative Programming
In imperative programming, programmers give the computer a list of exact steps to follow. These steps must be done in a certain order to achieve a goal.
For example, if you wanted the computer to draw a cat face, you might tell it: "Draw a big circle here. Then, draw two smaller circles for eyes. Next, add two triangles on top for ears." Imperative programs sometimes involve "side effects," which means they can change the computer's state or information as they run.
There are two big imperative paradigms, and many languages use both:
- Structured Programming
* The computer follows directions in a clear, organized order. It doesn't allow "goto statements," which are commands that tell the program to jump back to an earlier step. This makes the code easier to understand.
- Procedural Programming
* This lets programmers give a name to a list of orders. This named list becomes a "procedure" or "function" that can be used again later. Most structured languages are also procedural.
* In this style, ideas are explained using "objects." An object is like a mini-program that has its own information (called "properties") and actions it can perform (called "methods").
Declarative Programming
In declarative programming, the programmer tells the computer what to do, instead of how to do it.
If you wanted the computer to draw a cat face using a declarative style, you might simply say: "Draw a cat face, including two eyes, two ears, and a mouth." The computer then figures out the exact steps needed to draw it.
The most common declarative paradigms are:
* Most of the work is done by functions that don't have "side effects." This means they don't change anything outside of themselves, making them very predictable.
* You state a set of facts and rules. Then, you ask the computer questions based on those facts, and it tries to find the answers.
* Pieces of code are set up to run only when specific things happen. For example, a program might run a certain code when you click a button or when the computer starts up.
Other Programming Styles
Some programming styles can be found in both imperative and declarative languages. These are usually used alongside one of the main paradigms.
- Parallel Programming: This is when more than one part of the code runs at the same time. This can make programs much faster.
- Meta-programming: This is a special feature in a language that lets the programmer change how the language itself works. It's like writing code that writes or changes other code.
Overview of Programming Styles
Programming languages are grouped by their styles, much like machines might be grouped by what they are used for.
Some languages fit mostly into just one main style, such as:
- Smalltalk (object-oriented)
- Assembly (imperative, but not structured or object-oriented)
- Haskell (functional)
- Prolog (logic)
However, most languages fit into more than one style. Some examples of languages that use many styles are:
- Scala (object-oriented, functional, parallel)
- Visual Basic (event-driven, object-oriented)
- Common Lisp (procedural, functional, object-oriented, meta)
- Scheme (functional, procedural, meta)
- Perl (functional, procedural, meta, object-oriented, event-driven)
- Python (functional, object-oriented, procedural)
- Ruby (functional, object-oriented, procedural)
- Wolfram Language (functional, procedural, generally declarative)
- Oz (logic, functional, imperative, object-oriented)
- F# (functional, imperative, object-oriented, meta)
Having many styles in one language isn't always better. Sometimes, a language with fewer styles can be very good. For example, a language that is only functional might do less work than a language that is also procedural. This is because functional languages can be very efficient.
Many programming styles are known for what they don't let people do, as much as for what they do allow. For instance, functional languages often don't allow "side effects." Another example is structured programming, which doesn't let programmers use "goto statements." These limits can actually help programmers avoid mistakes. They can also allow the computer to run code faster or even check for problems before the code runs!
Why Some People Dislike Paradigms
Some experts who study programming languages, like Harper and Krishnamurthi, don't like using paradigms to group languages. They argue that many programming languages borrow ideas from so many different styles that they can't be neatly put into just one or two categories.
History of Programming Styles
New programming styles have been created over time. Sometimes, people noticed them right away, and other times, they were recognized later when looking back. One of the first styles to be seen as a new way of programming was structured programming in the 1960s.
The idea of a "programming paradigm" itself became known around 1978. Robert W. Floyd used the term when he was teaching. The word "paradigm" in this sense was first used by Thomas Kuhn in his 1962 book, The Structure of Scientific Revolutions.
Early Machine Code
The oldest and most basic programming style is machine code. This is an imperative style. Machine code is made of numbers in a specific order that the computer understands directly.
Assembly language came a bit later and is slightly easier to use than machine code. In assembly language, directions for the computer are given easier-to-remember names (called "mnemonics"). Also, locations in the computer's memory can be given names. These are sometimes called first- and second-generation languages.
In the 1960s, assembly languages got better. They added new features like being able to copy code from libraries, use macros (special code that changes into normal code), run procedures (named sets of directions), and use variables (named items to store information). This meant people could reuse code and didn't have to worry as much about problems specific to one type of computer.
Assembly language was, and sometimes still is, used when code needs to be very fast. It's also used a lot in embedded systems (like in smart devices) because it gives programmers exact control over what the machine does.
Procedural Languages Emerge
In the late 1960s, people started inventing procedural languages. These were third-generation languages, which were the first of what we now call high-level languages. They used words related to the problems they were trying to solve. For example:
- COmmon Business Oriented Language (COBOL): Uses words like "file," "move," and "copy."
- FORmula TRANslation (FORTRAN): Uses mathematical words and symbols. It was mostly made for science and engineering.
- ALGOrithmic Language (ALGOL): Made for writing algorithms (sets of steps for the computer). It also uses mathematical words and symbols.
- Programming Language One (PL/I): Was designed to be useful for everyone.
- Beginners All-purpose Symbolic Instruction Code (BASIC): Made to help beginners learn programming.
- C: A very versatile programming language. Dennis Ritchie worked on it from 1969 to 1973 at Bell Labs.
Object-Oriented Programming Develops
After many people started using procedural languages, they invented object-oriented programming languages. In these languages, data (information) and its "methods" (ways to use or change that data) are put together into one "object." Some programmers, like Richard Stallman, don't agree that object-oriented languages are always better than procedural languages for explaining ideas to a computer.
Because object-oriented programming is a style, not a specific language, people have even made object-oriented assembly languages, such as HLA.
Declarative Styles Grow
At the same time, some people were creating declarative programming languages. A well-known declarative language is SQL. SQL is used for adding, removing, and finding information in tables of data.
Related pages
- Mindset
- Type system
- Turing completeness
See also
In Spanish: Paradigma de programación para niños