Results 1 to 3 of 3

Thread: Help for a newbie please

  1. #1

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    2

    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

  2. #2
    type Woss is new Grumpy; wossname's Avatar
    Join Date
    Aug 2002
    Location
    #!/bin/bash
    Posts
    5,682

    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.

  3. #3

    Thread Starter
    New Member
    Join Date
    Aug 2005
    Posts
    2

    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
  •  



Click Here to Expand Forum to Full Width