Returning calculated values from a subroutine (New Features)

by Jim ⌂ @, Russell, KY, Wednesday, October 31, 2012, 18:22 (1697 days ago) @ lrcvs

In version 0.9.9.12 (on SVN right now and to be released soon), you can define a function or subroutine argument and the data being passed to it as REF(varname) so that it will pass the variable "By Reference".

This technique will allow you to pass an array (where you could not prior) or a variable you want he function/subroutine to modify. The testing is not totally complete but you can get the source from SVN and give it a whirl.

# test of subroutine calls with variables passed by reference
# 2012-10-30 j.m.reneau

a = 10
b = 20
dim c(10)
c[3] = 30
d$ = "am d"
e$ = "am e"
dim f$(10)
f$[1] = "f sub one"

print "before"
print "a " + ref(a) + " " + a
print "b " + ref(b) + " " + b
print "c[3] " + ref(c) + " " + c[3]
print "d$ " + ref(d$) + " " + d$
print "e$ " + ref(e$) + " " + e$
print "f$[1] " + ref(f$) + " " + f$[1]

call foo(ref(a), b, ref(c), d$, ref(e$), ref(f$))

print
print "after"
print "a " + ref(a) + " " + a
print "b " + ref(b) + " " + b
print "c[3] " + ref(c) + " " + c[3]
print "d$ " + ref(d$) + " " + d$
print "e$ " + ref(e$) + " " + e$
print "f$[1] " + ref(f$) + " " + f$[1]
end

subroutine foo(ref(m), n, ref(o), p$, ref(q$), ref(r$))
m = 99
n = 88
o[3] = 77
p$ = "now p"
q$ = "now q"
r$[1] = "now r sub 1"
call bar(ref(m),ref(q$))
end subroutine

subroutine bar(ref(x), ref(y$))
# reference of a reference goes back to the original
x = 101
y$ = "now y"
end subroutine


The REF() function returns a reference to a variable (the symbol number with type of variable string/numeric) to the stack and not the value of the variable like a call without REF() on an argument. The REF() on the function/subroutine definition tells the compiler that a reference is expected not normal data.


Complete thread:

 RSS Feed of thread

powered by my little forum