|
-
Dec 8th, 2016, 05:21 PM
#18
Re: Recursion
 Originally Posted by boops boops
George, what you are doing is not the same as a recursive function.
If any function, recursive or not, returns an expression like f() - 100000000, the computer will have to evaluate the function f() before subtracting 100000000. But if f returns a specific value (like 0) from a recursive function, that's the result. The function's work is finished. No subtractions. No appeals. The 100000000 is a forgotten pipe-dream. The result is 0.
The Return statement is in some respects similar to Exit Sub or Exit Function. Whatever comes after that is irrelevant; it's not part of the program flow. A recursive function needs a mechanism of this kind because otherwise it would go on forever or at least until you end up with StackOverflow (and you know what they're like  ).
BB
 Originally Posted by boops boops
This particular function can't be done non-recursively. Its whole purpose is probably to test your basic understanding of recursion. It will become clear once you use the function to calculate a series of values "on paper". Bear in mind that a Return statement with a literal value like Return 0 implies "here is the end result, now exit the calculation".
BB
I'm not sure what you're trying to say here boops.
Any statements after a Return statement won't be executed, but the subtraction of 1 or the adding of 2 is not another statement, it is part of the statement that contains the function call.
The function will return, and the subtraction or addition will be done and returned.
Whether you code the function recursively or code it using loops and conditionals (non-recursively), the answer will be the same, i.e. 444 for the input value of 1337.
If the subtraction wasn't done, then changing the - 1 to - 3 or - 5 would make no difference, but of course it does make a difference.
I'm really not sure what your point is.
p.s. Here are my recursive and non-recursive versions of the function
Code:
Function F(N As Integer) As Integer 'Recursive
If (N = 0) Or (N = 1) Then
Return 0
ElseIf (N Mod 2 = 0) Or (N Mod 3 = 0) Then
Return F(N - 1) - 1
Else
Return F(N - 2) + 2
End If
End Function
Function F2(N As Integer) As Integer 'NonRecursive
Dim r As Integer
Do
If (N = 0) Or (N = 1) Then
Exit Do
ElseIf (N Mod 2 = 0) Or (N Mod 3 = 0) Then
N -= 1
r -= 1
Else
N -= 2
r += 2
End If
Loop
Return r
End Function
Also, as noted, you should see a repetitious pattern, which can be calculated for an input N (0 to ....) without using a loop.
Code:
Function F3(N As Integer) As Integer
Dim r, i As Integer
If N = 0 Then Return 0
i = (N - 1)
r = (i \ 6) * 2
i = i Mod 6
If i <> 0 Then
r -= i Mod 4
End If
Return r
End Function
Test:
Code:
Private Sub Button2_Click(sender As System.Object, e As System.EventArgs) Handles Button2.Click
For i As Integer = 0 To 1337
Debug.Print("In: {0,4} Out:(F: {1,3}, F2: {2,3}, F3: {3,3} )", i, F(i).ToString, F2(i).ToString, F3(i).ToString)
Next
End Sub
Example Output:
Code:
'Note the pattern, incrementing by 2 in a cycle of 6
In: 0 Out:(F: 0, F2: 0, F3: 0 )
In: 1 Out:(F: 0, F2: 0, F3: 0 ) '<=== 0
In: 2 Out:(F: -1, F2: -1, F3: -1 )
In: 3 Out:(F: -2, F2: -2, F3: -2 )
In: 4 Out:(F: -3, F2: -3, F3: -3 )
In: 5 Out:(F: 0, F2: 0, F3: 0 )
In: 6 Out:(F: -1, F2: -1, F3: -1 )
In: 7 Out:(F: 2, F2: 2, F3: 2 ) '<=== 2
In: 8 Out:(F: 1, F2: 1, F3: 1 )
In: 9 Out:(F: 0, F2: 0, F3: 0 )
In: 10 Out:(F: -1, F2: -1, F3: -1 )
In: 11 Out:(F: 2, F2: 2, F3: 2 )
In: 12 Out:(F: 1, F2: 1, F3: 1 )
In: 13 Out:(F: 4, F2: 4, F3: 4 ) '<=== 4
In: 14 Out:(F: 3, F2: 3, F3: 3 )
In: 15 Out:(F: 2, F2: 2, F3: 2 )
In: 16 Out:(F: 1, F2: 1, F3: 1 )
In: 17 Out:(F: 4, F2: 4, F3: 4 )
In: 18 Out:(F: 3, F2: 3, F3: 3 )
In: 19 Out:(F: 6, F2: 6, F3: 6 ) '<=== 6
'.....
In: 1316 Out:(F: 437, F2: 437, F3: 437 )
In: 1317 Out:(F: 436, F2: 436, F3: 436 )
In: 1318 Out:(F: 435, F2: 435, F3: 435 )
In: 1319 Out:(F: 438, F2: 438, F3: 438 )
In: 1320 Out:(F: 437, F2: 437, F3: 437 )
In: 1321 Out:(F: 440, F2: 440, F3: 440 ) '<=== 440
In: 1322 Out:(F: 439, F2: 439, F3: 439 )
In: 1323 Out:(F: 438, F2: 438, F3: 438 )
In: 1324 Out:(F: 437, F2: 437, F3: 437 )
In: 1325 Out:(F: 440, F2: 440, F3: 440 )
In: 1326 Out:(F: 439, F2: 439, F3: 439 )
In: 1327 Out:(F: 442, F2: 442, F3: 442 ) '<=== 442
In: 1328 Out:(F: 441, F2: 441, F3: 441 )
In: 1329 Out:(F: 440, F2: 440, F3: 440 )
In: 1330 Out:(F: 439, F2: 439, F3: 439 )
In: 1331 Out:(F: 442, F2: 442, F3: 442 )
In: 1332 Out:(F: 441, F2: 441, F3: 441 )
In: 1333 Out:(F: 444, F2: 444, F3: 444 ) '<=== 444
In: 1334 Out:(F: 443, F2: 443, F3: 443 )
In: 1335 Out:(F: 442, F2: 442, F3: 442 )
In: 1336 Out:(F: 441, F2: 441, F3: 441 )
In: 1337 Out:(F: 444, F2: 444, F3: 444 )
Last edited by passel; Dec 8th, 2016 at 08:24 PM.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|