Reputation: 193
in top-down approach, it's recommended to make functions small to do just a single Task.
so. I don't know what main does? just invoking other functions or everything that we want?
thanks.
Upvotes: 3
Views: 1226
Reputation: 47267
The idea is to separate tasks into logical abstractions.
Here, I'll use an example of cooking in the kitchen to illustrate the concept. Suppose you want to prepare a delicious dinner with some dishes. Your mental checklist would probably start with the major steps:
Thus, these become the functions that would go in your main()
Then for each step, you break it down a little bit more: (Note that the sub-steps listed below are what each of the main functions would invoke from within their own function definitions)
Then for each sub-step, you can still make things more fine-grained to represent separate logical actions, we'll just examine the first 2 functions in main()
for brevity:
Now notice two things:
Upvotes: 6
Reputation: 10602
I like to think of main() as a blueprint for the whole program. It calls other functions in a high-level, "descriptive" manner. Really this describes how I think any function should read.
For example, a program that lets the user choose an image and shrink it to a thumbnail might go like:
// Pseudo-code
int main() {
string path = getImagePath();
checkImageValidity(path);
imageData imgData = new imageData();
imgData = readImageData(path);
writeImageData("../thumb.png", imgData, imageType.PNG);
fileClose(fp);
return 0;
}
Each function does some work and perhaps calls other functions, but by reading the main, you understand the general flow of the program. "It gets an image file path, checks to see it's a valid file, reads its raw image data, writes it to an output file, then closes it."
Real programs will be more complex than this one, but that just means you have more functions which should read the same way, as many levels deep and wide as the program's complexity merits.
Upvotes: 0
Reputation: 2658
In Top Down approach, you start with a high level design. So you first extract your ideas into a main() function (if you are using C or similar language). For example, if I want to calculate Fibonacci number, in top down approach I would start with
int main()
{
int *numbers;
int n = getCountOfNumbers(); // get the count of numbers to be generated
generateFibonacci(numbers, n); // generate numbers
processFibonacci(numbers); // display the numbers
return 0;
}
This sets up an abstract implementation of what you would be doing with your program. Next, you implement the functions, getCountOfNumbers(), generateFibonacci() and processFibonacci() to represent your logic and algorithm.
So, with main() function you can represent a high level organization of the program. Moreover since it is the start point in most of the programming language, your control flow starts from the main() function and then is delegated through to other functions in the program depending upon your call sequence.
Upvotes: 1
Reputation: 832
The top-down approach it's not really about functions and tasks in strict terms, it's more like an over simplified approach to the algorithms world.
Usually the top-down approach it's a topic that comes right after an introduction to the Turing theory and the algorithms to just let the student understand the practical implications and it's also a much easier alternative to a more complex and evoluted approach like the one that involves math + algorithms that it's usually something for the college and a computer science class.
You can also notice that speaking about a top-down approach no language comes in, it's a topic that abstract itself from any language or implementation of any kind, it's just about generic steps and algorithms with a problem-solving approach.
Regarding the C++, in really generic terms, the role of the main()
function is basically about starting the main thread for the application and managing the argc
and argv
values.
Upvotes: 0
Reputation: 573
Top-down approach starts with high level system or design then it goes to low level system or design or development. Top-down approach first focus on abstract of overall system or project. At last it focuses on detail design or development. In this approach first programmer has to write code for main function. In main function they will call other sub function. At last they will write code for each sub function.
Upvotes: 1