You are here:

Qbasic, Quickbasic/Passing string array into function/sub

Advertisement


Question
QUESTION: This is really a FreeBasic issue as I've moved from QBasic to take advantage of the extra it's possible to get in FreeBasic. I've been programming in Basic for around 40 years but I'm having a problem that I just can't solve. More or less ever since it was invented!)

As GOSUB is deprecated in Freebasic (unless I use -QB switch) it appears I need to use Sub.... End Sub. However, the purpose of the routine is to rearrange the items in a string array. It seems I need to pass the array in the parameter list to the subroutine but whatever I do to try and pass it I can't get it to work.

I have to do it "ByRef" not "ByVal" as I need the altered array to be processed elsewhere in my program. Any thopughts?

ANSWER: Hi Phil,

I'd have to see your code to really understand the problem, but until then, I can tell you how I would do it.  This is more C-style than Basic, but it does the job, and fairly quickly:

'' Note: first parameter is an "Integer Ptr"
'' If your array is of a different type, the first parameter type must be [ThatType] Ptr
Sub ReArrangeArray( ByVal myIntArray As Integer Ptr, ByVal myIntArraySize As Integer )

  '' In here, we can access the array by myIntArray[0] to myIntArray[myIntArraySize-1]
  '' If this is a regular integer array, you can get the value with:
  ''  Print myIntArray[0]
  '' And to set the value:
  ''  myIntArray[0] = 100
  '' And to swap the value from two elements:
  ''  Swap myIntArray[0], myIntArray[1]

End Sub

Dim test(5 To 100) As Integer

'' Notice the @ symbol, which gets the memory address of the variable, which is essentially what a pointer is.
ReArrangeArray( @test(LBound(test)), UBound(test) - LBound(test) )


There is no downside to using this over passing something like:

SUB ReArrange( ByRef myIntArray() As Integer )
 '' ...
END Sub


I came from a C background, so I'm just more comfortable working with variable pointers rather than byref'ing a variable.

If you can show me your code that doesn't work as expected, I can see if I can spot any issue, too :)  And, if you have any questions about my code that I may have been a little vague on, ask away.

Take care,
-Alex

---------- FOLLOW-UP ----------

QUESTION: Unfortunately, it works with integer values doing something like you proposed but not with string arrays.

However, I tried it by passing the array as "array() as string" without using ByRef or ByVal and that seems to have worked! It shouldn't do - but it does. As the array has a possible 50,000 items in it I really thought I was stuck. Doubtless it will fail in some future version of the compiler.

Thanks for your thoughts.

Answer
Hi Phil,

Now that I go over the documentation again, passing it without byref or byval is correct, and the behaviour you get is expected.  There used to be a wiki page that explains this but it seems to have been removed.

I don't think this behaviour will change in the near future, so you should be alright.

Take care,
-Alex

Qbasic, Quickbasic

All Answers


Ask Experts

Volunteer


Alex Barry

Expertise

I have been a qbasic programmer since 2000, creating games, minor libraries and various small programs. I have experience using interrupts, graphics, file input/output, the mouse cursor, and using libraries. I have also learned FreeBASIC, QB64, c/c++, python, lua, php and html.
I do not claim to be an absolute authority in any language, but I don't mind looking things up and learning with you.

Experience

I have been programming in *Basic dialects since 2000, as mentioned in my expertise. After a year of QBasic, I learned C and C++, and dabbled a little in ASM (I don't program in ASM - I literally just played around to see how things work). When QB64 and FreeBASIC were released, I played with those languages. At the time, FreeBASIC offered more functionality and I sided with that language for a while. During that time, while I was learning new languages, that I would see what scripting languages are available, where I took up python and lua. I started to notice a staleness to QB64's development (which I kept tabs on from time to time), and am now trying to be active in it's community and maybe in it's development in the future. Currently, I am only active on the QB64.net forums, but I appear on occasion on FreeBASIC.net's forums as well.

Education/Credentials
Highschool - 2007

©2016 About.com. All rights reserved.