I am trying to learn about recursives and found this code on the internet. It works, but am not sure why. Could some one explain to me why this works. I have posted comments by the function to explain what I do not understand.
#include <iostream>
using namespace std;
void printDisplay(int);
int main()
{
printDisplay(5);
printDisplay(900);
printDisplay(1234);
return 0;
}
void printDisplay(int n) //I understand the n is coming from main
{
cout << n % 10; // this takes the number and gives the remainder when divided by 10.
if (n < 10) { //if statement understand this.
cout << endl; //why are you cout an empty line?
return; }//What are we returning.
printDisplay(n / 10);//why are we dividing the number by 10?
}