Parameters extensions
Parameters with default values specified
As you know in VFP all the parameters declared with LPARAMETERS are by default optional so the following code is valid
SomeProcedure()
SomeProcedure("test")
SomeProcedure("test",1)
PROCEDURE SomeProcedure
LPARAMETERS tcValue, tnvalue, teValue
And the parameters not passed are intialized to .F.
When using the TPARAMETERS syntax, you need to pass all the parameters declared otherwise you get a compile time VFPCE0017 error about not overload with the matching parameters was found:.
SomeInfo("") && Show VFPCE0017
SomeInfo("",1) && Show VFPCE0017
PROCEDURE SomeInfo
TPARAMETERS tcValue as string, tnValue as integer, teValue
Now you can declare what will be the default value for your parameters so:
SomeInfo()
SomeInfo("")
SomeInfo("",1)
PROCEDURE SomeInfo
TPARAMETERS tcValue as string = "", tnValue as integer = -1, teValue = NULL
And the compiler generates the following code:
SomeInfo("", -1, null)
SomeInfo("", -1, null)
SomeInfo("", 1, null)
The same applies to typical PROCEDUREs with parameters declared with LPARAMETERS. In that case you can also use PCOUNT() to figure out how may parameters were passed to the procedure.
PROCEDURE SomeProcedure
LPARAMETERS tcValue = "test", tnvalue = 0, teValue && teValue defaults to .F. the other to the expression
Variable number of Arguments in procedures (VarArgs / unbound number of parameters)
Sometimes you need to have a procedure that can receive from 0 to n parameters, and you may not know what would be the upper bound for n. The compiler allows you to define procedures with variable number of parameters, known in the c / Java languages as varargs. You declare a procedure like that with:
PROCEDURE SumSquares
TPARAMETERS eValues() as double ...
LOCAL i, nResult
nResult = 0
FOR i = 0 TO ALEN(eValues)-1 && Start in zero because in .NET ParamArrays are zero based
nResult = m.nResult + eValues(m.i)^2
ENDFOR
RETURN m.nResult
And you invoke it with:
IF SumSquares(1,2,3,4,5,6) != 91
ERROR "ParamArray sum is failing"
ENDIF
The parameter that is varargs is declared as varargs using the three DOTS (.) after the parameter declaration Also the parameter must be the last one appearing in the TPARAMETERS declaration.
You can include other parameters before the varargs parameter as here:
PROCEDURE ComputeQubic
TPARAMETERS tnX, eValues() ...