decoder__'s blog

By decoder__, history, 4 years ago, In English

I have seen in many people's code that they use a solve function instead of writing the logic in the main function. What advantage it has over writing the code directly in the main function?

  • Vote: I like it
  • +7
  • Vote: I do not like it

| Write comment?
»
4 years ago, # |
  Vote: I like it +72 Vote: I do not like it

You can use return directly in solve() in case there are test cases, while in int main() you can't

  • »
    »
    4 years ago, # ^ |
      Vote: I like it -28 Vote: I do not like it

    "continue" keyword is also there.

    • »
      »
      »
      4 years ago, # ^ |
        Vote: I like it +13 Vote: I do not like it

      but you have to use many breaks statements too if you want to end the current test case from a loop

      • »
        »
        »
        »
        4 years ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        i just use goto statement to go back to the starting of the test case loop, is that less efficient?

  • »
    »
    4 years ago, # ^ |
      Vote: I like it +3 Vote: I do not like it

    Actually you can use goto but that would make spaghetti code

»
4 years ago, # |
  Vote: I like it +17 Vote: I do not like it

I personally use it because you can easily end the function with return(end).

»
4 years ago, # |
  Vote: I like it -26 Vote: I do not like it

I think it doesnt have obvious advantages. But many people are used to it since it looks comfortable. After all, the choice is up to you.

»
4 years ago, # |
  Vote: I like it +12 Vote: I do not like it

It's good to split everything in functions

»
4 years ago, # |
  Vote: I like it 0 Vote: I do not like it

Isn't it too obvious? Asking everything sucks

»
4 years ago, # |
  Vote: I like it +5 Vote: I do not like it

What is there in it to downvote except colortag?

»
4 years ago, # |
  Vote: I like it +20 Vote: I do not like it
solve()
main()

You decide.

»
4 years ago, # |
Rev. 2   Vote: I like it +6 Vote: I do not like it

.....................................................................................