Author Topic: I think to get a bug with passing for value to a function or sub  (Read 3328 times)

0 Members and 1 Guest are viewing this topic.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Hi
while I was attempting to solve a Rosetta Code challange (task)
I fall on this issue...
please copy paste run this code
Code: QB64: [Select]
  1. Print " Demo with number"
  2. a = 8
  3.  
  4. Half ((a)) ' here it doesn't modify a
  5. Print a, Half(a) 'expected   8 , 4
  6. Half (a) 'here it modifies a
  7. Print a, Half(a) ' expected 4 , 2
  8.  
  9.  
  10. Print " Demo with string"
  11. b = "I Love QB64!" ' 12 characters
  12.  
  13. HalfString ((b)) ' this doesn't modify b
  14. Print "--"; b;
  15. HalfString (b) ' this modify b
  16. Print "--"; b
  17.  
  18. Sub Half (num As Integer)
  19.     num = num / 2
  20.  
  21. Function Half (num As Integer)
  22.     Half = num / 2
  23.  
  24. Sub HalfString (stri As String)
  25.     stri = Left$(stri, Int(Len(stri) / 2))

As you can see from my comments after passing a number for value I got no modification of it also in the following normal calling of SUB.
Moreover using a string variable and calling the SUB that chunks it at middle the variable has been modified also if passed fpr value.

Your feedbacks.
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I think to get a bug with passing for value to a function or sub
« Reply #1 on: April 08, 2021, 10:21:28 pm »
Don't know why string arguments enclosed with () are being modified in main code, my understanding is that is passing ByVal so they wouldn't be altered in main code.

Really confusing to have a SUB and Function by same name so I changed case on H for Sub, IDE shows difference.
Code: QB64: [Select]
  1. Print " Demo with number"
  2. a = 8
  3.  
  4. half ((a)) ' here it doesn't modify a
  5. Print a, Half(a) 'expected   8 , 4
  6. half a 'here it >>>> really <<<< modifies a
  7. Print a, Half(a) ' expected 4 , 2
  8.  
  9.  
  10. Print " Demo with string"
  11. b = "I Love QB64!" ' 12 characters
  12.  
  13. HalfString ((b)) ' this modify b >>> it shouldn't but does apparently
  14. Print "-"; b
  15. HalfString (b) ' this modify b  >>> don't know why ?
  16. Print "--"; b
  17.  
  18. Sub half (num As Integer)
  19.     num = num / 2
  20.  
  21. Function Half (num As Integer)
  22.     Half = num / 2
  23.  
  24. Sub HalfString (stri As String)
  25.     stri = Left$(stri, Int(Len(stri) / 2))
  26.  
  27.  
« Last Edit: April 08, 2021, 10:25:06 pm by bplus »

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I think to get a bug with passing for value to a function or sub
« Reply #2 on: April 09, 2021, 01:00:23 am »
Holy crap! Even this is changing b$ !!!
Code: QB64: [Select]
  1. Print " Demo with string"
  2. b$ = "I Love QB64!" ' 12 characters
  3.  
  4. HalfString ((b$ + "")) ' this modify b >>> it shouldn't but does apparently
  5. Print "-"; b$
  6. HalfString b$ + "" ' this modify b  >>> don't know why ?
  7. Print "--"; b$
  8.  
  9. Sub HalfString (stri As String)
  10.     stri = Left$(stri, Int(Len(stri) / 2))
  11.  
  12.  

This is what is expected:
Code: QB64: [Select]
  1. Print " Demo with string"
  2. b$ = "I Love QB64!" ' 12 characters
  3.  
  4. HalfString ((b$ + " ")) '
  5. Print "-"; b$
  6. HalfString b$ + " "
  7. Print "--"; b$
  8.  
  9.  
  10. Sub HalfString (stri As String)
  11.     stri = Left$(stri, Int(Len(stri) / 2))
« Last Edit: April 09, 2021, 01:02:22 am by bplus »

Offline RhoSigma

  • QB64 Developer
  • Forum Resident
  • Posts: 565
    • View Profile
Re: I think to get a bug with passing for value to a function or sub
« Reply #3 on: April 09, 2021, 02:15:35 am »
I think, strings and entire arrays are always passed by reference no matter what.

It seems logic to me, think of passing a big string or array, maybe with 1000s of chars or elements, by value? - Wouldn't it in worst case not simply blow up the stack (stackoverflow), especially if done so in many nested SUB/FUNCs or even in a recursivly called function?

I've no idea how much stack space is available for a program assigned by the OS at startup time of a program, nor I know if there is some mechanism which can automatically increase the stack size at runtime if required.
My Projects:   https://qb64forum.alephc.xyz/index.php?topic=809
GuiTools - A graphic UI framework (can do multiple UI forms/windows in one program)
Libraries - ImageProcess, StringBuffers (virt. files), MD5/SHA2-Hash, LZW etc.
Bonus - Blankers, QB64/Notepad++ setup pack

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I think to get a bug with passing for value to a function or sub
« Reply #4 on: April 09, 2021, 02:28:32 am »
Yeah, I did not find anywhere in Wiki any notes where wrapping in () forces ByVal, and yes strings could take up allot of storage specially in recursion. So maybe it works only on numbers.

But I was surprised I could not force ByVal by adding a Null to the string in the call to sub.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: I think to get a bug with passing for value to a function or sub
« Reply #5 on: April 09, 2021, 04:12:57 am »
Hi
I'm sure and agree about not so good practice to try to pass a string as value and not as reference.
If we see at wiki page of ByVal it says that STRING must be passed as reference.

About recursion and stack issue I partially agree because it is real the limit of the stack of memory, but the same is for dynamic arrays
in this case the coder must set a limit to the expansion of the size of the memory used for the task.

However I have used so little times the passing by value and now I'm discovering how QB64 manages it.
And my philosophycal question is: is it the same in QB45?
As soon I have just a little time I'll test and I'll report here the results.

Thanks for feedbacks

PS How to find the () argument about passing by value in the wiki? I am not so clever with setting search in database!
Programming isn't difficult, only it's  consuming time and coffee

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: I think to get a bug with passing for value to a function or sub
« Reply #6 on: April 09, 2021, 02:51:07 pm »
Hi
I'm sure and agree about not so good practice to try to pass a string as value and not as reference.
If we see at wiki page of ByVal it says that STRING must be passed as reference.

About recursion and stack issue I partially agree because it is real the limit of the stack of memory, but the same is for dynamic arrays
in this case the coder must set a limit to the expansion of the size of the memory used for the task.

However I have used so little times the passing by value and now I'm discovering how QB64 manages it.
And my philosophycal question is: is it the same in QB45?
As soon I have just a little time I'll test and I'll report here the results.

Thanks for feedbacks

PS How to find the () argument about passing by value in the wiki? I am not so clever with setting search in database!

ByVal, the only reference I found in Wiki for ByVal concerned DECLARE LIBARY, using library, possible from another PL. I scoured Sub and Function and extra article on Subs for the mention of () wrap to pass ByVal instead of By Reference, nothing!

As I recall QB4.5 was ByVal because I remember being surprised to learn QB64 passed everything By Reference by default. In other Basic's I learned to pass arrays by reference to save stack or storage or processing time as ByVal passing was default for them.

So question remains when/where I learned trick of using () to pass numbers (only) by value. Don't think I'd even recommend doing that. I recommend making copy of passed variable if you intend on manipulating the value in the procedure and want it the same when exit procedure.


Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: I think to get a bug with passing for value to a function or sub
« Reply #7 on: April 10, 2021, 05:37:59 pm »
Hi
I have done some attempts in QB under Dosbox!
So I have discovered this: https://www.qb64.org/forum/index.php?topic=3798.0;topicseen in QB45 a function and a sub cannot have the same name, in QB64 it is possible!

And more and more
the same code  with those little adjustments  (declaration of model of SUB FUNCTION at the beginning of the code, different names for SUB and Function) gives these results...
  [ You are not allowed to view this attachment ]  
so with number seems that QB64 follows QB45 exactly  both passing the variable for value both for reference ,
instead with the string variable Qb64 acts in opposite way in respect of QB45, in fact this last doesn't change the string variable  whiile QB64  modifies the string variable.

Your thinkings?
« Last Edit: April 11, 2021, 05:03:09 am by TempodiBasic »
Programming isn't difficult, only it's  consuming time and coffee

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: I think to get a bug with passing for value to a function or sub
« Reply #8 on: April 10, 2021, 10:17:48 pm »
I think, strings and entire arrays are always passed by reference no matter what.

It seems logic to me, think of passing a big string or array, maybe with 1000s of chars or elements, by value? - Wouldn't it in worst case not simply blow up the stack (stackoverflow), especially if done so in many nested SUB/FUNCs or even in a recursivly called function?

I've no idea how much stack space is available for a program assigned by the OS at startup time of a program, nor I know if there is some mechanism which can automatically increase the stack size at runtime if required.

QBasic could set the stack space using: CLEAR , , stack

I recall needing CLEAR , , 2000 to get my office program to run without an out of stack space error.

Pete
Want to learn how to write code on cave walls? https://www.tapatalk.com/groups/qbasic/qbasic-f1/