AccessMyLibrary provides FREE access to millions of articles from top publications available through your library.
Create a link to this page
Copy and paste this link tag into your Web page or blog:
When I showed you code blocks last month, I started simple, showing you how to pass program code as a parameter, not only reducing the amount of code you write, but making programs more compact.
This month, I'll discuss some advanced issues concerning code blocks, techniques you can use to streamline your programs. I'll cover the following topics:
1. Assigning to variables from inside code blocks
2. Passing parameters to code blocks by value and by reference
3. Get/set blocks
4. Early and late evaluation of macros inside code blocks
5. Creating temporary variables inside code blocks
6. Exporting local variables through a code block
7. Exporting static functions through a code block
8. Returning code blocks from functions
9. Accesing LOCAL variables in code blocks compiled at runtime.
Assigning to variables
As I've said many times before, Clipper's "=" operator is overloaded. Stated simply, it acts differently depending on how you use it. For example, if you use it in a statement, it performs an assignment operation:
i = 1 // Assign 1 to the variable i
If you use it inside an expression, however, as in:
IF i = 1
and
DO WHILE i = 1
it performs a comparison.
Code blocks contain expression, therefore if you use the = operator inside a code block, it performs a comparison. For example:
i = 1 b = {~~ i = 3} ? eval(b) // .F. - it compares i to 3 here ? i // 1
To assign to a variable inside a code block, you must use the inline assignment operator, ":=", as:
i = 1 b = {~~ i :=3} ? eval(b) // 3 ? i // 3
Unlike the = operator, the inline assignment operator isn't overloaded; it always performs an assignment, so you can also use it in a statement. The following code example, the preferred way of doing things, is identical to the previous example:
i := 1 b := {~~ i :=3} ? eval(b) // 3 ? i // 3
Convention: Always use the inline operator, :=, to perform assignments, and use the = operator only for comparisons.
Passing parameters by value and reference
Consider the following code fragment:
i := 1 b := {~n~ n := 3} ? eval (b, i) // 3 ? i // 1 - i is unchanged
As you can see, I passed the variable "i" to code block b. Code block b receives it in the formal parameter n, then assigns 3 to it. The eval() function returns the result of the last expression it evaluated, in this case 3. However, when you ask the value of i, …