Welcome! Log In Create A New Profile

Advanced

Passing Integer Arrays between ByPic functions

Posted by g8jcf 
Passing Integer Arrays between ByPic functions
July 31, 2014 11:41PM
Hi

I'm just getting started with ByPic so please be patient with me !

I'd like to be able to pass Integer Arrays into ByPic functions, have the function then return back results in another array, something along the lines of :
function DoStuff( a(), b(), results() )
  results(0)=a(0)+b(0)
  results(1)=a(1)+b(1)
endf

function Test()

  dim i(2),j(2),k(2)

  i(0)=1; i(1)=2
  j(0)=3; j(1)=4

  DoStuff( i(), j(), k() )

  print k(0), k(1)

endf

The code above doesn't work, so can I do this type of thing in ByPic on a 150MX, and if so how ?

If I use Global variables then I can make it work, but that's a bit of "bodge" in my book, and I thought there must be a more elegant way.

Many thanks in advance

Peter

I've done further experiments, and using Peek and Poke I kind of like pass Integer Arrays between functions, see code below
constant  Billion 1000000000

//
// Add 2 18 digit integers, return result in p
// m, n, p are integer arrays
//
function Add18(m,n,p)
  dim i
  dim Cy
  
  i=peek(?m+0) + peek(?n+0)
  Cy=i / Billion
  i=i % Billion
  poke(p+0,i)
  
  i=peek(?m+4) + peek(?n+4) +Cy
  i=i % Billion
  poke(p+4,i)
  
endf

function a()
    dim I(2),J(2),K(2),R$[24]
    
    I(0)=383680000
    I(1)=343597
    
    J(0)=973836800
    J(1)=3435
    
    Add18(I(0),J(0),?K(0))
    
    R$=format$( "%9u", K(1) ) + "" + format$( "%9u", K(0) )
    
    print R$ + "\n"
    
endf

Is that it, or is there a more neater/elegant way ?



Edited 1 time(s). Last edit at 08/01/2014 02:01AM by g8jcf.
Re: Passing Integer Arrays between ByPic functions
August 01, 2014 08:38AM
Peter,
as you have discovered you can't pass arrays into functions but you can pass the address and use that as you have done rather well in the example.

Space on the IC is limited and so it is impossible to have the functionality of say Python so some compromises have to be made. Error reporting is one area that has been sacrificed to space and would be much better if there were more flash available.
Jim
Re: Passing Integer Arrays between ByPic functions
August 01, 2014 03:39PM
Thanks Jim, that's OK I understand that not everything can be squeezed in when memory is tight.

Now that Microchip are shipping PIC32MX170F256B-50I/SP which has double the memory, will you be releasing ByPic for the MX170 with some improved facilities ?

Many thanks

Peter
Re: Passing Integer Arrays between ByPic functions
August 01, 2014 04:15PM
Peter,
probably, but for the moment I am stuck with the commercial C32 compiler that doesn't support the newer chips. The migration path to XC32 will be time consuming but I am working on it.
Jim
Re: Passing Integer Arrays between ByPic functions
August 01, 2014 06:12PM
OK, will wait patiently :)

Take care

Peter
Sorry, you do not have permission to post/reply in this forum.