# Object Oriented Programming vs Structural Programming

#### fog37

Hello,

I am trying to get my head around the important difference between these two different styles (paradigms) of programming. I am familiar with traditional structural programming: data, variables, functions, structures. The functions are blocks of instructions that can process data, etc.
OOP is said more centered around the data than structural programming. I understand there are classes, objects, instances, methods, etc...

While programming in Arduino, I noticed that the use of certain hardware components, like a servo motor, has OOP in the code but I am not sure how it truly works, why it is needed and its advantages...

Thanks!
Fog37

Related Programming and Computer Science News on Phys.org

#### anorlunda

Mentor
Gold Member
That is a rich and rewarding topic, but you'll need much more than a paragraph or two on PF to educate yourself. And while you're at it, you should be studying other paradigms too. Each has something to offer, and the more of them you study, the better you grok the human problem.

https://en.wikipedia.org/wiki/Object-oriented_programming said:

#### FactChecker

Gold Member
2018 Award
As the name implies, OOD programming has "objects" as it's central feature. An object may treat data very differently, but I would say that the object, not the data is central. A good example may be the simulation of a traffic intersection. A traditional approach may be to make an array of vehicles and their properties and follow them through the intersection. Making it more OOD would be to have something create vehicle objects which initialize all their properties when they enter the simulation and control access to their properties through the simulation till they leave the intersection. A car object may know that it has bad brakes and behave as such. The other vehicles are not aware of the brake problem, and can only detect the speed and behavior of that car. Another object may be a motorcycle. It would have different stearing, acceleration, and braking procedures and behave accordingly. A third object might be an 18-wheeler truck. It would have very different procedures, properties, and behavior . These objects all take care of their own behavior and data, only allowing outsiders to know things and interact in restricted ways. This approach can be very helpful in organizing a computer program. (It can also be a great nuisance if it is overdone.)

#### phinds

Gold Member
As FactChecker said, the data is NOT central to OOP, it is one of two equally important parts off OOP, namely the data and the methods, which work hand in hand. As anorlunda said, OOP is a massive subject.

• Encapsulation
• Inheritance
• Polymorphism
Nothing we tell you is going to really mean much until you have at least a basic understanding of those concepts

#### fog37

I have done some reading. It is possible to write a program with many functions using the conventional procedural programming paradigm. Corrections are highly welcomed!

My understanding is that OOP organizes code more efficiently grouping functions and variables that are "similar" (related) into an object.

The functions within an object become called methods and the variables within the object are called properties.

functions= methods
variables =properties
object = example (instance) of a class
class= objects with common variables and properties

The term encapsulation is a fancy way to describe the idea that related variables and the functions that use those variables are grouped together into a unit called object. In regular procedural programming, the variables and the functions using those variables are in the same code but act more as separate entities.

Code written in OOP can have methods, i.e. functions, that do something but have few or even no input parameters when compared to the same functions used in procedural programming. This is because the method in OOP relies on the properties (variables) within the object....
Question: how does a method, which is essentially a function, perform some task without any input parameters? I am used to think that a function must have input(s) and output(s). Is that not always the case? There are surely void function that produce no output. Does that mean that they produce no numerical output? I think they still produce some outputs which get then assigned to a variable...As far as inputs, I have seen functions that have no inputs but use the external variables and their values to perform some calculation, etc..Aren't those external variables to be considered inputs for the function?

An interesting feature of OOP is abstraction: this makes it easier to use an object. It is about by hiding (making abstract) some of its properties and methods within the object itself so the user only interfaces with certain methods and functions and does not need to care about the other variables and methods inside the object .
Abstraction also makes changes to the object easier to implement without affecting the rest of the code outside of the object.

There is then inheritance which means that several objects can refer to a particular different object (which has its own properties and methods) and "inherit", i.e. refer to and use, the properties and methods defined for this particular reference object without having to redefine all those same properties and methods.

Polymorphism: not fully understand this yet.

Let me know if I am on the right track. In reference to my question above, I would like to understand how a function can do something without inputs or outputs...

Thanks!

#### anorlunda

Mentor
Gold Member
Methods are functions. They can get arguments and they can return results.

What is your goal, and how are you studying?

If you want to learn OOP, then I suggest a tutorial that includes coding examples. Based on your questions, it sounds like you are reading definitions without coding examples.

#### phinds

Gold Member
If you want to learn OOP, then I suggest a tutorial that includes coding examples. Based on your questions, it sounds like you are reading definitions without coding examples.

#### Attachments

• 19.4 KB Views: 242

#### FactChecker

Gold Member
2018 Award
Question: how does a method, which is essentially a function, perform some task without any input parameters? I am used to think that a function must have input(s) and output(s). Is that not always the case? There are surely void function that produce no output. Does that mean that they produce no numerical output? I think they still produce some outputs which get then assigned to a variable...As far as inputs, I have seen functions that have no inputs but use the external variables and their values to perform some calculation, etc..Aren't those external variables to be considered inputs for the function?

Let me know if I am on the right track. In reference to my question above, I would like to understand how a function can do something without inputs or outputs...
Yes. You are on the right track. A method can have access to the data of the object. It can use some as inputs and modify some as outputs. It can also perform other tasks, such as initialize a buffer of data, check the status of some hardware, signal other entities that the object exists, etc.

#### QuantumQuest

Gold Member
My understanding is that OOP organizes code more efficiently grouping functions and variables that are "similar" (related) into an object.
More generally object-oriented design (OOD) which provides a more natural and intuitive way to view the design process, is modeling software components just as we describe real-world objects i.e. by their attributes and behaviors. Also, OOD models communication between objects. Objects communicate via messages just as people do for instance.

The term encapsulation is a fancy way to describe the idea that related variables and the functions that use those variables are grouped together into a unit called object. In regular procedural programming, the variables and the functions using those variables are in the same code but act more as separate entities.
It rather refers to the concept of information hiding which is crucial to good software engineering.

When we design an object - and next implement it in an OOP language, we encapsulate attributes and operations (behavior) into objects. The attributes and operations of an object are intimately tied together. Now, objects have the property of information hiding. This means that, although objects may know how to communicate with one another across well-defined interfaces, objects normally are not allowed to know how other objects are implemented as implementation details are hidden within the objects themselves.

Question: how does a method, which is essentially a function, perform some task without any input parameters? I am used to think that a function must have input(s) and output(s). Is that not always the case? There are surely void function that produce no output. Does that mean that they produce no numerical output? I think they still produce some outputs which get then assigned to a variable...As far as inputs, I have seen functions that have no inputs but use the external variables and their values to perform some calculation, etc..Aren't those external variables to be considered inputs for the function?
Methods in an OOP language and functions in a procedural language have essentially the same functionality as both are called by their name and operate on data but in the case of a method, it is associated with an object. Now, a function may be passed data on which it operates (input parameters) and may return data i.e. some return value . When a method is called, it is implicitly passed the object on which was called and it operates on this so we also have the notion of a class here which is basically a blueprint for the aforementioned object.

Now, a void function performs a task, and then control returns back to the caller but, it does not return a value. As for functions having no inputs that use "external" variables as you say, you most likely mean functions operating on global variables which is a fairly dangerous programming practice. Global variables are visible i.e. can be used from any part of a program, so this does not refer exclusively to functions.

An interesting feature of OOP is abstraction: this makes it easier to use an object. It is about by hiding (making abstract) some of its properties and methods within the object itself so the user only interfaces with certain methods and functions and does not need to care about the other variables and methods inside the object .
Abstraction also makes changes to the object easier to implement without affecting the rest of the code outside of the object.
Just to make it a little more clear, in the real world we, humans, think in terms of objects and we possess the marvelous ability of abstraction. For instance, we can view a mountain in a digital image as object rather than as individual dots of color. In the OOP context, objects provide an abstraction which hides the internal implementation details. You are given a "handle" which are the methods that the object exposes to the programmer in order to use it but you don't have to know about these methods' implementation details i.e. how they produce the desired result.

There is then inheritance which means that several objects can refer to a particular different object (which has its own properties and methods) and "inherit", i.e. refer to and use, the properties and methods defined for this particular reference object without having to redefine all those same properties and methods.
Inheritance is a form of software reuse in which classes are created by absorbing an existing class's data (attributes) and methods (behaviors) and embellishing them with new or modified capabilities. I'm referring to classes because these are the blueprints from which the objects are instantiated. The benefits of inheritance from a software engineering perspective are both saving time during program development through software reusability and also reuse of proven high quality software.

#### harborsparrow

Gold Member
My two cents worth. (And, i like the previous comments people have made as well).

When I taught programming, I emphasized that creating an object (in object-oriented languages) allocates memory for the object, and destroying the object returns the memory to the open pool. Scope rules generally apply, so unless the object is "static" (similar to "global" in procedural languages), only code which has the object name (which is really a pointer in disguise) "in scope" can use the object. By "use the object", I mean access/read/write the data stored within the object.

I emphasized the memory usage because, unless you know what is going on in memory (at least in a virtual sense), eventually a person will have problems using ANY programming language, especially for code used on the web.

An object definition is a description of a data structure. If an object "inherits" from another object definition, it is its progenitor's data structure, plus whatever you add to it.

Note that inheritance also is used in C++, which is largely a procedural language, whereas inheritance is an object oriented concept. So C++ is a particularly large, tricky, messy language (though powerful of course), mainly because it is pretty much the only OOPL that allows for multiple inheritance, which can because so twistedly complex that it can become difficult for the programmer to understand. As a result, later OOPL's restricted objects to inheriting from a single other object. And of course, C++ is like C in that it really does not shelter you at all from the responsibility of knowing almost precisely what is happening in the computer's memory.

Object-oriented programming languages, OTOH, try to manage memory "safely" for you, so that you are free to think more conceptually about your programming goals. As long as performance is not a paramount requirement, as a learner, you can sort of kind of ignore memory allocation. For a while. But it is better not to, especially if you have already learned C first. But Java or C# will certainly shield you from a portion of the nitty gritty details going on in executing the program at run-time--up to a certain point.

So if you've been exposed to C or C++, and pointers, then just realize that in Java or C#, an object name is a pointer variable in effect. When you "instantiate" (create) the actual object, the programming environment populates the address of the newly allocated object into that object name (pointer variable). When you destroy an object by setting it to null, its allocated memory should considered reusable by the system. It isn't quite as simple as that, so if performance matters over time, you'll want to learn much more about how object-oriented languages such as Java or C# handle memory allocation and de-allocation internally (i.e., there is an automatic garbage collector, but you NEVER want to actually let it run, if performance matters, because it temporarily takes over the processing from your program while it runs--a situation that is preventable by always carefully destroying your objects after you are done using them (and before you set them to null); this is an advanced topic though).

Last edited:

#### FactChecker

Gold Member
2018 Award
There is one aspect of OOD that is simple and IMHO has great practical advantages. When one includes several libraries, there has always been a problem when multiple libraries might have functions with the same name. The problem is to control which library function is used. OOD allows one to control that. So using external libraries much easier when the programmers have used OOD.

#### Mark44

Mentor
There is one aspect of OOD that is simple and IMHO has great practical advantages. When one includes several libraries, there has always been a problem when multiple libraries might have functions with the same name. The problem is to control which library function is used. OOD allows one to control that. So using external libraries much easier when the programmers have used OOD.
Are you referring to the concept of namespace, where you qualify the use of some identifier by the namespace it belongs to? If so, I'm not sure that is specific to OOD. I could be wrong, though.

#### harborsparrow

Gold Member
Are you referring to the concept of namespace, where you qualify the use of some identifier by the namespace it belongs to? If so, I'm not sure that is specific to OOD. I could be wrong, though.
Maybe namespaces helping distinguish libraries is sort of a side effect of OOP.

Of course, name collisions do still happen in OOP even when a namespace is required. I blogged about an annoying one here: http://harborsparrow.blogspot.com/2012/11/microsoft-update-kills-web-application.html

My website was about algae studies, and algal samples taken from specific "sites". So I had an object (page) named Site that displayed everything known about one sampling site--as I'm sure biologists and geologists world around do. And some idiot at Microsoft decided to introduce Site as a namespace into its .NET library suite. It broke my web service without warning, and it took me a few hours to get the web service working again (and a lot of painful recoding).

#### Chestermiller

Mentor
Methods are functions. They can get arguments and they can return results.

What is your goal, and how are you studying?

If you want to learn OOP, then I suggest a tutorial that includes coding examples. Based on your questions, it sounds like you are reading definitions without coding examples.
Any tutorial recommendations that you particularly like? Simple and gradual is highly desirable by me.

#### Mark44

Mentor
Any tutorial recommendations that you particularly like? Simple and gradual is highly desirable by me.
Chet, take a look at this book -- https://www.amazon.com/dp/B00F9311YC/?tag=pfamazon01-20
I looked at 20 or 30 books on Amazon, but didn't see any names that I had recognized from years back when I taught C++ (other then Herb Schildt, who has received scathing criticism for the errors in his books).
The author of the book I linked to, Alex Allain, has a website called cprogramming.com. I've looked at a number of his articles on that site, and they seemed quite good. He also has tutorials for C and C++ on his site, so you might start there. If you like what he has to say, you might consider getting the book, too. It's relatively inexpensive, at about \$35, cheap as programming books go.

#### Chestermiller

Mentor
Thanks Mark. I'll take a look. My main interest is C#, and I've had some experience with learning and solving practice problems with that until I ran into OOP. Then, head spinning. Any C# recommendations that make OOP simpler?

Mentor

Mentor

#### fog37

Thank you everyone. I am getting some concepts clear about OOP.

As far as a function being void, returning a parameter, etc. I am used to think of a function as receiving one or multiple input that it will use and process to produce an output: $output = f(x_1, x_2, etc.)$ If the function is void, it means it does not receive any parameter to work on. For example, in the Arduino programming language,
there is function loop() which is void. But lots of activities happen inside the loop() function...

In C, for example, it seems that the return instruction can only provide a single output value. What if a function needed to manipulate multiple inputs to produce multiple outputs? Having the return function with only a single output seems limiting. Where am I wrong?

Thanks.

#### PeterDonis

Mentor
In C, for example, it seems that the return instruction can only provide a single output value.
But that output value can be a pointer, which can point to anything--it can point, for example, to a structure containing multiple fields (and some of those fields can in turn be pointers to still other structures, etc.).

What if a function needed to manipulate multiple inputs to produce multiple outputs?
Then it would store the multiple outputs in a structure and return a pointer to it. Or, which is a much more common technique, one of the inputs could be a pointer to a structure that the function will store its outputs in. (With this technique, the function is usually either void or returns a value indicating whether it succeeded or failed at its task.)

#### PeterDonis

Mentor
If the function is void, it means it does not receive any parameter to work on.
No, that's just a function with no parameters. A void function is a function that returns no result (except that, as I said in my previous post just now, it can still receive a pointer to a structure that it stores results in). At least, that's the C terminology.

#### Mark44

Mentor
If the function is void, it means it does not receive any parameter to work on.
No, that's just a function with no parameters. A void function is a function that returns no result (except that, as I said in my previous post just now, it can still receive a pointer to a structure that it stores results in). At least, that's the C terminology.
Just to clarify, a void function does not return a result. If one of its parameters is a pointer (to, say, a structure), when the function returns, the structure that is pointed to will have been updated with new values. In this case, the structure is being passed by reference, as opposed to being passed by value. Technically, the function doesn't return any value, so qualifies for being a void function.

#### fog37

Thank you very much everyone. OOP has become more clear.

Let me post this short Arduino code:

The loop function does not take any input parameter and does not output any parameter. However, various simple calculations and actions (print, etc.) are performed within its curly brackets. The function loop may not take any input parameters but it uses the variables and data outside its scope...

So, when a function uses data defined externally to it, it still does not mean that is it receiving parameters. For example, let's think of a function performing a calculation, with a certain formula, using values that stored in variables outside of the function. Are those values parameters? What about the result of the calculation? Would that be the output of the function or not necessarily?

#### Attachments

• 47.9 KB Views: 160

#### FactChecker

Gold Member
2018 Award
"parameters" usually refers to the specific data that is passed in as part of the function call. Data obtained another way are not called parameters. When external data is modified which has not been accessed through the parameter list, that is called a "side effect". Many people hate side effects because it makes the effect of the code more difficult to keep track of. The parameter list is where people will look first to see what a function does. It is also a way that specific functions with the same name but with different parameter lists can be distinguished from each other. When two functions have the same name, there are programming tools which use the parameter list to help the programmer decide which function he wants to use.

#### Mark44

Mentor
The parameter list is where people will look first to see what a function does. It is also a way that specific functions with the same name but with different parameter lists can be distinguished from each other. When two functions have the same name, there are programming tools which use the parameter list to help the programmer decide which function he wants to use.
The mechanism discussed above is called function or method overloading, and pertains to C++, C#, Java, and possibly other languages. It does not pertain to C.

"Object Oriented Programming vs Structural Programming"

### Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving