Results 1 to 9 of 9

Thread: [RESOLVED] confusion abt function.

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2008
    Posts
    327

    Resolved [RESOLVED] confusion abt function.

    hi all,
    i am a little confused abt function and a public sub .please take a look at the below examples.
    vb Code:
    1. Private Sub Command_Click()
    2.   Call intO(5, 6)
    3.   Debug.Print intO
    4. End Sub
    5.  
    6. Public Function intO(intL As Integer, intX As Integer) As Integer
    7.     intO = intL + intX
    8.     Exit Function
    9. End Function
    here i get an error on debug.print intO although this can be over come by adding these lines
    vb Code:
    1. Private Sub Command_Click()
    2. dim intOut as integer
    3.  intOut = Call intO(5, 6)
    4.   Debug.Print intOut
    5. End Sub
    my question is i am trying to return the value from the public function . but i get errors.. i tried to use the return statement but still i get errors. although i have publicly declared intO as an integer then why do i have to assign it to a variable again?

  2. #2
    "Digital Revolution"
    Join Date
    Mar 2005
    Posts
    4,471

    Re: confusion abt function.

    When you put: Debug.Print intO you're calling the intO function again, and not giving it any parameters...

    Debug.Print intO(5, 6) would have worked...

    And: intOut = Call intO(5, 6) should be: intOut = intO(5, 6)

  3. #3
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: confusion abt function.

    It's not recommended to use hungarian notation on functions because it can lead to confusion with variables.

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2008
    Posts
    327

    Re: confusion abt function.

    hey.. everything was working well until i encountered another problem.

    i have a sub
    vb Code:
    1. private sub form(Load)
    2. call sortData("apple","ball","cat","dog")
    3. end sub
    4. public function sortData(strOne as string,strTwo as string, strThree as string,strFour as string)
    5. 'rest o'da codes here..
    6. end function

    but i cannot call sortData as it says sortData argument not optional.. i changed the function to a sub yet it don't work.. i am confused..
    i am in China and no body teaches me VB6

  5. #5
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: confusion abt function.

    The code you showed should not give that error, but this minor change would:
    Code:
    call sortData("apple","ball","cat")
    The error means that the sub/function needs a certain amount of arguments (aka parameters), and your call has less.

  6. #6
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: confusion abt function.

    Fewer!

    I've noticed that television seems to have collectively decided that fewer is no longer a word, and it drives me nuts. "More taste, less calories." Gah!

    Less of a whole
    Fewer in number

    For example, you could have less pie by having fewer slices of pie.

  7. #7
    New Member
    Join Date
    Jul 2009
    Posts
    7

    Re: confusion abt function.

    Now I'm confused. Although actually there is nothing wrong with calling a function that way you don't have to. You could use Fewer words (LOL) and just say

    sortData "apple","ball","cat","dog"

    However, normally if you aren't returning a value you would use a Sub not a function, and I'm confused about what the representative SortData function does when you pass it arguments and get no return value.

    And Si is correct, if you executed your code EXACTLY as you've shown it will work. You must have a typo either in the function declaration or in the call.

  8. #8
    PowerPoster Ellis Dee's Avatar
    Join Date
    Mar 2007
    Location
    New England
    Posts
    3,530

    Re: confusion abt function.

    I'm assuming it's just a made-up example to give us the basic idea, and that there is no SortData function in his project. Setting up a sort function where the inidividual elements get passed as parameters is bad enough, but then to not even use a parameter array? The mind boggles.

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2008
    Posts
    327

    Re: confusion abt function.

    Quote Originally Posted by Ellis Dee View Post
    I'm assuming it's just a made-up example to give us the basic idea, and that there is no SortData function in his project. Setting up a sort function where the inidividual elements get passed as parameters is bad enough, but then to not even use a parameter array? The mind boggles.
    yes, Ellis you are correct.. my Os is windows 7 and i was using a vd addin that would split long lines of codes to shorter lines to make it readable.. unfortunately a parameter was being deleted in the process and i kept on getting errors. now, its fine. thank you all.
    i am in China and no body teaches me VB6

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