1

How does one write a Variadic Function in Microsoft Visual Foxpro?

A variadic function is one that accepts a variable number of arguments - see http://en.m.wikipedia.org/wiki/Variadic_function. Examples are given for just about every other programming language in the world at http://rosettacode.org/wiki/Variadic_function but not the good ol' fox.

So given the following function:

Function PrintVars(var1,var2,var3)
    ? var1
    ? var2
    ? var3
End Func

How do we allow any number of arguments?

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
Caltor
  • 2,538
  • 1
  • 27
  • 55

2 Answers2

2

I would disagree that this is a limited capability. You don't have to do anything special. By default, VFP lets you pass fewer than the specified number of parameters.

Also, don't use the PARAMETERS() function to see how many parameters you received. It has a flaw; if you call another routine before using it, it tells you how many parameters were passed to that routine. Use PCOUNT() instead; it always tells you how many parameters were passed to the current routine.

Here's some code that demonstrates what's wrong with PARAMETERS():

DEBUG

Subproc("abc", 123)

RETURN

PROCEDURE Subproc(cParm1, nParm2)

DEBUGOUT "Immediately on entry to Subproc"
DEBUGOUT "  PARAMETERS() returns ", PARAMETERS()
DEBUGOUT "  PCOUNT() returns ", PCOUNT()

Subsubproc()

DEBUGOUT "After call to Subsubproc"
DEBUGOUT "  PARAMETERS() returns ", PARAMETERS()
DEBUGOUT "  PCOUNT() returns ", PCOUNT()

RETURN

PROCEDURE Subsubproc

RETURN

After running this code, take a look in the Debug Output window.

Tamar E. Granor
  • 3,817
  • 1
  • 21
  • 29
  • 2
    True. I don't tend to think much about the differences between PCOUNT() and PARAMETERS(); we do all the parameter-related housekeeping at the beginning of any given function so it doesn't matter. The limitation on the facility (compared to most modern languages) is that you have to specify at least as many parameters in the PARAMETERS statement as you ever intend to actually call it with. – LAK Nov 19 '14 at 15:34
  • Are you saying the only way to do it would be a function signature with the maximum number of parameters (according to http://msdn.microsoft.com/en-us/library/3kfd3hw9(v=vs.80).aspx the maximum number of passed parameters is 26) and use PCOUNT() to check how many have been passed? Good catch with PARAMETERS vs PCOUNT. Can't believe I have been developing with VFP near 20 years and have never used or come across DEBUGOUT. I have always just used ? or STRTOFILE in the past. – Caltor Nov 19 '14 at 15:55
  • 2
    Another alternative is to pass an object that contains a property for each parameter, or an array property. To be honest, I've never needed more than (or anywhere near) 26 parameters. If you do, you may want to rethink the design. – Tamar E. Granor Nov 19 '14 at 21:29
1

You have a limited ability to do this in VFP.

FUNCTION printvars
PARAMETERS p1, p2, p3, p4
    ? "Parameter count", PARAMETERS()
    ? p1
    ? p2
    ? p3
    ? p4
RETURN

Call this like this: printvars(1, 2)

and your results will be:

Parameter count    2
         1
         2
.F.
.F.

VFP will initialize any parameter you don't explicitly pass with a logical .F. value. The PARAMETERS() function obviously tells you how many were actually passed.

Passing too many parameters will give you an error that your PARAMETER statement needs to specify more parameters.

LAK
  • 961
  • 7
  • 18
  • As per my comment to Tamar are we coming to the conclusion specifying the maximum number of parameters is the only way to do this in VFP? Just thinking out loud I was waiting for someone to come along with a magic trick involving macro substitution or arrays or pointers or passing by reference or a Windows API call or something. – Caltor Nov 19 '14 at 16:01
  • 2
    Yes, you'd need to specify the maximum number of parameters in your PARAMETER statement. If you want to pass a single array containing the actual parameters, the sky is the limit! That might be your best way to go if you truly don't know how many things you may need to pass. – LAK Nov 19 '14 at 19:14
  • 2
    In addition to simple parameters, you could actually pass in objects too that could have many properties, arrays, alias names, etc, but I think the intent would be for common same data types. – DRapp Nov 24 '14 at 01:14