here is my problem

Discussion in 'AutoCAD' started by youngman, Jul 7, 2004.

  1. youngman

    youngman Guest

    hi,

    here is my problem.

    Function t1(sita As Double, t0 As Double, l0 As Double) As Double
    sita = (sita - 90) / 180 * 3.14159265
    t1 = t0 - l0 * Tan(sita)
    End Function 'ok

    Function t2(sita As Double, t0 As Double, l0 As Double, l As Double) As
    Double
    t = t1(sita, t0, l0)
    MsgBox sita
    t2 = t + l * Tan(sita)
    End Function

    Sub test()
    MsgBox t1(95, 7.5, 20)
    MsgBox t2(95, 7.5, 20, 120)
    End Sub

    in MsgBox sita the sita is not 95, why.


    thank you.
     
    youngman, Jul 7, 2004
    #1
  2. youngman

    john m Guest

    not sure what you're trying to do but my calculator gets like .087

    i suggest you make another little function to do the degrees to radians or
    anyway separate that from whatever else you want to do to keep it clear

    i hope this helps

    jm
     
    john m, Jul 7, 2004
    #2
  3. youngman

    youngman Guest

    thanks.

    but,why it is 0.87,instead of 95.
    i didnt dim sita in public,

    thanks
     
    youngman, Jul 7, 2004
    #3
  4. youngman

    john m Guest

    hmmm
    i see what you are saying
    i guess if you were to use option explicit
    or else just use different variable names for sita in each function
    it might clear it up

    JM
     
    john m, Jul 7, 2004
    #4
  5. youngman

    wivory Guest

    I think you are getting confused because you are passing your function parameters by reference rather than by value. To pass by value, prefix the parameter(s) with the ByVal keyword in the Function statement. When you pass parameters by reference, any modifications you make to the passed value within the function gets passed back to the original variable (in fact technically you are dealing with the *same* original variable).

    So what is happening is from your test sub you are calling t2 with 95 passed in and assigned to the variable sita (note that because 95 is a literal, visual basic automatically makes this by-value). Then from within t2 you call t1 passing the variable sita which itself is assigned to a variable named sita (the fact that the names are the same is immaterial - it is the fact that it is passed by reference that is critical). Within t1 you modify the value of sita which simultaneously modifies the value of sita from t2 that you passed into t1. So by the time you exit the t1 function and display your message box within t2, the sita value has changed.

    I hope this makes sense.

    Regards

    Wayne Ivory
    IT Analyst Programmer
    Wespine Industries Pty Ltd
     
    wivory, Jul 7, 2004
    #5
  6. youngman

    john m Guest

    Thanks for setting me straight Wayne.

    JM

    parameters by reference rather than by value. To pass by value, prefix the
    parameter(s) with the ByVal keyword in the Function statement. When you
    pass parameters by reference, any modifications you make to the passed value
    within the function gets passed back to the original variable (in fact
    technically you are dealing with the *same* original variable).
    passed in and assigned to the variable sita (note that because 95 is a
    literal, visual basic automatically makes this by-value). Then from within
    t2 you call t1 passing the variable sita which itself is assigned to a
    variable named sita (the fact that the names are the same is immaterial - it
    is the fact that it is passed by reference that is critical). Within t1 you
    modify the value of sita which simultaneously modifies the value of sita
    from t2 that you passed into t1. So by the time you exit the t1 function
    and display your message box within t2, the sita value has changed.
     
    john m, Jul 7, 2004
    #6
Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.