What is the diffrence between a function and a module?
Asked Answered
P

3

7

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 programming, than modular programming and finally,OOP.

Papillon answered 22/1, 2011 at 10:20 Comment(4)
"Modular programming" isn't a formal concept in C++. Did you mean "object-oriented programming"?Belleslettres
"I just know modules by definition that it contains functions." - I have never heard of this definition of "modules" in the context of C++. Care to elaborate or explain? Where did you hear of this?Desai
I am talking about modular programming irrespective of language.Sorry for the c++ tag.Papillon
by "function oriented" do you mean Procedural or Functional (totally different, even contrasting, paradigms)?Sombrous
T
4

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.

Thoughtful answered 22/1, 2011 at 11:2 Comment(0)
C
3

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.

Camera answered 22/1, 2011 at 10:34 Comment(2)
Thanks.You means its kind of nested functions?Papillon
I wouldn't use the word nested. In lisp, the functions are nested as it's a process of synthesis (look at code examples and you'll see what I mean). I'd use the word "grouping".Camera
H
0

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

Harrar answered 20/10, 2018 at 16:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.