|
-
Aug 18th, 2005, 03:09 AM
#1
Thread Starter
New Member
Help for a newbie please
Hi
I am new to programming and to VB.net. and am working through the book ASP.NET in 24 hrs. There is a very simple exercise to create a factorial function called from the Page_Load event. I have got as far as the following code which works, but the book suggests that it is the function that should contain a loop.
Could anyone suggest an alternative way of setting up the function.
Thanks
Sub Page_Load (sender as Object, e as EventArgs)
Dim I as Integer
For I = 1 to 5
Response.Write (Fac (I) & "<br>")
Next I
End Sub
Function Fac (number as Integer) as Integer
If number <= 1 Then
Return 1
Else
Return number * Fac (number - 1)
End If
End Function
-
Aug 18th, 2005, 03:57 AM
#2
Re: Help for a newbie please
The one you have at the moment is recursive (it calls itself, decreasing the parameter each time until the termination condition is met).
The book wants you to alter the Fac fuction so that it doesn't call itself anymore but does all the calculations in one go.
Add new loop to Fac() that calculates the factorial for the number that is passed to Fac().
I'm surprised taht the book has taught recursion BEFORE iteration, thats a bit strange.
I don't live here any more.
-
Aug 18th, 2005, 09:54 AM
#3
Thread Starter
New Member
Re: Help for a newbie please
Thanks for your help. Cheers.
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
|