| Author |
Message |
victorf
Joined: 01 Jan 2006
Location: Schenectady, New York
|
|
Posted: 23 January 2006, 15:17 PM Post subject: Functions - How do they return |
|
|
| Code: |
Function foo(byval x as integer) as boolean
foo = False
if x > 10 then
foo = True
end if
end Function
|
Can I do this In other words can I set the function false then carry on with the function's statements and eventually set the function true if necessary?
Any enlightenment will be appreciated.
Vic |
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Location: Portland, OR
|
|
Posted: 23 January 2006, 16:49 PM Post subject: |
|
|
What you suggest will work and is commonly done. In your function, "foo" is an automatically-defined local variable that has the same type as the function. This is called the "return value variable" and it always has the same name as the function itself. You can use it just like any other variable meaning that you can assign to it, pass it (by value or by reference) to another procedure, take its address, etc.
As a side note, the name of the function has two different meanings within the function itself. When used one way, it refers to the return value and when used in another way, it refers to the function itself. Consider the following code:
| Code: | Function foo(ByVal x as Integer) as Boolean
foo = False
If x > 10 Then
foo = foo(x \ 10)
End If
End Function |
On the fourth line, 'foo' is used in both contexts. The compiler distinguishes between the contexts by the presence of parentheses following the name - a reference to the function foo must have them while a reference to the return value will not. |
|
| Back to top |
|
 |
victorf
Joined: 01 Jan 2006
Location: Schenectady, New York
|
|
Posted: 23 January 2006, 20:00 PM Post subject: |
|
|
| Code: |
Function foo(ByVal x as Integer) as Boolean
foo = False
If x > 10 Then
foo = foo(x \ 10)
End If
End Function
|
This seems a bit fishy to me It smacks of recursion. How is it different from recursion
Vic |
|
| Back to top |
|
 |
mikep
Joined: 24 Sep 2005
Location: Austin, TX
|
|
Posted: 23 January 2006, 20:25 PM Post subject: |
|
|
It is recursion. Don was making 2 points:
1. Explicit: how the compiler differentiates between the return value foo and the call to the function foo
2. Implicit: The fact that the ZBasic language supports true recursion whereas BasicX cannot support this syntax and you need two functions that mutually call each other for the same effect. |
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Location: Portland, OR
|
|
Posted: 23 January 2006, 20:31 PM Post subject: |
|
|
| Quote: | | This seems a bit fishy to me. It smacks of recursion. |
There is nothing fishy about recursion. It is simply a useful tool that, like all tools, can be used properly and improperly and, in the hands of the careless or untrained, can be dangerous. |
|
| Back to top |
|
 |
victorf
Joined: 01 Jan 2006
Location: Schenectady, New York
|
|
Posted: 24 January 2006, 11:59 AM Post subject: |
|
|
| Quote: | This seems a bit fishy to me. It smacks of recursion.
|
I didn't mean to imply that recursion was fishy. I have been known to use recursion from time to time. I guess I just didn't understand Don's reasoning in light of my original post. I'm not sure that everyone is aware that the function name can be used in the manner that I was asking about. Delphi (Borland Pascal) has gone one step further with this by having a pre-declared variable Result that acts the same way as foo in my example so one can use it or the function name in the manner I was asking about. However, Result cannot be used recursively.
Speaking of recursion, one needs to be careful about stack space depletion. How deep can you recurse?
Vic |
|
| Back to top |
|
 |
mikep
Joined: 24 Sep 2005
Location: Austin, TX
|
|
Posted: 24 January 2006, 15:09 PM Post subject: |
|
|
Recursion depth depends on the amount of stack taken up by the parameters, the return result and the intermediate calculations of your recursive function. Section 3.18 (http://www.zbasic.net/doc/ZBasicRef/ZBasicRef69.html) discusses function recursion and section 3.11 (http://www.zbasic.net/doc/ZBasicRef/ZBasicRef64.html) discusses runtime stack checking.
Here is an example program that uses a recursive function to calculate the simple series 1 + 2 + 3 ... From high school math we know the sum is
I originally tried factorial but the Long variable that holds the result overflows before the stack runs out of space. Here is an example program that recursively calculates the addition series and also tracks when the stack overflows.
| Code: | Private Const MAX_DEPTH as Long = 82
Sub Main()
If Register.FaultType = 1 Then
Register.FaultType = 0
Debug.Print "Previous run resulted in a stack fault"
Exit Sub
End If
Call StackCheck(True)
Debug.Print CStr(SimpleSeries(MAX_DEPTH))
End Sub
Function SimpleSeries(ByVal val as Long) As Long
If Val > 1 Then
SimpleSeries = SimpleSeries(val-1) + val ' MAX_DEPTH = 82 with answer = 3403
'SimpleSeries = val + SimpleSeries(val-1) ' MAX_DEPTH = 62 with answer 1953
Else
SimpleSeries = 1
End If
End Function | Note that the two forms of calculation have different call depths i.e is the next recursive function call done first or second for the addition. In the second case the push of the val parameter is taking up an extra 4 bytes for every recursion. |
|
| Back to top |
|
 |
dkinzer Site Admin
Joined: 03 Sep 2005
Location: Portland, OR
|
|
Posted: 24 January 2006, 16:48 PM Post subject: |
|
|
| Quote: | | Speaking of recursion, one needs to be careful about stack space depletion. How deep can you recurse? |
It should be noted that the stack use analysis that I posted about yesterday is static and does not, therefore, take recursion into account. Even so, if you know the maximum number of times that your routine will be recursively invoked, you can add to the calculated stack usage that number times the stack usage of the recursively invoked routine to arrive at the maximum stack usage. |
|
| Back to top |
|
 |
|