Home
JAQForum Ver 24.01
Log In or Join  
Active Topics
Local Time 01:57 16 Jul 2025 Privacy Policy
Jump to

Notice. New forum software under development. It's going to miss a few functions and look a bit ugly for a while, but I'm working on it full time now as the old forum was too unstable. Couple days, all good. If you notice any issues, please contact me.

Forum Index : Microcontroller and PC projects : help with subroutine

Author Message
crez

Senior Member

Joined: 24/10/2012
Location: Australia
Posts: 152
Posted: 09:13pm 03 Mar 2016
Copy link to clipboard 
Print this post

I wish to call a subroutine like:

this_sub(1,2,3)

or this_sub(22,67,5,42)

and have this_sub allocate the n arguments to the first n elements of a local integer array. I feel there is a simple way of doing this but I have not found it. I'm probably having a brain fade . Can anyone help?
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4038
Posted: 09:41pm 03 Mar 2016
Copy link to clipboard 
Print this post

Probably not possible as almost all Basics disallow variable numbers of arguments.

John
 
matherp
Guru

Joined: 11/12/2012
Location: United Kingdom
Posts: 10251
Posted: 10:02pm 03 Mar 2016
Copy link to clipboard 
Print this post

  Quote   wish to call a subroutine like:

this_sub(1,2,3)

or this_sub(22,67,5,42)


This works fine. non-included arguments are set to 0 in the subroutine. The only gotcha is if 0 needs to be a valid parameter.

test 1,2,3
end
sub test(a%,b%,c%,d%)
print a%,b%,c%,d%
end sub


prints "1 2 3 0"
 
crez

Senior Member

Joined: 24/10/2012
Location: Australia
Posts: 152
Posted: 10:49pm 03 Mar 2016
Copy link to clipboard 
Print this post

that works, but now for allocating the values to elements of an array, without
arr(1)=a% : arr(2)=b% : arr(3)=c% etc. It gets a bit untidy if I have a maximum of say 25 values.
 
JohnS
Guru

Joined: 18/11/2011
Location: United Kingdom
Posts: 4038
Posted: 06:21am 04 Mar 2016
Copy link to clipboard 
Print this post

It is rare to want to write code like that in Basic. Your best bet is to use a different way.

John
 
crez

Senior Member

Joined: 24/10/2012
Location: Australia
Posts: 152
Posted: 11:36am 04 Mar 2016
Copy link to clipboard 
Print this post

John,
I guess you are right, the main reason I am trying this way is because I am translating some C code to basic. The C code uses pointers, which don't have a direct translation in basic. I was hoping I was missing something obvious which someone would point out to me.

The following code does what I wanted but relies on the local variables a-f being in sequence in memory. Perhaps there is a chance that an interrupt happening during the opening of the sub would mean this is not so.

'a% is the number of following arguments
' all arguments are 0-255 integers
Sub writeBuf(a%,b%,c%,d%,e%,f%)
Local integer z,cnt,v
cnt=Peek(var a%,0)
For z=1 To cnt
arr(z)=Peek(var a%,z*64)
Next z
End Sub
 
Print this page


To reply to this topic, you need to log in.

The Back Shed's forum code is written, and hosted, in Australia.
© JAQ Software 2025