Author Topic: simple SUB-procedure doesn't work  (Read 6370 times)

0 Members and 1 Guest are viewing this topic.

Offline MrFreyer

  • Newbie
  • Posts: 34
    • View Profile
simple SUB-procedure doesn't work
« on: February 22, 2018, 08:56:42 am »
Hi all,

anybody knows why this subprocedure doesn't work?

Code: QB64: [Select]
  1.  
  2.  
  3.  
  4. var1 = 1
  5. var2 = 2
  6. var3 = 3
  7.  
  8. sum1 = 0
  9.  
  10. PRINT "var1:"; var1; " =1"
  11. PRINT "var2:"; var2; " =2"
  12. PRINT "var3:"; var3; " =3"
  13. PRINT "sum1:"; sum1; " =0"
  14.  
  15. CALL calc(var1, var2, var3, sum1)
  16.  
  17. PRINT "var1:"; var1; " =1"
  18. PRINT "var2:"; var2; " =2"
  19. PRINT "var3:"; var3; " =3"
  20. PRINT "sum1:"; sum1; " =6 ?"
  21.  
  22.  
  23. SUB calc (subvar1, subvar2, subvar3, subsum1)
  24. subsum1 = subvar1 + subvar2 + subvar3
  25.  

I have written much more complex subprocedures, and (it seems like) they worked.

Thanks

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: simple SUB-procedure doesn't work
« Reply #1 on: February 22, 2018, 10:04:45 am »
Code: QB64: [Select]
  1. DEFINT A-Z   '<<<<<<<<<<<<<<<< added this
  2.  
  3.  
  4. var1 = 1
  5. var2 = 2
  6. var3 = 3
  7.  
  8. sum1 = 0
  9.  
  10. PRINT "var1:"; var1; " =1"
  11. PRINT "var2:"; var2; " =2"
  12. PRINT "var3:"; var3; " =3"
  13. PRINT "sum1:"; sum1; " =0"
  14.  
  15. calc var1, var2, var3, sum1 '<<<< removed call and ()
  16. PRINT "Outside sub now in Main:"
  17. PRINT "var1:"; var1; " =1"
  18. PRINT "var2:"; var2; " =2"
  19. PRINT "var3:"; var3; " =3"
  20. PRINT "sum1:"; sum1; " =6 ?"
  21.  
  22. PRINT "subvar1:"; subvar1; " =1"
  23. PRINT "subvar2:"; subvar2; " =2"
  24. PRINT "subvar3:"; subvar3; " =3"
  25. PRINT "subsum1:"; subsum1; " =6 ?"
  26.  
  27.  
  28.  
  29.  
  30. SUB calc (subvar1, subvar2, subvar3, subsum1)
  31.     PRINT
  32.     PRINT "Inside calc now recieved:"
  33.     PRINT "subvar1:"; subvar1; " =1"
  34.     PRINT "subvar2:"; subvar2; " =2"
  35.     PRINT "subvar3:"; subvar3; " =3"
  36.     PRINT "subsum1:"; subsum1; " =6 ?"
  37.  
  38.     subsum1 = subvar1 + subvar2 + subvar3
  39.     PRINT
  40.     PRINT "Inside calc now calced:"
  41.     PRINT "subvar1:"; subvar1; " =1"
  42.     PRINT "subvar2:"; subvar2; " =2"
  43.     PRINT "subvar3:"; subvar3; " =3"
  44.     PRINT "subsum1:"; subsum1; " =6 ?"
  45.  
  46.  
  47.  
  48.  

But now it does work with CALL and () as well?!?! curious indeed!
« Last Edit: February 22, 2018, 10:11:00 am by bplus »

FellippeHeitor

  • Guest
Re: simple SUB-procedure doesn't work
« Reply #2 on: February 22, 2018, 10:09:09 am »
@MrFreyer:

You can only change a parameter if it matches types. In your case subsum1 is an INTEGER, but when you create the SUB, subsum1 is a SINGLE precision parameter.

The solution would be:
Code: QB64: [Select]
  1. SUB calc (subvar1, subvar2, subvar3, subsum1 AS INTEGER)

If you want to use the variable you had already DIM SHARED before, don't create a new variable with the same name for a parameter:

Code: QB64: [Select]
  1. SUB calc

After all, if all your variables are already SHARED, you don't need to pass them to the SUB anyway.

Now, it seems like what you actually want is a function. A function is a SUB too, but it returns a value:

Code: QB64: [Select]
  1. FUNCTION calc(value1, value2, value3)
  2.     calc = value1 + value2 + value3

This way you invoke it like this:

Code: QB64: [Select]
  1. result = calc(subvar1, subvar2, subvar3)
  2. 'or even
  3. result = calc(1, 2, 3)

Offline MrFreyer

  • Newbie
  • Posts: 34
    • View Profile
Re: simple SUB-procedure doesn't work
« Reply #3 on: February 22, 2018, 10:18:49 am »
Okay, now I get it.

Yes, I could do this as a function, but I was testing something other and just posted this selected piece of code.

Thanks!

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: simple SUB-procedure doesn't work
« Reply #4 on: February 22, 2018, 10:28:58 am »
OK he gets it...  but now I am confused.

subsum1 was dimensioned as an integer!

and code does work with DEFINT A-Z



Not that I would actually do this crazy stuff in code, I can find my own just fine!
« Last Edit: February 22, 2018, 10:54:21 am by bplus »

Offline MrFreyer

  • Newbie
  • Posts: 34
    • View Profile
Re: simple SUB-procedure doesn't work
« Reply #5 on: February 22, 2018, 10:40:37 am »
The SUB calc redimensioned the subsum1 as SINGLE (for the use in the subprocedure).
But if you use DEFINT A-Z, the subsum1 will automatically redimensioned as a INTEGER .

The variables subvar1, subvar2 and subvar3 will also redimensined as SINGLE, but they don't need to be shared with the maincode.
And that's the problem. subsum1 will be shared with the maincode, but a SINGLE (subsum1) and a INTEGER (sum1) dosn't fit together.

Am I right?
« Last Edit: February 22, 2018, 10:48:38 am by MrFreyer »

FellippeHeitor

  • Guest
Re: simple SUB-procedure doesn't work
« Reply #6 on: February 22, 2018, 10:40:57 am »
Scope is the question here. When you DIM SHARED subvar1 AS INTEGER, you make it available globally. If you create any sub or function, you can access subvar1 from there, change it, erase it, redimension it (if it's an array). However, if you instruct QB64 that a SUB/FUNCTION needs to have a parameter that's also named subvar1, QB64 will assume that for this new scope you want a subvar1 that's not an INTEGER (remember that not defining a type makes a variable of type SINGLE). The local subvar1 parameter takes precedence over the shared variable. They are separate entities.

Internally (in the C++ output) you'll have __INTEGER_SUBVAR1 and _SUB_CALC_SINGLE_SUBVAR1, which can be verified by looking in internal/temp/main.txt.
« Last Edit: February 22, 2018, 10:46:29 am by FellippeHeitor »

Offline Pete

  • Forum Resident
  • Posts: 2361
  • Cuz I sez so, varmint!
    • View Profile
Re: simple SUB-procedure doesn't work
« Reply #7 on: February 22, 2018, 12:23:48 pm »
A long time ago in an IDE far far away, we used COMMON SHARED instead of DIM SHARED.

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

Offline Edster

  • Newbie
  • Posts: 2
    • View Profile
Re: simple SUB-procedure doesn't work
« Reply #8 on: February 24, 2018, 07:05:21 am »
I have a similar question so I thought I would tack it to the end of this thread.

DIM reply(i) AS STRING
CALL Answer(reply())
.
.
.
SUB Answer(passedReply() AS STRING)
    PRINT passedReply(i)
END SUB

The above code works just fine ...

DIM reply(i) AS STRING
PRINT Answer(reply())
.
.
.
FUNCTION Answer(passedReply() AS STRING)
    Answer = passedReply(i)
END FUNCTION

This code produces IILEGAL STRING-NUMBER CONVERSION error

However, if 'DEFSTR A' is used, then the above function works fine. The function name needs to be defined as string.

Should FUNCTIONS and SUBS differ in this way? It took me some time to figure out why the FUNCTION wouldn't work.
« Last Edit: February 24, 2018, 07:27:16 am by Edster »

FellippeHeitor

  • Guest
Re: simple SUB-procedure doesn't work
« Reply #9 on: February 24, 2018, 07:32:29 am »
Now that could be a bug. Give us some time to look into to it.

Edit:
No, not a bug. You're trying to return a string but your function is of type SINGLE. Fix it like this:

Code: QB64: [Select]
  1. FUNCTION Answer$(passedReply() AS STRING)
  2.     Answer$ = passedReply(i)
« Last Edit: February 24, 2018, 07:45:11 am by FellippeHeitor »

Offline Petr

  • Forum Resident
  • Posts: 1720
  • The best code is the DNA of the hops.
    • View Profile
Re: simple SUB-procedure doesn't work
« Reply #10 on: February 24, 2018, 07:45:33 am »
Try using DIM SHARED

Offline Edster

  • Newbie
  • Posts: 2
    • View Profile
Re: simple SUB-procedure doesn't work
« Reply #11 on: February 24, 2018, 08:16:55 am »
Now that could be a bug. Give us some time to look into to it.

Edit:
No, not a bug. You're trying to return a string but your function is of type SINGLE. Fix it like this:

Code: QB64: [Select]
  1. FUNCTION Answer$(passedReply() AS STRING)
  2.     Answer$ = passedReply(i)

My bad, if I taken a closer look on wiki then I'd have seen the need for a type suffix for functions.

Thanks for pointing that out to me.


FellippeHeitor

  • Guest
Re: simple SUB-procedure doesn't work
« Reply #12 on: February 24, 2018, 08:52:14 am »
My pleasure. Welcome to the forum!