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:
1 String s2 = toBinary2(6, sb);
the actual code:
1 static String toBinary2(long n, StringBuilder sb) 2 { 3 if(n > 0) 4 { 5 toBinary2(n / 2, sb); 6 sb.append(n % 2); 7 } 8 return sb.toString(); 9 }
the relevant screenshot:
figure 01 - to binary
the second part of the code called in the main:
1 int sum = sum(100000);
the actual code:
1 static int sum(int num) 2 { 3 if(num == 1) 4 return 1; 5 return num + sum(num - 1); 6 }
the correspond screenshot: