Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What to include in main function?

Tags:

c++

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.

like image 396
Sir Meysam Ferguson Avatar asked Aug 12 '26 23:08

Sir Meysam Ferguson


2 Answers

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)
like image 55
sampson-chen Avatar answered Aug 14 '26 17:08

sampson-chen


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.

like image 30
nbs Avatar answered Aug 14 '26 15:08

nbs



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!