tags:

views:

225

answers:

2

Hello. I came across a program with 10 header and 10 source files. I read in my text book that the functions are called from main. But how can I pass data to so many functions from main()?

+12  A: 

Functions don't necessarily need to called from main. They can be called by other functions. For example:

int foo(int x)
{
    return x*x;
}

int bar(int x)
{
   return foo(x) + 1;
}

int main()
{
    int a = bar(42);
    std::cout << a << std::endl;
    return 0;
}

Note that foo() is never called directly from main().

Emile Cormier
Thanks. But my program has 100 classes and more than 1000 functions spread over many files. How to call them from main()? What is the procedure adopted?
Supriyo
Many of those classes and functions will depend on other classes and functions, and so on. I suspect that you only need to call a few top-level ones from main, and it will trickle down from there. You'll have to study the code to figure out what your "entry points" are.
Emile Cormier
A nice way to quickly navigate through code, is to run the code through a documentation program called Doxygen. It will produce HTML documentation files with lots of hyperlinks, as well as header/class dependency graphs. Make sure you enable the SOURCE_BROWSER option, so that it produces HTML versions of the source code with hyperlinks to all classes, structs, functions, etc.
Emile Cormier
+1  A: 

To my mind, this phrase isn't correct, but I guess what was meant to be said could be rephrased like "Every function or class method that you implement and use would be somehow called from your main() routine"

And somehow in this context would actually mean directly or indirectly - via other functions / function wrappers.

Anyway, the idea should be clear - any significant action that is done in your application is actually done using some function call from your main() routine, which is sometimes also called application root (try to think of your application as a tree of function calls and then your main() function would be right in the top of your tree).

Kotti