Skip to content

3.2 Defining and calling procedures

6502Nerd edited this page Oct 23, 2023 · 3 revisions

Procedures

Defining procedures

Declare and close a procedure definition with;

  • def_XXX(...)
  • enddef

Where XXX is the name of the procedure and ... represents any parameters. The parameter list is a sequence of variables which will take on the value passed, after first having their value saved on the stack, except for those variables qualified by &, for example

  • def_test(a,&b) defines a procedure called test, with two parameters - value of a is restored when the procedure is done, but not the value of b
  • enddef procedure ends and control transfers to the next statement after the invocation

The purpose of the & qualifier is to enable passing values back to the caller through variables as opposed to through a function return value.

Procedures can return values too - through the return keyword;

  • 100 def_add(a,b)
  • 110 return a+b
  • 120 println "Hi"
  • 140 enddef

In the above example, see that the procedure does not print out 'Hi', but jumps to the enddef of the definition - effectively terminating the procedure at the point return is encountered.

Passing arrays and strings

dflat handles parameters very simply in the interest of speed. This doesn't work well for passing arrays into procedures as the parameter variable does not inherit the all the necessary attributes of the array such as the dimensions. So we have to re-dimension parameter variable in the procedure, hence the redim keyword

  • It has the same syntax as dim
  • But it only affects the dimension size and does not allocate any storage
  • Therefore it uses the same storage as the array that was passed by the caller
  • So no deep copy here, any changes in the procedure to array values will be globally affected
  • Example:
  • 100 dim a$[10,5]
  • 110 _showString(a$,i)
  • ...
  • 200 def_showString(strArray$,index)
  • 210 redim strArray$[10,5]
  • 220 println strArray$[index]
  • 230 enddef