GDL
About building parametric objects with GDL.

Passing parameters to subroutines

dushyant
Enthusiast
Hi

Is it possible to pass parameters to subroutines? This page talks about parameters, but I can't find the syntax: https://gdl.graphisoft.com/gdl-style-guide/subroutines

Also can subroutines call other subroutines?

Thanks
4 REPLIES 4
Podolsky
Ace
Yes, you can pass parameters to subroutine and call another subroutine from previous. The script logic can be like that:
parameterName=1
GOSUB 'SUBROUTINE'
parameterName=2
GOSUB 'SUBROUTINE'
parameterName=3
GOSUB 'SUBROUTINE'

END

'SUBROUTINE':
IF parameterName=1 THEN GOSUB 'SUBROUTINE_1'
IF parameterName=2 THEN GOSUB 'SUBROUTINE_2'
IF parameterName=3 THEN GOSUB 'SUBROUTINE_3'
RETURN

'SUBROUTINE_1':
...
RETURN

'SUBROUTINE_2':
...
RETURN

'SUBROUTINE_3':
...
RETURN
dushyant
Enthusiast
Thanks Podolsky. I don't see though the passing of parameters _to_ the subroutine. I understand what you did there but it's a bit odd to keep resetting the variable just so that the next call to the subroutine takes that value. I wish there was some way to pass custom params directly to subroutines, like you do with macros.
runxel
Legend
This is simply not possible, and also, frankly, would not make any sense in a BASIC-derived language.

See, GDL has no concept of scopes, so it makes no real sense to be able to pass any variables to a subroutine.
A subroutine is not a function. It is basically the same as a GOTO, but you are able to RETURN. This alone doesn't make it a function as you might be used to as in other more modern scripting languages.

The only thing that is kind of scoped in GDL is the transformation stack while using GROUPs.

So, to recap: There are no functions, no scopes, and no variable passing in GDL.
Instead every variable behaves as it would be global.
Variables set in the Master script are even usable in every other script.

Instead of the passing of a variable you need like podolsky has shown something that I call a "nasty caller".
if x = 1 then 
    nasty_caller = "foo"
else
    nasty_caller = "bar"
endif
gosub "Quz"
....
end
"Quz":
    ! do something with nasty_caller 
return
Lucas Becker | AC 27 on Mac | Author of Runxel's Archicad Wiki | Editor at SelfGDL | Developer of the GDL plugin for Sublime Text |
«Furthermore, I consider that Carth... yearly releases must be destroyed»
dushyant
Enthusiast
Ok. Thanks runxel.