none
Subroutine parameters/local variables

    General discussion

  • I know this has been asked and suggested before but there hasn't been any new post since 2009 about it. I also know there has been some strong opinions against this. However I believe there is a way to implement this and leave the current simplicity of Small Basic as is. I would suggest prefixing local variables and parameters with a special character (maybe a dot). This way global variables would still be used the same way as before and students wouldn't have to know about scope if you don't want to go into it (no pun intended). Here's an example:

    Sub Func(x)

    .y = .x * 2 ' define new local variable and use parameter

    y = 20 ' define global variable

    .Func = .y * 3 ' define output of function

    EndSub

    Tuesday, February 14, 2017 11:17 AM

All replies

  • Grega,

    Good suggestion. I don't believe that Microsoft would want to implement anytime soon though. There is a way to implement something similar in the LitDev extension. I don't know if you know that it is there. It is LDCall.Function if you have the extension loaded. He also has a sample program in Other-Samples. Look for LDCall.sb.


    JR

    Tuesday, February 14, 2017 12:58 PM
    Answerer
  • Yes, I know about it. Thanks for the suggestion. LDCall.Funcion is a solution but not a good one. If you're trying to teach kids about DRY principle and have to do all the ceremony around LDCall.Function you kind of missed the point.
    Wednesday, February 15, 2017 7:26 AM
  • If you're trying to teach kids about DRY principle...

    ...then Small Basic is probably not the right language for you.

    Small Basic works to teach computational thinking and logic, but it isn't suited to "proper" (for want of a better term) programs. If you're getting into software engineering methodology, you probably need to move to a more advanced language with more advanced tools.

    As an aside, I'm not certain you've got DRY right.It doesn't mean "eliminate duplicate code" and shouldn't be interpreted literally:

    Bill Venners: What's the DRY principle?
    Dave Thomas: Don't Repeat Yourself (or DRY) is probably one of the most misunderstood parts of the book.
    Bill Venners: How is DRY misunderstood and what is the correct way to understand it?
    Dave Thomas:Most people take DRY to mean you shouldn't duplicate code. That's not its intention. The idea behind DRY is far grander than that.

    DRY says that every piece of system knowledge should have one authoritative, unambiguous representation. Every piece of knowledge in the development of something should have a single representation. A system's knowledge is far broader than just its code. It refers to database schemas, test plans, the build system, even documentation.

    Source


    Wednesday, February 15, 2017 12:10 PM
    Moderator
  • Well I guess part of it is don't repeat code. And by the way I am not trying to teach kids programming principles I am just trying to teach them to eliminate duplicate code. And this should be taught as soon as possible not when they get to C# or Java.

    Here's a quote from Robert C. Martin:

    Duplicate code is the root of all evil in software design. When a system is littered with many snippets of identical, or nearly identical code, it is indicative of sloppiness, carelessness, and sheer unprofessionalism. It is the guilt-edged responsibility of all software developers to root out and eliminate duplication whenever they find it.
    Small Basic is excellent for teaching kids programming simple programs using "only" procedural programming paradigm. I don't see why there would be a problem adding parameters to procedures to teach them "better" procedural programming.


    • Edited by Grega Stamac Thursday, February 16, 2017 9:37 AM
    Wednesday, February 15, 2017 12:44 PM
  • Hi Grega

    I also invite you to read this very recent post (January 2017) on this issue:

    Revisiting variable declaration

    Wednesday, February 15, 2017 1:36 PM
    Answerer
  • Thanks. This is about variable types and declarations though.

    • Edited by Grega Stamac Wednesday, February 15, 2017 1:41 PM
    Wednesday, February 15, 2017 1:40 PM
  • Hi Grega !

    I think it's a good idea.

    But we should agree on a format and a character who can work internationally.

    In some regional settings, the point is used as the decimal separator, whereas in other countries the comma is used as the decimal delimiter.
    Wednesday, February 15, 2017 6:39 PM
    Answerer
  • You're right. It was just a suggestion. Variable name starting with a number is not valid anyway so ".343" wouldn't be interpreted as a local variable name. But you are right it should be more explicit to separate it. Maybe "~"?

    • Edited by Grega Stamac Thursday, February 16, 2017 9:29 AM
    Thursday, February 16, 2017 9:29 AM
  • local vars should be defined by prefix LOCAL or LOC, not just ~ or # or other symbols

    also sub shoud be defined as functions with no return

    for simplicity sub should return 0 or "" if no return specified

    params should be passed by array args, as in ld xtension

    example

    Sub sum

      result=args[1]+args[2]/args[3]

    end sub

    textwindow.writeline(call sum(2 3 4))

     'result is 2.75

    same as:

    call sum(2 3 4)

    textwindow.writeline(result)

    Thursday, February 16, 2017 12:08 PM
  • Why? Isn't this easier to understand?

    Sub sum(a, b, c)

        result = a + b + c

    EndSub

    or with prefix

    Sub sum(a, b, c)

        ~sum = ~a + ~b + ~c

    EndSub

    Thursday, February 16, 2017 12:33 PM
  • The solution proposed by Tryhest is interesting :)
    Friday, February 17, 2017 11:17 PM
    Answerer
  • It is almost exactly the same as using LDCall.Function. Just replace

    call sum(2 3 4)

    with

    LDCall.Function3("sum", 2, 3, 4)

    There's absolutely no benefit except few less characters.

    With my solution you can even have IntelliSense working with args you cannot.

    • Edited by Grega Stamac Saturday, February 18, 2017 7:48 PM
    Saturday, February 18, 2017 6:50 PM