none
Arguments RRS feed

  • Question

  • How do I pass arguments and accept parameters in subroutines? 

    I thought it would be something like this

    ...
    Sub add(x,y)
       TextWindow.WriteLine(x+y)
    EndSub
    
    add(3,4)
    ...

    But it doesn't seem to work :(. Any ideas? I'm sure this an easy solution. 
    Saturday, May 2, 2009 4:54 AM

Answers

  • This may come as a surprise to you, but you can't. If you read some of the earlier posts, you will find that the decision was made not to provide the facility. The idea was to keep the language simple to be suitable for beginners so variables have no scope. You can use a stack object to simulate passing parameters, but this is surely a harder concept for beginners and so why was it included? I suggest you add your voice to the "Features & Bug Fixes for v0.5" thread and ask for parameters to be included.
    Saturday, May 2, 2009 10:25 AM

All replies

  • This may come as a surprise to you, but you can't. If you read some of the earlier posts, you will find that the decision was made not to provide the facility. The idea was to keep the language simple to be suitable for beginners so variables have no scope. You can use a stack object to simulate passing parameters, but this is surely a harder concept for beginners and so why was it included? I suggest you add your voice to the "Features & Bug Fixes for v0.5" thread and ask for parameters to be included.
    Saturday, May 2, 2009 10:25 AM
  • JordanScales,

    There is no argument passing to subroutines in SmallBasic.  This is to keep it as simple as possible.

    All variables are global and can be accessed and changed from anywhere in your code at any time.

    This can cause its own difficulties, but careful use of variables allows you to go a long way.

    One possible suggestion is to name variables that you only use locally within a subroutine differently (e.g. prefix "m_", often used to denote a 'member' or local variable).  You copy the global variable to the variable prefixed "m_" at the start of the subroutine to ensure the original global variable cannot be changed by the subroutine.

    For x = 1 To 10
      SinSquare()
    EndFor

    Sub SinSquare
      m_x = x
      m_x = m_x*m_x
      m_x = Math.Sin(m_x)
      TextWindow.WriteLine(m_x)
    EndSub

    Another suggestion is to use the Stack command to populate a stack of variables before each call to a Sub.  This is a bit unnecessary I think and makes for over complex code in most cases.

    It is important to make sure you always set the required variables before a subroutine call is made:

    For i = 1 To 10
      For j = 1 To 10
        x = i
        y = j
        Add()
      EndFor
    EndFor

    Sub Add
      TextWindow.WriteLine(x+y)
    EndSub

    Be careful for the following miss-use of variables ("i" used inappropriately):

    For i = 1 To 10
      x = i
      y = 10-i
      Add()
    EndFor

    Sub Add
      For i = 1 To 3
        TextWindow.WriteLine(x+y+i)
      EndFor
    EndSub

    Good Luck
    Saturday, May 2, 2009 10:31 AM
    Moderator
  • Sub Add
       Add_Return = Add_X + Add_Y
    EndSub
    
    Add_X = 5
    Add_Y = -2
    Add()
    ' result is in Add_Return

    Fremy - Developer in VB.NET, C# and JScript ... - Feel free to try my extension
    Saturday, May 2, 2009 12:14 PM
  • Thanks everyone, I know of some workaround methods, I was just wondering if there was a clear-cut way to pass arguments and such. 

    Thank you, I love this fun little language. 
    Saturday, May 2, 2009 10:34 PM