Board Logo
« SUB, GOSUB, FUNCTION, Useful ERROR Messages »

Welcome Guest. Please Login or Register.
Jan 18th, 2018, 3:53pm


Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

This board is not meant for general discussion, it is meant for posting articles to help others.
For general discussions use the appropriate board, which best describes your problem area.

« Previous Topic | Next Topic »
Pages: 1 2  Notify Send Topic Print
 hotthread  Author  Topic: SUB, GOSUB, FUNCTION, Useful ERROR Messages  (Read 13245 times)
pantsonfire
Member in Training
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 25
xx Re: SUB, GOSUB, FUNCTION, Useful ERROR Messages
« Reply #15 on: Aug 10th, 2011, 8:53pm »

I know arrays are global and thats the problem. It means the function cannot figure out which array to deal with.
I could write a function to deal with say x() array but then i need to write a new function to deal with y() array.
Unless i can pass an array as a parameter to the function the function cannot determine which particular array to deal with. Or am i missing something simple here.
User IP Logged

Stefan Pendl
Administrator
ImageImageImageImageImage


member is offline

Avatar

Let's talk JB ...


Homepage PM

Gender: Male
Posts: 3712
xx Re: SUB, GOSUB, FUNCTION, Useful ERROR Messages
« Reply #16 on: Aug 10th, 2011, 11:43pm »

Just BASIC does not support passing arrays as arguments to procedures.

You do not have any possibility to accomplish what you like to do.

BTW, it would have been best to start a new thread, since this is a tutorial and not a questioning thread.
User IP Logged

Stefan - Homepage

Remember to read the forum rules board wink

Just BASIC 1.01, Windows 10 Professional x64, Intel Core i7-4710MQ 2.5GHz, 16GB RAM
Welopez
Moderator
ImageImageImageImageImage


member is offline

Avatar

Never let your beliefs get in the way of learning.


PM

Gender: Male
Posts: 4407
xx Re: SUB, GOSUB, FUNCTION, Useful ERROR Messages
« Reply #17 on: Aug 11th, 2011, 12:26pm »

I'm sorry, pantsonfire, I still fail to see your problem. Perhaps it's sort of like "I can't see the forest for the trees?"

My function is named sumTwo(). It reads the values of two elements from different arrays and sums them for you. fiddleFaddle is simply a nonsense label to call the function.
Code:
'  sumTwo (sum two numbers from different arrays)

DIM x(20), y(20)

FOR k=1 TO 20
    x(k)=k
    y(k)=(k*3)
NEXT k

FOR j=1 TO 10
    fiddleFaddle=sumTwo(j)
NEXT j

PRINT
FOR k=1 TO 10
    PRINT x(k), y(k)
NEXT k

END

FUNCTION sumTwo(j)
    PRINT x(j)+y(j)
END FUNCTION
 

First I filled array x() and array y() with twenty values each. Then I repeated FUNCTION sumTwo ten times, but you can do it twenty times if you choose. Because array values are global, I had no difficulty summing the two values, passing only the falue of j to identify the array elements to be summed.

If you wished to choose array values manually, it would be a very simple process to write an input statement to define the two array elements.

Good luck to you.
User IP Logged

JB 1.01
Win7 64bit, 4 GB RAM, Pentium 6200@2.13 GHz (laptop)
WinXP, 1 GB RAM, Intel N270@1.6 GHz (netbook)
tenochtitlanuk ( John F)
Full Member
ImageImageImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 430
xx Re: SUB, GOSUB, FUNCTION, Useful ERROR Messages
« Reply #18 on: Aug 11th, 2011, 2:49pm »

In some languages you can have several one-D arrays of the same size, and pass them by name, and have operators/functions that work on them. You could then have ( pseudo-code) like
Code:
x[] =1,  2,  3,   4, 5, etc.... 
y[] =3,  6,  9, 12, .....
z[] =2,  1,  6,   8, ....

print addThem( x(), y())
print addThem( x(), z()) 

In JB a way to do this kind of thing is to invent your own 'structure' to hold the data, rather than a named array. The following code defines a structure which holds the name, and twenty values, in a single comma-separated string. Now you can define functions to do what I think you may want... you can ask it to add two named array-strings and allocate the result to a new one.
Code:
global arrayL
arrayL =20

x$     ="x"
y$     ="y"
z$     ="z"
pants$ ="pants"

for k=1 to arrayL
    x$       =x$      +","  +str$( k)
    y$       =y$      +","  +str$( k *3)
    z$       =z$      +","  +str$( k /3)
    pants$   =pants$  +","  +str$( rnd( 1))
next k

print " x$     "; x$
print " y$     "; y$
print " z$     "; z$
print " pants$ "; pants$
print

print " Finding n'th term"
for i =1 to arrayL
    print term( x$, i), term( y$, i), term( z$, i), term( pants$, i)
next i

print
new1$ =addEm$( x$, y$, "new1")
print " new1$ =array with terms made by adding corr. terms of x and y"; chr$( 10); "   =";     new1$
print

new2$ =addEm$( x$, z$, "new2")
print " new2$ =array with terms made by adding corr. terms of x and z"; chr$( 10); "   =";     new2$
print

new3$ =addEm$( z$, pants$, "new3")
print " new3$ =array with terms made by adding corr. terms of z and pants"; chr$( 10); "   ="; new3$
end
'   _____________________________________________
function addEm$( a$, b$, c$)
    addEm$ =c$
    for i =1 to arrayL
        aVal =val( word$( a$, i +1, ","))
        bVal =val( word$( b$, i +1, ","))
        addEm$ =addEm$ +"," +str$( aVal +bVal)
    next i
end function
'   _____________________________________________

function term( d$, n)
    term =val( word$( d$, n +1, ","))
end function

 

Does that make any kind of sense?
User IP Logged

Visit my LB/JB pages at http://www.diga.me.uk/index.html
I use XP and Ubuntu Linux on old machines!
pantsonfire
Member in Training
ImageImage


member is offline

Avatar




PM

Gender: Male
Posts: 25
xx Re: SUB, GOSUB, FUNCTION, Useful ERROR Messages
« Reply #19 on: Aug 11th, 2011, 3:34pm »

Thanks for your help Welopez. I can tell your a really helpful person. smiley
User IP Logged

Pages: 1 2  Notify Send Topic Print
« Previous Topic | Next Topic »

Conforums Terms of Service | Membership Rules | Home | Search | Recent Posts | Notification | Format Your Message | Installation FAQ

Donate $6.99 for 50,000 Ad-Free Pageviews!

| |

This forum powered for FREE by Conforums ©
Sign up for your own Free Message Board today!
Terms of Service | Privacy Policy | Conforums Support | Parental Controls