recursive function return Without return

In the following code, I wrote a function recursively which should return a value, and really when two numbers are equal it returns, in the function I did not use the return and she is still working, I'd love to get an answer to why it works?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
  #include <iostream>
#include <string.h>
using namespace std;

int gcd(int num1, int num2);

void main()
{
    int ret_num = gcd(24, 60);
    cout << ret_num << endl;
}

int gcd(int num1, int num2)
{
    if (num1 == num2)
        return num1;
    else
    {
        if (num1 < num2)
            gcd(num1, num2 - num1);
        else
            gcd(num2, num1 - num2);
    }
}
first of all, why are you using string.h?
second of all, dont use void main
third of all i think its just undefined behavior. what compiler are you using?
Why not use string.h?
I ran into Visual Studio 2013 also and Linux with g ++ compiler
Visual studio it's not showing me any error or warning.
Last edited on
<string.h>
This is the C string functions. This file contains functions defined for the "C" programming language (Not C++), things like strlen() and strcpy().

<string>
This is the correct one to use as you will be able to do more with the C++ std::string class. Since you are programming in C++ you will want to use <string>.

Now if you intend to use <string.h> (Which I 99% suspect you don't) You would need to actually use <cstring>.

------------------------------------------------------------------------------------------------------------------

Now, why not to use void main(), the super simplified answer is that C++ kind of requires main() to return an int value. Just think of it this way. It is a standard that you are required to adhere to. In the same kind of standard that you cannot name your main() function superCOOLmainfunction(). It just has to be int main().
a) youre not using anything in it
b) thats a c library. the proper one would be <cstring>
c) if youre using c++, you might as well use <string> ie the std::string class. what version of g++ are you using?
Topic archived. No new replies allowed.