recursive - simple screenshot but detail principle.

时间:2023-03-09 20:30:02
recursive - simple screenshot but detail principle.

the code below demonstates the principle of the'recursive-call' that the programing beginner may be confused with,and each demonstrate was followed by a screenshot:

the first part of the code called in the main:

 String s2 = toBinary2(6, sb);

the actual code:

 static String toBinary2(long n, StringBuilder sb)
{
if(n > 0)
{
toBinary2(n / 2, sb);
sb.append(n % 2);
}
return sb.toString();
}

the relevant screenshot:

recursive - simple screenshot but detail principle.

figure 01 - to binary

the second part of the code called in the main:

 int sum = sum(100000);

the actual code:

 static int sum(int num)
{
if(num == 1)
return 1;
return num + sum(num - 1);
}

the correspond screenshot:

recursive - simple screenshot but detail principle.