#26 functions / sub routines with parameters


having a way to define functions/subroutines with arguments would be very useful, for example something like:

sub multiply(x,y)
return x*y
end sub

or maybe just something like:

return x*y

The latter form seems to be less intuitive but much more compact and would just read like a conventional label with arguments, where the stack frame would be popped two times and the corresponding values would be assigned to the local variables x and y so that they are available in the current scope


  • Nobody/Anonymous

    I would like to second this request, and to provide some more insight:

    I started programming back in the late 80s on the C64 and later on also using GWBasic on a 286 PC - however, it was only until I started using qbasic, that I finally stopped using lots of global variables, even though I was already using "subs" (gosub) - but a sub routine without support for parameters is not that useful and actually encourages bad programming. (I had to keep track of all the variables used in each subroutine)

    However, once you can actually create self contained mini functions that do not rely on external global variables, the point of code reuse becomes blatantly obvious and everything else so much more powerful and FUN

    Maybe this really is a bit too advanced for children on the other hand, but personally -in retrospective- I do feel that the lack of parameter lists for sub routines in these two basic dialects, was severely limiting my evolution as a programmer.

  • Nobody/Anonymous

    I was also thinking about this a bit, and maybe there is a way to have some form of global vector/array for function arguments, which could be internally implemented like a stack.

    So that we could keep using simple labels for defining a function entry point, but also overload the gosub instruction for labels which implicitly supports a number of arguments which are in turn added to an "argument stack", which could be accessed using a "get_argument" helper function in each function:

    $name=get_argument 1
    print "hello " + $name

    gosub say_hello "Basic 256";

    If overloading gosub is not favored, there could be a separate command to directly manipulate the argument stack to add variables to it, which could in turn be retrieved by the called function.

    By exposing such low level routines, it would also be straight forward to support functions with different signatures or differing argument count

    What do you think?

  • Jim Reneau

    Jim Reneau - 2010-05-31

    The spirit of BASIC-256 (as I understand it) is to be a language used for early exploration. The introduction of variable scope will add quite a lot of complexity.
    I believe that a young programmer, today, with an understanding of loops, variables, conditions, arrays, and basic graphics should be able to easily move to a language like Python for real functions/objects and the like.

  • Nobody/Anonymous

    I totally agree with you, on the other hand programming in general is all about abstraction, composition and code sharing/reuse - parametrization of existing code is a key feature to understand programming in the long run.

    Binary machine code is abstracted away using assembly mnemonics (i.e. symbols), common boilerplate assembly mnemonics are abstracted away by use of macros, which also helps isolate bugs because code is not simply copied/pasted, but literally being reused. Use of functions instead of redundant macros improves code size, and so on.

    Another important thing to keep in mind is that today, use of global variables is generally discouraged in most programming languages and environments/settings - there should be as little shared/accessible state as possible in programs, in order to simplify programs and components, this applies in particular in multi-threaded and concurrent programming, where programs should have a well defined public interface with a certain signature of inputs and outputs.

    So being able to show people how to turn a certain piece of code into a generic and parameterizable routine is extremely powerful because it has so many benefits.

    It basically illustrates how the language itself works, by consisting of building blocks that make use of other lower level building blocks.

    I am not saying this as someone who wants to see support for function arguments added to kidbasic, but instead as someone who remembers having had quite some issues migrating from GWBasic to QBasic, where variable scope was suddenly function specific (SUBS), it took me quite a while to get behind everything - simply because I was always thinking in terms of global variables.

    I don't know if there really is a good solution, because the concept in general is probably not easy to grasp when you are totally new to programming.

    Maybe the easiest thing to deal with this would be to encourage a good programming style where there is a global vector (array) maintained for storing arguments, and an argument counter to store the arity of arguments passed to a function, basically in an argc/argv fashion.

    So that all this argument setup/processing stuff is still taught by being explicitly shown in examples, where the arguments array is being explicitly set up before a function is called, and all arguments get in turn retrieved from the arguments array.


Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:

No, thanks