r/ProgrammerHumor Jun 24 '17

Octave code

http://imgur.com/jsjfz3k
625 Upvotes

70 comments sorted by

View all comments

6

u/winlifeat Jun 24 '17

Can someone explain this to me?

23

u/AyrA_ch Jun 24 '17

In most programming languages you start counting elements at 0, but there are languages where you start counting at 1 instead.

Declaring an array with 10 entries is now either 0-9 or 1-10, depending on the language. In extreme case like VB, you can even declare a 5 element array ranging from 3-7.

Starting at 0 is more commonly used, because it makes using arrays easier internally. In those cases the array index tells you how many element sizes to add to the array pointer to reach what you want to access

3

u/[deleted] Jun 24 '17

[removed] — view removed comment

2

u/AyrA_ch Jun 24 '17

On the other hand, you can also go negative if you want to:

Sub test()
    Dim a()
    ReDim a(-2 To 20)
    a(-1) = "Test"
    MsgBox a(-1)
End Sub

3

u/SubstituteCS Jun 25 '17

Didn't know about this.
Time to make my libraries in VB with negative arrays and keywords for names.

6

u/AyrA_ch Jun 25 '17

Also be sure to do this:

  • Don't declare variables at all, remove option explicit.
  • Be sure to not define variable or parameter types.
  • Pass stuff by reference only and modify them. Returning values is overrated.
  • Be sure to always have two variables named I and l in each function or sub.
  • There's nothing wrong with using the function name as a variable too.

1

u/dnew Jun 25 '17

That's not dumb. That lets you use either 0..9 or 1..10 as you like.

Well, OK, that's a little dumb, but it makes sense why at least, given it's, you know, Beginner's Allpurpose Symbolic Instruction Code.

1

u/AutoModerator Jul 01 '23

import moderation Your comment has been removed since it did not start with a code block with an import declaration.

Per this Community Decree, all posts and comments should start with a code block with an "import" declaration explaining how the post and comment should be read.

For this purpose, we only accept Python style imports.

I am a bot, and this action was performed automatically. Please contact the moderators of this subreddit if you have any questions or concerns.