Returning calculated values from a subroutine (New Features)

by UglyMike ⌂ @, Brussels, Tuesday, October 30, 2012, 14:54 (1816 days ago)

I've noted that subroutines cannot return calculated values without the use of globals. It that the way it is going to stay? Ideally, I'd want something like Call MySub(in1,in2,in3 ; out1,out2) which would pass 3 variables to the subroutine and receive 2 back.¨Is there a preffered method of doing this or is this a pipedream?

Returning calculated values from a subroutine

by lrcvs, Tuesday, October 30, 2012, 20:09 (1815 days ago) @ UglyMike

Can / how to spend DIM's / Arrays to functions and procedures?

Regards

Returning calculated values from a subroutine

by UglyMike ⌂ @, Brussels, Tuesday, October 30, 2012, 21:00 (1815 days ago) @ lrcvs

Can / how to spend DIM's / Arrays to functions and procedures?

Regards

If you mean how can we send arrays to subroutines, then it's the same as with varables: you need to define them as global:

Global MyArray, MyTextArray$
Dim MyArray(99)
Dim MyTextArray$(10)
...

Now you can freely use them in your main code and in your subroutines.

Returning calculated values from a subroutine

by lrcvs, Tuesday, October 30, 2012, 21:06 (1815 days ago) @ UglyMike

Very Thanks, UglyMike!!!

Returning calculated values from a subroutine

by lrcvs, Tuesday, October 30, 2012, 22:02 (1815 days ago) @ UglyMike

A example!!!

:::::::::::::::::::::::::::::::::::::::::::::::::::::::


dim x$(1)

cls
b$ = "10 20 30 40" #data to operate

print b$
print

x$ = explode (tst1$ (b$), " ") #<<< to send >>> function tst1 (b$)


for n = 0 to 5
print x$[n] # <<< reception new data of tst1
next n
print

j = int(x$[4])
k = int(x$[5])

print j + k

end

function tst1$(b$)
dim z(1)
dim y$(7)
j$ = ""
z$ = explode (b$, " ")
for n = 0 to 3
y$[n] = z$[n]
next n

#operations
j$ = string(int(z$[0]) + int(z$[3]))
k$ = string(int(z$[2]) + int(z$[3]))

y$[4] = j$
y$[5] = k$

tst1$ = implode (y$, " ")
end function

::::::::::::::::::::::::::::::::::.


send: 10 20 30 40

reception: 10 20 30 40 50 70

50 70 new data!!!

Returning calculated values from a subroutine

by Jim ⌂ @, Russell, KY, Wednesday, October 31, 2012, 18:22 (1815 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.

Returning calculated values from a subroutine

by lrcvs, Wednesday, October 31, 2012, 18:36 (1815 days ago) @ Jim

Ok!

I hope

Regards

RSS Feed of thread
powered by my little forum