开发者

What is the diffrence between a function and a module?

I am very new to c++ and confused between what is the difference between modular programming and function oriented programming.I have never done modular programming so I just know modules by definition that it contains functions.So what is the difference between a sequential(function-oriented language)and modular programming?Thanks in advance.

EDIT: I was reading about C++'s OOP.It started something like what is unstructured programming, than gave a basic idea about structured programmin开发者_如何学Cg, than modular programming and finally,OOP.


Modular programming is mostly a strategy to reduce coupling in a computer program, mostly by means of encapsulation.

Before modular programming, local coherence of the code was ensured by structured programming, but global coherence was lacking: if you decided that your spell-checking dictionary would be implemented as a red-black tree, then this implementation would be exposed to everyone else in the program, so that the programmer working on, say, text rendering, would be able to access the red-black tree nodes to do meaningful things with them.

Of course, this became hell once you needed to change the implementation of your dictionary, because then you would have to fix the code of other programmers as well.

Even worse, if the implementation detail involved global variables, then you had to be exceedingly careful of who changed them and in what order, or strange bugs would crop up.

Modular programming applied encapsulation to all of this, by separating the implementation (private to the module) from the interface (what the rest of the program can use). So, a dictionary module could expose an abstract type that would only be accessible through module functions such as findWord(word,dictionary). Someone working on the dictionary module would never need to peek outside that module to check if someone might be using an implementation detail.


They are both ways of structuring your code. If your interested in function-oriented programming and want to understand it a bit better, I'd take a look at lisp. C++ isn't truly function oriented as every function should return a value yet C++ functions can return void (making it a procedure rather than a function), so it's not a true functional programming language in the sense.

"I have never done modular programming so I just know modules by definition that it contains functions".

Modules are a level higher than functions.

That's a good start. Think of a function as a unit of work that does something and when you have several functions that you can group in a certain way, you put them in a module. So, string.h has a bunch of functions for working with strings, but you simply include the header and you have access to all those functions directly. You can then reuse those modules in other projects as you'd already used the modules previously and they've been (I assume) debugged and tested and stop people from reinventing the wheel. The whole point is to benefit from the cumulative experience.

I'd suggest you think of a project you'd like and write some functions and think about how you'd like to organize the code for another developer to use.

Hope this is of some use to you.


I believe functional programming leads us to micro services paradigm as for now while modular programming tends to similar to OOP concept.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜