0
#include <iostream>
using namespace std;

int test(int numb) {
    if (numb == 0) return 0;
    test(numb - 1);
}

int main() {
    cout<<test(10);
}

so i was solving some algorithm questions. this code works in visual studio but not in other online shell. what does test() function returning even though there is no return? also

#include <iostream>
#include<stdio.h>
using namespace std;

int test(int numb) {
    if (numb == 0) return 0;
    cout<<test(numb - 1)<<endl;
}

int main() {
    cout<<test(10);
}

//result
0
1349009568
1349009568
1349009568
1349009568
1349009568
1349009568
1349009568
1349009568
1349009568
1349009568

what does thoes number 1349009568 means??

songgunho
  • 25
  • 2

1 Answers1

0

Your function has undefined behavior when it doesn't return anything. The number 1349009568 is meaningless and probably random. For me, it shows a 0, and has the same output as this version which will never return anything.

int test(int numb) {
    if (false) return -1;
}
Daniel Giger
  • 2,023
  • 21
  • 20