Passing arrays into functions?

Have a QScript to share, or need help with programming? Post your comment here.
Post Reply
hjelmstade
Posts: 34
Joined: Wed May 18, 2016 6:51 pm
Contact:

Passing arrays into functions?

Post by hjelmstade » Sat Dec 24, 2016 10:18 pm

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

User avatar
earik
Site Admin
Posts: 474
Joined: Mon Dec 01, 2014 12:41 pm
Contact:

Re: Passing arrays into functions?

Post by earik » Tue Dec 27, 2016 7:39 pm

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

hjelmstade
Posts: 34
Joined: Wed May 18, 2016 6:51 pm
Contact:

Re: Passing arrays into functions?

Post by hjelmstade » Tue Dec 27, 2016 11:35 pm

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!

Post Reply

Who is online

Users browsing this forum: No registered users and 42 guests