Author Topic: INC Function  (Read 2699 times)

0 Members and 1 Guest are viewing this topic.

Offline Taylorover9001

  • Newbie
  • Posts: 6
    • View Profile
INC Function
« on: February 21, 2020, 08:29:31 pm »
I would love to have a function that can change the value of a variable by an amount. Yes, I can and have created my own Sub to do this in some programs, but I would love for this to be implemented as a QB64 function, especially considering how many variable types exist. It would certainly be more convenient, by only having the variable/array once, instead of twice. If a change needs to be made, it only needs to be made once, not to mention code being shorter. Another language I've used (SmileBASIC) does have such a function, and QB64 also has the SWAP function, so I don't see why QB64 couldn't have an INC function.


Syntax:
_INC VAR[, STEP]
VAR = Variable to increment.
STEP = Amount to increment by. If omitted, 1 will be used.

Example:
X = 7
PRINT X
_INC X, -3
PRINT X
_INC X
PRINT X
----------------------
7
4
5


You can probably see how that would be more convenient, by having _INC X instead of X = X + 1. Or _INC LongAFVariableName(3, 1, 2, 5) instead of LongAFVariableName(3, 1, 2, 5) = LongAFVariableName(3, 1, 2, 5) + 1.
« Last Edit: February 21, 2020, 08:34:07 pm by Taylorover9001 »

Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: INC Function
« Reply #1 on: February 21, 2020, 08:33:46 pm »
2 questions:

1) How is _INC X, -3 any better than X = X -3?

2) Why not just write your own sub for this?


x = 7
INC x, -3
PRINT x
SUB INC (x, step)
    x = x + step
END SUB
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Taylorover9001

  • Newbie
  • Posts: 6
    • View Profile
Re: INC Function
« Reply #2 on: February 21, 2020, 08:43:16 pm »
1) By making code shorter (eliminating redundant code), making it less likely for bugs to be created, and easier to fix bugs without creating new bugs. For example, this could happen, either creating new code or trying to fix a bug: X(1,4) = X(1,3) + 1. It could take awhile for such a bug to even be found, depending on the circumstances. With _INC, you just have _INC X(1,4). Sure if you mess that up, it could still take awhile to find, but I think it would be less likely to mess up, especially when modifying code.

2) I have. It's at the top of the post. But different variable types can cause problems, unless I create a Sub for every variable type in the program.

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: INC Function
« Reply #3 on: February 21, 2020, 11:34:27 pm »
While I see your point of view on this, especially when adding 1 to a long variable name, I agree with Steve. A simple subroutine added to your .BM toolbox library would be more than sufficient for this.

Personally, and this is my personal opinion, adding commands that mimic other languages may sound good in the short term but can muddy the waters. Imagine being able to do this:

variable++

That would look just all kinds of crazy intermixed within BASIC code.
In order to understand recursion, one must first understand recursion.

Offline TerryRitchie

  • Seasoned Forum Regular
  • Posts: 495
  • Semper Fidelis
    • View Profile
Re: INC Function
« Reply #4 on: February 21, 2020, 11:36:37 pm »
I do however like that a new forum member is coming in with ideas on how to improve QB64. Thank you and welcome :-)
In order to understand recursion, one must first understand recursion.

Offline TempodiBasic

  • Forum Resident
  • Posts: 1792
    • View Profile
Re: INC Function
« Reply #5 on: February 22, 2020, 01:14:24 pm »
Hi  Taylorover9001
welcome to QB64 forum.

about
Quote
I would love to have a function that can change the value of a variable by an amount.
I think that the most of us QB64 coders for  a short time have thought the same thing... why not to have a more realable function at the place of write extensively the variation of the value of a variable (Var = var +/- Amount)

but  do why we have to mimic other languages?
However some dialect of Basic have a similar function... for example TurboBasic 1.0 has these two functions
Quote
DECR statement
  DECR decrements a variable.
DECR variable [, size]
variable is a numeric variable, and size is an optional numeric
expression that indicates the value to be subtracted from variable.
If size is omitted, 1 is used.
DECR is a handy way to decrement a variable. Use the INCR
statement to increment a variable


Quote
INCR statement

INCR increments a variable.
INCR numeric variable [,size]
size is an optional numeric expression that indicates the value to be
added to the specified variable. If size is omitted, 1 is used.
INCR is simply a quick way to increment a variable without using
an assignment statement.
This statement is not available in Interpretive BASIC.

I don't know if also PureBasic or Liberty Basic or Freebasic have these (this?) functions .
Maybe Pascal or TurboPascal have DEC(var) and INC(var) my memory is not so strong!
And what say about C/C++ with its -- ++, hey and Java what has as short form of increasing /decreasing the variable?

However original BASIC of Dartmouth has not this issue!
Never forget our root! :-))
« Last Edit: February 22, 2020, 01:15:38 pm by TempodiBasic »
Programming isn't difficult, only it's  consuming time and coffee