Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4346720
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T12:12:38+00:00 2026-05-21T12:12:38+00:00

I have an array witch I pass to a function by reference to sort

  • 0

I have an array witch I pass to a function by reference to sort it. However, seems like the array is passed byval. Can anyone solve what’s the problem? (Also sort workarounds accepted)


1) The script below passes an array by-reference to the sort function.

2) Sort function outputs the sorted array values.

3) The script outputs the sorted array values. However they are not sorted.


The script outputs:

300,200,100,,

100,200,300,


'declare variables
mitta(1) = 1
mitta(2) = 2
mitta(3) = 3

sort(mitta)  ' see the function below

' show variables
For i = 1 To 3
    response.write mitta(i) & ","
next

' sort function
function sort(byref a)
    dim num,i,j,temp
    num = ubound(a)+1
    For i = 0 To num - 1
       For j = i + 1 To num - 1
          If a(i) < a(j) Then
             temp = a(i)
             a(i) = a(j)
             a(j) = temp
          End If
       Next
    Next

    ' show sorted variables
    For i = 0 To num - 1
        response.write a(i) & ","
        a(i) = 0
    next
end function
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T12:12:39+00:00Added an answer on May 21, 2026 at 12:12 pm

    By wrapping mitta in parentheses in the function call sort(mitta), you’re actually passing it by value, despite the function declaration. From http://blogs.msdn.com/b/ericlippert/archive/2003/09/15/52996.aspx:

    The rules are

    3.1) An argument list for a function call with an assignment to the
    returned value must be surrounded by
    parens: Result = MyFunc(MyArg)
    3.2) An argument list for a subroutine call (or a function call with no
    assignment) that uses the Call keyword
    must be surrounded by parens: Call
    MySub(MyArg)
    3.3) If 3.1 and 3.2 do not apply then the list must NOT be surrounded by
    parens.

    And finally there is the byref rule:
    arguments are passed byref when
    possible but if there are “extra”
    parens around a variable then the
    variable is passed byval, not byref.

    Now it should be clear why the
    statement MySub(MyArg) is legal but
    MyOtherSub(MyArg1, MyArg2) is not. The
    first case appears to be a subroutine
    call with parens around the argument
    list, but that would violate rule 3.3.
    Then why is it legal? In fact it is a
    subroutine call with no parens around
    the arg list, but parens around the
    first argument!
    This passes the
    argument by value. The second case is
    a clear violation of rule 3.3, and
    there is no way to make it legal, so
    we give an error.

    See also the MSDN reference for ByRef and ByVal Parameters. Instead, you should call sort either with:

    sort mitta
    

    or:

    Call sort(mitta)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a JSON array with ActiveRecord objects. These objects can be reconstructed using
I have a simple function in which an array is declared with size depending
I have this function array_diff(\@DNs, \@prev_DNs); which must take array references as arguments. The
In my program I have one array with 25 double values 0.04 When I
I have a column array with the following values in my sheet: 11, 15,
What is the best way to have an associative array with arbitrary value types
Say I have an array of strings in a php array called $foo with
I often have a subroutine in Perl that fills an array with some information.
I have an array of 1000 or so entries, with examples below: wickedweather liquidweather
I have a sparse array in Jscript, with non-null elements occuring at both negative

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.