xsharp.eu • VFP - Do <proc> with ...
Page 1 of 1

VFP - Do <proc> with ...

Posted: Fri Oct 25, 2019 5:47 pm
by Karl-Heinz
Guys,

a questions to the VFP community. It´s about what the VFP SP2 helpfile says about "Parameters in Procedures and Functions"

Code: Select all

myVar = 4
myVar2 = 5

DO myProcedure WITH myVar, myVar2
 

By default, variables and arrays pass to procedures by reference. Therefore, changes made in the procedure to passed variables and arrays are passed back to the calling program. For example, suppose the procedure increments the value in myVar by the value in myVar2. The modified value of myVar becomes the 
new value of myVar when the procedure returns control to the calling program.+
ok, so far there´s no problem, but here it comes ;-)

Code: Select all

Alternatively, if you want to use the DO command but want to pass data by value, enclose each parameter with parentheses (()) as shown in the following 

example:

 
DO myProcedure WITH (myVar), (myVar2)
Honestly, even in my wildest (xbase) dreams i never made it to dream about such an option ;-) Does this really work as described ?

regards
Karl-Heinz

VFP - Do <proc> with ...

Posted: Fri Oct 25, 2019 5:55 pm
by robert
Karl Heinz,
The version of the compiler on my machine (and if you download the compiler source and do a get latest in the XSharpCompiler branch, you will have that too) supports parameters by reference to untyped function and procedure calls (which we call "Clipper Calling Convention", but that should probably be "Xbase calling convention")
What we have done:
- parameters passed in the DO <proc> WITH <params> syntax will be by reference if they are "simple identifiers" (like variable names). They are by value when they are literals, expressions such as (myvar) etc
- parameters passed in the x = Func(params) syntax are passed by value. If you want to pass them by reference you either have to prefix them with the @ sign or use the REF keyword.

What other "wild dreams" do you have. Or no, forget it. You would probably break the "forum rules" when you tell us that B)

Robert

VFP - Do <proc> with ...

Posted: Sat Oct 26, 2019 1:26 am
by mainhatten
Karl-Heinz,
yupp it does. Consider use of () as "getting the expression" in

Code: Select all

use (lcTable) alias (lcMyAlias)
not the variable itself (the ev_struct in C code) is the parameter, only the ev_value or the string ev_ptr points at.

MUCH safer compared to fiddling with "set udfparms", as any code from 3. party might have very distinct ideas on what parameter is called as value or via reference. Also pls. remember normal vfp is an interpreter - if I have a function clearly spelling out that parameter is not to be passed as reference, for 1-5 liners I work not on a copy of the parameter, but modify the parameter - expected as value itself. Saves lines, which is important in vfp, as no JIT optimizer can eliminate them in vfp.

HTH
thomas