Logo

Hacker’s Guide to Visual FoxPro
An irreverent look at how Visual FoxPro really works. Tells you the inside scoop on every command, function, property, event and method of Visual FoxPro.

PARAMETERS(), PCOUNT()

These functions tell you how many parameters were passed to a procedure or function, but there’s a subtle difference between them.

Usage

nParmCount = PARAMETERS()
nParmCount = PCOUNT()

PARAMETERS(), the FoxPro version, tells you how many parameters were passed in the last function or procedure call. PCOUNT(), supposedly added only for dBASE compatibility and not even listed in the Help index before VFP 5, tells you how many parameters were passed to the routine you’re now in. This time, we think dBASE got it right.

Using PARAMETERS() has always been a tricky thing. You have to be sure to grab the value as soon as you enter the routine and hope that no ON KEY LABEL or other interrupt occurs that could change the value before you get it.

PCOUNT(), on the other hand, gets that part right every time. We recommend you stick with PCOUNT() and avoid PARAMETERS().

Example

* Demonstrate the difference between PARAMETERS() and PCOUNT()
DO rtn1 WITH 1,2,3
DO rtn2 WITH 1,2,3

PROCEDURE rtn1

PARAMETERS a, b, c

? "Parameters() returns", PARAMETERS()      && 3
? "Now I'll use a function"
= myudf(a)
? "Now parameters() returns", PARAMETERS()  && 1!

RETURN

PROCEDURE rtn2

PARAMETERS a, b, c

? "Pcount() returns", PCOUNT()      && 3
? "Now I'll use a function"
= myudf(a)
? "Now pcount() returns", PCOUNT()  && still 3

RETURN

PROCEDURE myudf()

PARAM x

RETURN

One of Visual FoxPro’s cool new capabilities introduced a gotcha in these two functions. You can leave parameters out of the list passed by just putting in a placeholder (“,”). These omitted parameters are filled in by Visual FoxPro as .F. values. But PARAMETERS() and PCOUNT() count the number of commas and add 1, rather than counting the actual number of parameters passed. In the example above, if you call the first routine with:

DO rtn1 WITH 1,,3

you still get 3 from both PARAMETERS() and PCOUNT().

The problem here is that, no matter which way this is handled, the ability to leave parameters out in the middle makes the whole business of parameter checking much harder. As it stands, there’s no way to really check which parameters were passed and which were omitted. So you need to be extra careful to check each parameter for validity and not make assumptions about what was passed and what wasn’t.

See Also

Function, LParameters, Parameters, Procedure