You could pass an array or object instead of many parameters and step through it in your loop. Note: If you are passing an array, you will have to pass it by reference.
Array:
function calcAverage
lparameters laValues
local i, l, v
v = 0
l = alen(laValues,1)
for i = 1 to l
v = v + laValues[i]
endfor
return v/l
Object:
function calcAverage
lparameters loValues
local i, l, v
local array laValues[1]
laValues = amembers(laValues,loValues,,'U')
v = 0
l = alen(laValues,1)
for i = 1 to l
v = v + laValues[i]
endfor
return v/l
- Keith Payne
>Can anyone suggest a way to pass a variable number of parameters to a method or function where the maximum number is not known in advance?
>
>E.g. Foxpro pseudocode below:
>
>function calcAverage()
>local n,v,i
>n=pcount()
>for i=1 to n
> v=v+parameter(i)
>next
>return v/n
>
>avg=calcAverage(1,2,3,4,5,6,7)
>
>I know that you can declare a maximum number of parameters. But what if I dont know in advance? Should I declare 10 parameters and do the following
>
>function calcAverage(p1,p2,p3,p4,p5,p6,p7,p8,p9,p10)
>local n,v,i,sp
>n=pcount()
>v=0
>for i=1 to n
> sp='p'+ltrim(str(i,2))
> v=v+&sp
>next
>return v/n
>
>
>Costas