add tag
Trevor
I started learning MetaPost a few days ago. I want to create a function that returns an array (of pairs, for example). I know that I can create a function that returns a pair as follows:
```
def pairfun(expr k)=
  (k,2*k)
enddef;

pair a;
a = pairfun(3);
```
I would like to create a function that returns an array of pairs, where the value of each pair is filled by a for-loop. The first thing I did was try this:
```
vardef pairarrayfun(expr k,n)=
  for i=0 upto n: (i,2*k); endfor
enddef;

pair b[];
b = pairarrayfun(3,5);
```
But this didn't work. Then I tried to create a variable that references the array in the function, and return that variable:
```
vardef pairarrayfuntwo(expr k,n)=
  save v;
  pair v[];
  for i=0 upto n: v[i] = (i,2*k); endfor
  v
enddef;

pair c[];
c = pairarrayfuntwo(3,5);
```
This didn't work either. I know I'm probably doing many things wrong here, but please forgive me, I'm a beginner. :)

I've tried looking in the [MetaPost User's Manual](https://www.tug.org/docs/metapost/mpman.pdf) and examples online, but still can't figure out how to do something like this.
Top Answer
Anonymous 2412
```
vardef pairarrayfuntwo(suffix c)(expr k,n)=
  for i=0 upto n:
    c[i]=(i,2*k);
  endfor;
enddef;

beginfig(1);
  pair G[];
  pairarrayfuntwo(G)(3,5);
  for k=0 upto 5:
    dotlabel("",G[k]);
  endfor;
endfig;

end
```

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.