how does this program work

Can anyone please tell me how does this program work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include<iostream>
using namespace std;
void write_vertical(int n);
int main()
{
	cout<<"write_vertical(3)  ";
write_vertical(3);
	cout<<"wrtie_vertical(12)   ";
write_vertical(12);


	cout<<"wrtie_vertical(123)   ";
write_vertical(123);
return 0;
}
void write_vertical(int n)
{
	if (n<10)
	{
		cout<<n<<endl;
	}
	else
	{
		write_vertical(n/10);
		cout<<(n%10)<<endl;
	}
}
Execution starts a line 4, it's the first function to be called.
Line 6 writes write_vertical(3) without a linefeed.
Line 7 calls function write_vertical and passes 3 as its parameter
Execution transfers to line 16 where n is set to 3.
Line 18, n<10 is true
Line 20 calls writes an end of line sequence
The function returns to line 8
Line 8 writes write_vertical(12) without a linefeed.
Line 7 calls function write_vertical and passes 12 as its parameter
Execution transfers to line 16 where n is set to 12.
Line 18, n<10 is false
Line 24 calls function write_vertical and passes 2 as its parameter
Line 18, n<10 is true
Line 20 calls writes a carrage-return/linefeed
The function returns to line 25
Line 25 writes 2 with an end of line sequence
The function returns to line 10

... and so on
Thank you ! KBW
Topic archived. No new replies allowed.