There are various forms of programming, and programming languages. The three most common you will find are listed below, procedural, then functional, and finally object oriented. Each of these have their own benefits and drawbacks. We’re going to look at a high level at these, and see how it will help us move forward in our programming experiences. The good news is that Python supports all three of these methodologies, and we’ve already seen one, and are about to dive into the second. We will not be looking at object oriented code this term, but know that if you continue with programming in either a CS major or minor, you will see this again.
Procedural Programming
Procedural programming is a method of which you write code based upon the action which is to be performed. This could be based off of a few lines of code, or thousands of lines.The basic idea is you will start at the top and complete each line of code, one line at a time, until you reach the end.
During the course of the code execution, you might come across a conditional statement which allows you to skip some lines of code, or a loop which requires you to repeat some code, but the basic tenant is you start at the top of the script, and work your way down the code.
Everything we’ve written so far is procedural. Almost all of the original programs could be thought of as procedural. Today, it is not used as much, except for smaller simpler programs, like those which automate systems and batch jobs, or perform simple tasks.
Functional Programming
Functional programming takes it’s design from mathematical functions. These functions allow you you make repeated calls and get consistent results.
You can think of a function as a mini-program. Your code will break from the normal top to bottom reading of the code, to go off to a function, and then return from where it left. You can write your code to go out to the same function multiple times, or just a single time.
Functions have lots of flexibility. For example you can pass values to a function, which allows the function to behave differently, or you can choose to not pass values to a function. You can write, and use functions, which will return a value to the calling code, or not.
You can even write your code so a function can call another function, making your application jump all around your source code, and even into other files, instead of following a consistent flow.
So why would you use a function?
Functions allow you to break up your code into logical chunks.
Functions allow you to call sections of code repeatedly, without it being in a loop.
Functions allow multiple people to work on different sections of code depending upon their skills/ability and the time they have available.
Object oriented programming
Object oriented programming (OOP) focuses on the data that is being stored, and what needs to happen to that data. For simple projects, you might find that you have to write more code for an object oriented program. However, with larger projects, you’ll find that the code is simpler, and easier to maintain.
Most applications now a days are written with OOP, as almost all modern languages require it, such as Java, or support it such as C++, Python, and more.
With OOP you define a data structure which is made up of smaller data structures, and then you define functions – called methods in OOP, which allow you to work with that data.
This might seem complicated, but think about what types of data we’ve looked at so far. Simple things like numbers, strings, and getting into more complex things like lists, sets, and dictionaries. However, those were just collections of simple data types in many ways – like a list of numbers.
Now think of a real world object – like yourself. You aren’t just a number, or string. You have lots of attributes that help describe and define you. These attributes might be simple data types like age (number), name (string), student number (string), and college major (string). Or that could be made up of other complex data types like your address, parents, etc.
In fact, if you think of most things in this world (nouns) they are made up of multiple attributes, and this is where we get OOP. We can’t describe a house, car, person, school, etc with a single data type, so we build a class which contains all of those attributes, which we store as class variables.
Procedural vs Object Oriented Programming was originally found on Access 2 Learn