Recursive Function Problem

1. Member
Join Date
Dec 1969
Posts
57

## Recursive Function Problem

Hi. Can someone explain to me<BR>1). Why my function is returning a binary value<BR>2). How to get it to return an integer value<BR>Function:<BR>&#060;% Option Explicit<BR><BR> Dim intNumber<BR> intNumber = Request.Form("input")<BR> Dim intNumber2<BR><BR> Function Fibo(intNumber)<BR> If (intNumber = 0 OR intNumber = 1) Then<BR> Response.Write intNumber<BR> Else<BR> intNumber2 = Fibo(intNumber - 2) + Fibo(intNumber - 1)<BR> Response.Write intNumber2<BR> End If<BR> End Function<BR> Fibo(intNumber)<BR><BR>%&#062;<BR>Thanks,<BR>Steve

2. Senior Member
Join Date
Dec 1969
Posts
623

## RE: Recursive Function Problem

Your function is never returning a value, you are just writing the value out to the screen...<BR><BR>This is how you return a value<BR><BR>Function someFunction (thisAndThat)<BR> someFunction = thatAndThis<BR>End Function<BR><BR>

3. Senior Member
Join Date
Dec 1969
Posts
96,118

## What return value???

You never return *ANY* value from the function, you know?<BR><BR>That&#039;s part of the problem. I&#039;ll let you work on that for a while.<BR><BR>(And you *aren&#039;t* getting back a binary number. You are simply getting back a bunch of decimal one and zero values.)<BR><BR>

4. Senior Member
Join Date
Dec 1969
Posts
623

## RE: What return value???

Long time Bill, how have you been?

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## I can't be all that good...

...if I let you beat me to an answer. &#060;snicker /&#062;<BR><BR>Or if I&#039;m answering silly questions this late at night. &#060;yawn /&#062;<BR><BR>Did you try that code, with return values added in? I&#039;m not sure how you are supposed to meaningfully get an output stream from it. Seems kind of a silly way to get a Fibonacci sequence, to me, when the loop way is so straightforward.<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
623

## Code?

I didn&#039;t see no code anywhere... Just got through with my finals today, all I can see is my bed and my good ol&#039; coors lit ;o)

7. Member
Join Date
Dec 1969
Posts
57

## RE: I can't be all that good...

I&#039;ve been trying to get this thing to return a value, but I really don&#039;t know what I&#039;m doing (sob) :0)

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## Well...here's what I did...

...to test it out.<BR><BR>&#060;HTML&#062;&#060;BODY&#062;<BR><B R>&#060;%<BR>Function Fibo(intNumber) <BR>&nbsp; &nbsp; Dim intNumber2 &#039; put the DIM in the right spot<BR>&nbsp; &nbsp; If (intNumber = 0 OR intNumber = 1) Then <BR>&nbsp; &nbsp; &nbsp; &nbsp; &#039; Response.Write intNumber & "-" &#039; I don&#039;t think you want this!<BR>&nbsp; &nbsp; &nbsp; &nbsp; Fibo = intNumber &#039; what you were missing!<BR>&nbsp; &nbsp; Else <BR>&nbsp; &nbsp; &nbsp; &nbsp; intNumber2 = Fibo(intNumber - 2) + Fibo(intNumber - 1) <BR>&nbsp; &nbsp; &nbsp; &nbsp; Response.Write intNumber2 & "#"<BR>&nbsp; &nbsp; &nbsp; &nbsp; Fibo = intNumber2 &#039; also missing!<BR>&nbsp; &nbsp; End If <BR>End Function<BR> <BR>Fibo(7) <BR>Response.Write "&#060;HR&#062;"<BR>Fibo(5)<BR>%&#062;<BR><BR>&#06 0;/BODY&#062;&#060;/HTML&#062;<BR>*******************************<BR>< BR>The final sequence of numbers you see gives the right answer, but it&#039;s unclear to me how you suppress all the prior sub-sequences.<BR><BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
623

## RE: I can't be all that good...

Function Fibo(fibIndex)<BR> If (fibIndex &#060; 2) Then<BR> Fibo = fibIndex<BR> Else<BR> Dim fibArray(2) As Integer<BR> Dim i As Integer<BR> fibArray(0) = 0<BR> fibArray(1) = 1<BR> For i = 2 To fibIndex<BR> fibArray(1) = fibArray(1) + fibArray(0)<BR> fibArray(0) = fibArray(0) + fibArray(1)<BR> Next<BR> Fibo = fibArray(1)<BR> End If<BR>End Function

10. Member
Join Date
Dec 1969
Posts
57

## I got it to work...

But I&#039;m not sure what I&#039;ve done! Here it is:<BR>&#060;% Option Explicit<BR><BR> Dim intNumber<BR> intNumber = Request.Form("input")<BR> Dim intAgain<BR><BR> Function Fibo(intNumber)<BR> If (intNumber = 0 OR intNumber = 1) Then<BR> Fibo = intNumber<BR> Else<BR> Fibo = Fibo(intNumber - 2) + Fibo(intNumber - 1)<BR> End If<BR> End Function<BR> Response.Write Fibo(intNumber)<BR>%&#062;<BR>I&#039;m a beginner, so please bear with me. I got this code from a beginning-level C++ course and have made a mini project out of trying to write it in ASP. I got some help this morning when I asked how to return a value in ASP:<BR>Function Add(a,b)<BR> Add = a+b<BR>End Function<BR>I&#039;m trying to implement what I&#039;m learning. Thanks again, Steve

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•