Sir Meysam Ferguson
Sir Meysam Ferguson

Reputation: 193

What to include in main function?

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

Answers (5)

sampson-chen
sampson-chen

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:

  • Go grocery shopping for ingredients
  • Pre-process ingredients
  • Cook food
  • Serve food
  • Clean dishes

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)

  • Go grocery shopping for ingredients:
    • Make a shopping list
    • Visit grocery store
    • Collect ingredients
    • Pay for ingredients
    • Return home
  • Pre-process ingredients:
    • Wash vegetables
    • Chop up the meat
    • Chop up the ginger, the cloves...
  • Cook food
    • Prepare pots & pans
    • Add cooking oil
    • Turn on stove
    • Cooke meat with vegetables in pan until done
  • Serve food:
    • Set plates
    • Divide food into portions
    • Call others to dinner
    • Eat dinner
  • Clean dishes:
    • Wash dishes with detergent
    • Rinse dishes with water
    • Put dishes away

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:

  • Go grocery shopping for ingredients:
    • Make a shopping list:
      • Open fridge
      • Examine contents of fridge
      • Write down which ingredients are running low in the fridge.
      • Close fridge
    • Visit grocery store:
    • Collect ingredients:
      • Open fridge
      • Put frozen vegetables in shopping cart
      • Close fridge
      • Put fresh meat in shopping cart
    • Pay for ingredients:
    • Return home:
  • Pre-process ingredients:
    • Wash vegetables:
      • Open fridge
      • Retrieve vegetables from fridge
      • Close fridge
      • Rinse vegetables under tap
    • Chop up the meat:
    • Chop up the ginger, the cloves...

Now notice two things:

  1. There is now a very logical "top-down" tree-like structure to your program's approach, by breaking major tasks into smaller subtasks. When you are looking at main, you can immediately see what the overall plan is without having to understand exactly how each step is implemented. But by examining the individual functions, you then immediately get a sense of how each function accomplishes its task... and so on.
  2. You can now reuse any abstracted logic that was encapsulated into functions (e.g. the open/close fridge actions listed in bold) in multiple places. When you need to open the fridge, you can just call "openFridge" instead of having to re-code exactly how to do it each time. (You can technically copy and paste code, but that causes an anti-pattern called code cloning: if you need to change how to open the fridge, you'd have to do it everywhere instead of just in 1 function)

Upvotes: 6

Charles Burns
Charles Burns

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

krammer
krammer

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

user1797612
user1797612

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

nbs
nbs

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

Related Questions