AccessMyLibrary provides FREE access to millions of articles from top publications available through your library.

Code blocks, inside and out. (Hardcore Clipper column) (Tutorial)

Data Based Advisor

| December 01, 1991 | Spence, Rick | COPYRIGHT 1989 Advisor Publications, Inc. (Hide copyright information)Copyright

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, …

Related articles from newspapers, magazines, journals, and more
Using code blocks, again. (Hardcore Clipper column) (Tutorial)
Magazine article from: Data Based Advisor Spence, Rick November 1, 1991 700+ words
Code blocks: what, where, and why? (Clipper Basics) (Column) (Tutorial)
Magazine article from: Data Based Advisor Gutierrez, Dan D. October 1, 1992 700+ words
Code block basics. (Hardcore Clipper) (tutorial)
Magazine article from: Data Based Advisor Spence, Rick February 1, 1991 700+ words
Laying the foundation. (using Clipper 5.0's arrays and code blocks) (Clipper...
Magazine article from: Data Based Advisor de Lisle, Phil April 1, 1991 700+ words
A system model for dynamically reconfigurable software.
Magazine article from: IBM Systems Journal Whisnant, Keith Kalbarczyk, Zbigniew T. Iyer, Ravishankar K. March 1, 2003 700+ words
©2013 Gale, a part of Cengage Learning. All rights reserved. Contact us | Privacy policy | Terms and conditions

The AccessMyLibrary advertising network includes: womensforum.com GlamFamily