What’s the different between C function and other OOP languages function

JohnGu
4 min readDec 14, 2020

You might think C function and OOP(Object-Oriented Programming) language function are the same stuff just in different language. But actually no. One sentence summary, C function a plain blueprint of operations that indicate machine how to handle data and OOP language function is an OBJECT that contain not only the function but also runtime context. So what’s the different behind.

First, let’s take a look at a common problem that almost every OOP programmer meet the problem in C++:

Two functions’ formats look identical but you can’t treat a class member function as a normal function in C/C++. How to solve? C++11 or later offer std::function and std::bind.

But it’s not enough to declare the problem. Without tear down and dive deep into the std library, let’s go all the way back to C++03. There’s no std::function and std::bind at all. And how to solve this problem?

Now you must notice that: you must use a class instance to “drive” the function. Why? Because a class member function maybe access the member variables in the runtime. But in C/C++, every single function is just a plain “function” that do the job for you but doesn’t take any care of the resource (The class member function doesn’t know where’s the instance in C++. Every member function is just only one piece of memory, just like class static function. ).

Without the resource, it can’t run at all. So if you use the function as a “dependent function”, you also need where the instance is at the same time. It’s what the std::bind do. Bind the instance pointer and function pointer together inside the std::function class.

But why you didn’t meet this problem in OOP languages? You know most of OOP language base on C/C++. They can’t break the “C/C++ physics rule”. OOP language also need to need the resource location. If the class member function doesn’t know where the resource is, it doesn’t work at all. How do they solve that?

nothing can’t go out side of my sight

Easy, the OOP languages do this for you automatically. OOP languages not only get the function native pointer but also hold the class instance reference. And then OOP languages package them together as a new resource, OBJECT! If you keep the object, you also keep a reference of the class instance in the dark side you may not know in the past. Now you can magically call it everywhere.

And it’s the reason that member function always need a “self” argument in Python, Python just do not hide the argument and expose it to you. It just tell you the “this” or “self” does not come from nowhere. But in runtime you don’t need to pass a class instance to it. Python will do it for you.

Another example is from old version JAVA. Old version JAVA doesn’t offer this feature. So JAVA use Runnable to solve problem usually. The Runnable class is the prototype of automatic reference mechanism that I mention just now. Now, you need to extend the class, add the resource you need inside the class. And create the class with resource actually is. Finally call run().

Oh, you will find it out immediately that you need to replace this job over and over again in the real work. Why do not let machine do it for you?

One more thing. The instance of the function is just one kind of runtime context. Another kind of runtime context that’s popular is in the closure function.

“Why human are so flexible? Because we can trace much context. ”

So, to make programming language more flexible. You just make it trace more context easily. And closure is available in many OOP language such as Kotlin, Python, Dart, JavaScript and Swift.

What’s the closure?

main.dart

Where’s the context it trace? Here, here and here. And language complier will automatically help you trace the context for you.

You don’t do anything(don’t need to pass something as arguments, or declear a class) but you can refer anything inside the current scope. Because OOP languages just do it for you.

OOP languages do a lot for you.

--

--