1

I have a problem. Here is the simplified version:

    #include <iostream>
    using namespace std;
    int main() {
      cout << "Hello!";
      return 0;
     }
    string name = "My name is ______";
    int main2() {
      cout << name;
      return 0;
     }

I have tried to remove the first return 0; in the main(), but it did nothing. I want to somehow start main2() from main(). Is it possible? I tried running the code(I use repl.it) and it didn't return any error messages. I also tried running it from the Terminal and it just prints Hello!.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Axolotl101
  • 31
  • 4
  • 1
    You need to _call_ main2. – tkausl Jun 26 '19 at 07:54
  • soo, how do i do that? – Axolotl101 Jun 26 '19 at 07:54
  • 1
    ... and declare it before `main`, as its definitions appears after `main`. – lubgr Jun 26 '19 at 07:55
  • 4
    Hi, welcome to SO. If you've just started learning C++, you might want to pick up a [good C++ book](https://stackoverflow.com/q/388242/1782465) to get a solid foundation to build on. – Angew is no longer proud of SO Jun 26 '19 at 07:55
  • Put `main2();` in your `main` then. In C++ you need to declare the function before you use it, so either put the entirety of `int main2()` before `int main` or put a prototype before `int main` by adding `int main2();` – Blaze Jun 26 '19 at 07:55
  • I think part of the confusion here is due to misunderstanding the `main` function. `main` gets a special treatment, unlike other functions. The comprehensive details are complicated, but a good C++ book/tutorial should be able to address this. – TrebledJ Jun 26 '19 at 07:58

3 Answers3

3

Yes. Try something like this:

#include <iostream>
using namespace std;

// Forward declare |main2|.
int main2();

int main() {
  cout << "Hello!";
  main2();
  return 0;
}
string name = "My name is ______";
int main2() {
  cout << name;
  return 0;
}

To elaborate on the differences between C++ and Python here: In C++ main is the entry point for your program. So in C++, you can think of the start of the main function as the equivalent of Python's "top of the file". Your program will begin running at the top of main, and stop running at the end of it. Anything that is not invoked starting from the main function (or transitively invoked) will not be executed.

Nathan Rogers
  • 444
  • 2
  • 4
  • You should `return main2();` rather than calling `main2` and returning 0. – Holt Jun 26 '19 at 07:59
  • 1
    It depends on what the goals of the program are. It's not wrong to ignore the return value of `main2` and return our own 0. Personally I would prefer it if `main2` were of return type `void`, but I wanted to keep things similar to the example code. – Nathan Rogers Jun 26 '19 at 08:01
  • Just to say, this worked. Thank you Natah Rogers! I'll mark it as soon as I can. "Avoid comments like +1 or Thanks!" oh well... – Axolotl101 Jun 26 '19 at 08:02
  • Returning `0` in a main indicates wheter your program has terminated correctly or not. So if you do `main2(); return 0;`, you indicates that the program has terminated correctly, even if `main2()` has failed. By doing `return main2();`, you forward the information from `main2` to the original caller. There are other ways to do so, but discarding the return value of `main2` is certainly not a good idea. – Holt Jun 26 '19 at 08:03
  • Sure, for `main` in particular that is true, however I would argue that `main2` "failing" vs "succeeding" here is nonsensical. – Nathan Rogers Jun 26 '19 at 08:04
  • "nonsensical"? Why? `main2` returns an `int`, which appears to be an error code. Even if in this example the error code is meaningless, it is present, so you should not discard it... – Holt Jun 26 '19 at 08:05
  • I guess I just don't see the `int` it returns as something that "appears to be an error code". I see it more as something that just shouldn't be there in the first place. If the purpose here were to demonstrate handling/propagating an error code, then I would strongly prefer if the example code that did that at least had a path handling a legitimate error. – Nathan Rogers Jun 26 '19 at 08:09
3

What you wrote is roughly equivalent to the following Python:

def main():
  print('Hello')
  return 0

name = 'My name is ____'

def main2():
  print(name)
  return 0

if __name__ == '__main__'
  sys.exit(main())

I believe now it's clear why main2 never gets executed: it's never called.

Note that the main function in C++ is the one function which gets called automatically at program start, and exiting main terminates the program. Whatever you want to happen while your program is running must be called from within main.

Also note that a name (such as a function) must be declared before it can be used. So you'll have to either move the definition of main2 before that of main, or at least declare main2 there. Which you could do like this:

#include <iostream>

using namespace std;

int main2();

int main() {
  cout << "Hello!";
  return main2();
}

string name = "My name is ______";

int main2() {
  cout << name;
  return 0;
}
Angew is no longer proud of SO
  • 167,307
  • 17
  • 350
  • 455
0

You have to call main2() inside main():

#include <iostream>
    using namespace std;

        string name = "My name is ______";
        int main2() {
          cout << name;
          return 0;
         }

        int main() {
          cout << "Hello!";
          main2();
          return 0;
         }
Paolo Mossini
  • 1,064
  • 2
  • 15
  • 23