-2

I'm a bit new to c++ and I've only recently learned about functions..The main function int main(){} function is just like any other function and therefore you can pass arguments into it right...well I want to know how to use that ability to insert arguments to make the code better...and also how those arguments affect the content of the function...normally in other functions the arguments we use in functions is used in the processing of the content in that function...for example:-

int sum(int firstnumber, int secondnumber){
       return firstnumber+secondnumber;}

if the function above was performed without parameters, it wouldn't make sense and therefore it wouldn't output right...but that doesn't apply to main(){} function...I haven't yet used any parameter to the 'main(){}' function but the code runs just fine...I would like to know why that is and also how , if possible, to modify other functions that I create to be like the main function and not always accept inputs or arguments....I looked into this in other sources but different sources give different results and as I am a bit new to c++ I don't really understand all the complicated details...so I would really appreciate it if you can help me...thanx a million in advance

  • 4
    The main function accepts argc and argv, and can be used to pass commandline information into your program. E.g. https://www.geeksforgeeks.org/command-line-arguments-in-c-cpp/. For functions : https://www.learncpp.com/cpp-tutorial/introduction-to-functions/ – Pepijn Kramer Jun 03 '22 at 08:37
  • 1
    `the main function is just like any other function` that's not true, it's a special function which will be called in some implementation specific way at the start of the program. Calling `main` directly in your program is not allowed – Alan Birtles Jun 03 '22 at 09:17
  • Related, but not quite the same question: [Why is no argument passed in main function?](https://stackoverflow.com/questions/51645688/) and [What does `int argc, char *argv[]` mean?](https://stackoverflow.com/questions/3024197/) – JaMiT Jun 03 '22 at 09:39
  • 1
    *"how those arguments affect the content of the function"* -- that is up to the person writing the function. Not really a question that can be answered without more context. If you choose this to be the *one* question for this question, please clarify it. – JaMiT Jun 03 '22 at 09:44

0 Answers0