Page 1 of 1

Passing arrays into functions?

Posted: Sat Dec 24, 2016 10:18 pm
by hjelmstade
Hi Earik,

Can you pass arrays into functions? If so, can you provide an example of the syntax?

I tried doing this numerous ways, including the c++ syntax but could not get it to work. I'm assuming arrays would be passed by reference but I kept running into an issue where the function would not recognize the input as an array and I couldn't figure out how to cast it correctly.

Let me know if you need an example of what I was trying to do.

Thanks!
hjelmstade

Re: Passing arrays into functions?

Posted: Tue Dec 27, 2016 7:39 pm
by earik
Yes, sort of. I haven't done it in a while, so I had to mess around to remind myself though. Try it like this:

indicator:

Code: Select all

define array one[];

if (barnum == barsback) {
    push(one,1);
    push(one,2);
    push(one,3);

    ret=testfunc(one);
}

plot1=ret;  
function: "testfunc"

Code: Select all

input:myin;
return myin[1];  
So basically, just pass it as a regular variable. The trick is that the function isn't going to know it's an array, and will think it's a regular variable, so you can't use array-specific functions (sort, etc) and have to also pass in things like length along with the array itself.

Regards,

Earik

Re: Passing arrays into functions?

Posted: Tue Dec 27, 2016 11:35 pm
by hjelmstade
Got it, thanks Earik. My issue was trying to use clear() in the function after I had passed. I'll just avoid passing arrays and will instantiate where I need them.

Thanks!