Skip to content

Commit 563ad5a

Browse files
authored
Create OpenCV Python Tutorial
1 parent ea85d4c commit 563ad5a

File tree

1 file changed

+63
-0
lines changed

1 file changed

+63
-0
lines changed

OpenCV Python Tutorial

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
Java Recursion
2+
3+
Recursion is the technique of making a function call itself. This technique provides a way to break complicated problems down into simple problems which are easier to solve.
4+
5+
Recursion may be a bit difficult to understand. The best way to figure out how it works is to experiment with it.
6+
Recursion Example
7+
8+
Adding two numbers together is easy to do, but adding a range of numbers is more complicated. In the following example, recursion is used to add a range of numbers together by breaking it down into the simple task of adding two numbers:
9+
Example
10+
Get your own Java Server
11+
12+
Use recursion to add all of the numbers up to 10.
13+
14+
public class Main {
15+
public static void main(String[] args) {
16+
int result = sum(10);
17+
System.out.println(result);
18+
}
19+
public static int sum(int k) {
20+
if (k > 0) {
21+
return k + sum(k - 1);
22+
} else {
23+
return 0;
24+
}
25+
}
26+
}
27+
28+
Example Explained
29+
30+
When the sum() function is called, it adds parameter k to the sum of all numbers smaller than k and returns the result. When k becomes 0, the function just returns 0. When running, the program follows these steps:
31+
10 + sum(9)
32+
10 + ( 9 + sum(8) )
33+
10 + ( 9 + ( 8 + sum(7) ) )
34+
...
35+
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + sum(0)
36+
10 + 9 + 8 + 7 + 6 + 5 + 4 + 3 + 2 + 1 + 0
37+
38+
Since the function does not call itself when k is 0, the program stops there and returns the result.
39+
ADVERTISEMENT
40+
Halting Condition
41+
42+
Just as loops can run into the problem of infinite looping, recursive functions can run into the problem of infinite recursion. Infinite recursion is when the function never stops calling itself. Every recursive function should have a halting condition, which is the condition where the function stops calling itself. In the previous example, the halting condition is when the parameter k becomes 0.
43+
44+
It is helpful to see a variety of different examples to better understand the concept. In this example, the function adds a range of numbers between a start and an end. The halting condition for this recursive function is when end is not greater than start:
45+
Example
46+
47+
Use recursion to add all of the numbers between 5 to 10.
48+
49+
public class Main {
50+
public static void main(String[] args) {
51+
int result = sum(5, 10);
52+
System.out.println(result);
53+
}
54+
public static int sum(int start, int end) {
55+
if (end > start) {
56+
return end + sum(start, end - 1);
57+
} else {
58+
return end;
59+
}
60+
}
61+
}
62+
63+
The developer should be very careful with recursion as it can be qu

0 commit comments

Comments
 (0)