Results 1 to 6 of 6

Thread: Ok. Some questions about Public Functions.

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    65

    Post

    In creating a function, what must I do?

    Do I need to write

    Public Function MYFACTORIAL(number as integer, otherone as integer, lastone as integer)

    number = otherone * lastone

    end function

    Or what? Someone show me the correct syntax for a public function. Thank you, goodbye.

    ------------------
    "I'm carrying a Geometry book, but I'm not in Geometry...it's crazy...crazy man!"

  2. #2
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    Post

    Hope this will work:
    Code:
    Public Function Factorial (anotherone as integer, lastone as integer)
        Factorial=AnotherOne * LastOne
    End Function
    To call this function and calculate value for Number do:
    Code:
    Number=Factorial (AnotherOne, LastOne)
    ------------------
    Visual Basic Programmer
    -----------------
    PolComSoft
    You will hear a lot about it.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 1999
    Posts
    65

    Post

    Well, would the same go for a for-next statement?

    Public Function MYFACTORIAL(numberin as Integer, newnumber as integer,counter as integer)

    MYFACTORIAL = For counter = 1 to numberin

    newnumber = newnumber * counter

    next

    end function

    Would it?

    ------------------
    "I'm carrying a Geometry book, but I'm not in Geometry...it's crazy...crazy man!"

  4. #4
    Serge's Avatar
    Join Date
    Feb 1999
    Location
    Scottsdale, Arizona, USA
    Posts
    2,744

    Post

    Not exactly:

    Code:
    Public Function MyFactorial(NumberIn As Integer)
        Dim i As Integer
        
        For i = 1 To NumberIn
            MyFactorial = MyFactorial + NumberIn
        Next
    End Function

    Example: MsgBox MyFactorial 10

    The result would 55.

    Regards,

    ------------------

    Serge

    Software Developer
    [email protected]
    [email protected]
    ICQ#: 51055819




    [This message has been edited by Serge (edited 11-04-1999).]

  5. #5
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    Post

    Code:
    Public Function Factorial(numberin As Integer)
        dim Counter as Integer
        Factorial = 1
        For counter = 1 To numberin
            Factorial = Factorial * counter
        Next counter
    End Function
    To call this function
    Code:
    Number=factorial(NumberIn)
    Hope this what You are looking for

    ------------------
    Visual Basic Programmer
    -----------------
    PolComSoft
    You will hear a lot about it.

    [This message has been edited by QWERTY (edited 11-04-1999).]

  6. #6
    Fanatic Member
    Join Date
    Oct 1999
    Location
    MA, USA
    Posts
    523

    Post

    Serge's method will only work for addition and substraction because if you use multiplication or division or something like that you will always have Factorial value of 0. On the other hand my method won't work for addition and substraction
    ------------------
    Visual Basic Programmer
    -----------------
    PolComSoft
    You will hear a lot about it.


    [This message has been edited by QWERTY (edited 11-04-1999).]

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