CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Apr 2005
    Posts
    159

    Can an array be passed to a Function as parameter..........

    I am using the following code
    Please tell me whats wrong here in.
    nd any alternative for it.

    Basically i want to get max no of an array through a function.
    nd i m passing an array to the function "Max".
    it gives me error "Subscript out of range"
    can i pass an array as Argument(Parameter) to a function.
    If yes then How?



    Code:
    Dim ar() As Integer
    Private Sub Command1_Click()
    Dim i As Integer ''Holds array length
    i = InputBox("Enter Total elements")
    For j = 0 To i
    	ReDim Preserve ar(j)
    	ar(j) = InputBox("Enter the Nos to GetMax Value Of")
    Next
    MsgBox max(ar())
    End Sub
     
    
    Public Function max(ParamArray a()) As Integer
    Dim temp As Integer
    For b = 0 To UBound(a())
    	If a(b) > a(b + 1) Then			  <==Error Here
    		temp = a(b)
    	Else
    		temp = a(b + 1)
    	End If
    Next
    max = temp
    End Function
    Thanks in Adv.

  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: Can an array be passed to a Function as parameter..........

    A ParamArray is used to define a unknown amount of parameters, not to pass a parameter.

    The declaration of the function should be this:
    Code:
    Public Function max(a() as Integer) As Integer
    Tom Cannaerts
    email: cakkie@cakkie.be.remove.this.bit
    www.tom.be (dutch site)

  3. #3
    Join Date
    Dec 2002
    Location
    London, UK
    Posts
    1,569

    Re: Can an array be passed to a Function as parameter..........

    But, note that the type of the array does have to be defined.
    Mike

  4. #4
    Join Date
    Jul 2005
    Posts
    13

    Re: Can an array be passed to a Function as parameter..........

    in the function you have defined, how can the function access an element that has an index greater than that of the array.
    for example, yuo have an array of size 9(0-8). now b is going to be 0 to 8, so no problem abt a(b), but wht abt a(b+1), it will finally be a(9),got it.
    try to use the upper limit of the for loop in the function as -
    ubound(a())-1

    another way is to define the array as variant data type, but you have specified that you are going to mreturn the max. value in the array to the user, obviously the array won't contain struings, it may contain integers or floats or in the best case longs.

    i hope that helps...
    reply.

  5. #5
    Join Date
    Apr 2005
    Posts
    159

    Re: Can an array be passed to a Function as parameter..........

    Thanks for ur comments.
    ofcourse the problem is same as u told but it always takes UBound=0.
    nd when a(b+1) executes it generates error coz it becomes a(1).

    The code is now working with some changes

    Code:
     
    Dim ar() As Integer
    
    Private Sub Command1_Click()
    		 Dim i As Integer ''Holds array length
    		 i = InputBox("Enter Total elements")
    		 For j = 0 To i
    			  ReDim Preserve ar(j)
    			  ar(j) = InputBox("Enter the Nos to GetMax Value Of")
    		 Next
    		 MsgBox max(ar())
    End Sub
     
    Public Function max(a() As Integer) As Integer
    				  Dim temp As Integer
    				  temp = a(0)
    				  For b = 1 To UBound(a())
    					   If temp < a(b) Then
    							 temp = a(b)
    					   End If
    				  Next
    				  max = temp
    End Function

    This was nice to see such usefull information.
    With lots of apreciation nd thanks.

    Take Care all of u.

    Silly Star




    Quote Originally Posted by pangolin_10
    in the function you have defined, how can the function access an element that has an index greater than that of the array.
    for example, yuo have an array of size 9(0-8). now b is going to be 0 to 8, so no problem abt a(b), but wht abt a(b+1), it will finally be a(9),got it.
    try to use the upper limit of the for loop in the function as -
    ubound(a())-1

    another way is to define the array as variant data type, but you have specified that you are going to mreturn the max. value in the array to the user, obviously the array won't contain struings, it may contain integers or floats or in the best case longs.

    i hope that helps...
    reply.
    Last edited by Silly Star; August 18th, 2005 at 11:30 PM.

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

Featured