Microsoft KB Archive/101025: Difference between revisions

From BetaArchive Wiki
(importing KB archive)
 
m (Text replacement - """ to """)
Line 68: Line 68:
   mvar3=ADDIT(mvar1)  && Passing mvar1 as a parameter: by value
   mvar3=ADDIT(mvar1)  && Passing mvar1 as a parameter: by value
   ?
   ?
   ?"Value of memory variables after the routine is executed"
   ?"Value of memory variables after the routine is executed"
   DISP MEMO LIKE m*
   DISP MEMO LIKE m*
   ?
   ?
   ?"The value of mvar1 is unchanged"
   ?"The value of mvar1 is unchanged"


   FUNCTION ADDIT
   FUNCTION ADDIT
Line 77: Line 77:
   mpar1=mpar1+1    && Copy of mvar1 is incremented
   mpar1=mpar1+1    && Copy of mvar1 is incremented
   mvar3=mpar1+1    && Variable to be returned to the calling program.
   mvar3=mpar1+1    && Variable to be returned to the calling program.
   ?"Value of memory variables in the routine"
   ?"Value of memory variables in the routine"
   DISPLAY MEMORY LIKE m* && mvar1=1 and mpar1=1
   DISPLAY MEMORY LIKE m* && mvar1=1 and mpar1=1
   RETURN mvar3      && Value returned to the calling program mvar3
   RETURN mvar3      && Value returned to the calling program mvar3
Line 100: Line 100:
   DO ADDIT WITH mvar1  && Passing mvar1 as a parameter by reference
   DO ADDIT WITH mvar1  && Passing mvar1 as a parameter by reference
   ?
   ?
   ?"Value of memory variables after the routine is executed"
   ?"Value of memory variables after the routine is executed"
   DISPLAY MEMORY LIKE m*
   DISPLAY MEMORY LIKE m*
   ?
   ?
   ?"The value of mvar1 is modified"
   ?"The value of mvar1 is modified"


   PROCEDURE  ADDIT
   PROCEDURE  ADDIT
   PARAMETER mpar1
   PARAMETER mpar1
   mpar1=mpar1+1            && Reference to mvar1 is incremented
   mpar1=mpar1+1            && Reference to mvar1 is incremented
   ?"Value of memory variables in the routine"
   ?"Value of memory variables in the routine"
   DISPLAY MEMORY LIKE m*  && mvar1=2 and mpar1= @mvar1
   DISPLAY MEMORY LIKE m*  && mvar1=2 and mpar1= @mvar1
                 </pre>
                 </pre>
Line 124: Line 124:
== REFERENCES ==
== REFERENCES ==


&quot;Language Reference,&quot; version 2.5, page L3-1012<br />
"Language Reference," version 2.5, page L3-1012<br />
<br />
<br />
&quot;Using FoxPro Version 2,&quot; Slater and Arnott, pages 613-614, Que, 1992
"Using FoxPro Version 2," Slater and Arnott, pages 613-614, Que, 1992


</div>
</div>

Revision as of 09:19, 20 July 2020

Article ID: 101025

Article Last Modified on 12/3/2003



APPLIES TO

  • Microsoft Visual FoxPro 3.0 Standard Edition
  • Microsoft FoxPro 2.5b
  • Microsoft FoxPro 2.5a
  • Microsoft FoxPro 2.0
  • Microsoft FoxPro 2.5b for MS-DOS
  • Microsoft FoxPro 2.5a



This article was previously published under Q101025

SUMMARY

By default, FoxPro passes parameters to procedures (subroutines called with a DO <subroutine> command) by reference and parameters to functions [subroutines called with a =<subroutine>() or ?<subroutine>() command] by value. The information below describes how parameters are passed to subroutines.

MORE INFORMATION

When parameters are passed by value, a copy of the original variable is made in the routine. The variable declared in the subroutine is a different variable from the original variable. When control is passed back to the calling program, the variables declared in the routine are released and the value of the original variable is not changed.

When parameters are passed by reference, a reference to the original variable is made in the routine. Any change made to the variable in the subroutine is reflected in the calling program.

Passing Parameters to User-Defined Functions

By default, parameters are passed by value to user-defined functions. In essence, any change made to the variables declared as parameters in the subroutine will not affect the original variables in the calling program. For example:

   * Main program
   CLEAR
   mvar1=1
   mvar2=2
   mvar3=ADDIT(mvar1)   && Passing mvar1 as a parameter: by value
   ?
   ?"Value of memory variables after the routine is executed"
   DISP MEMO LIKE m*
   ?
   ?"The value of mvar1 is unchanged"

   FUNCTION ADDIT
   PARAMETER mpar1
   mpar1=mpar1+1    && Copy of mvar1 is incremented
   mvar3=mpar1+1     && Variable to be returned to the calling program.
   ?"Value of memory variables in the routine"
   DISPLAY MEMORY LIKE m* && mvar1=1 and mpar1=1
   RETURN mvar3      && Value returned to the calling program mvar3
                

To pass a parameter by reference to a user-defined function, use the at sign (@) before the variable passed. For example:

   mvar3=ADDIT(@mvar1)
                

To change the default and pass all the parameters by reference, use SET UDFPARMS command. For example:

SET UDFPARMS TO REFERENCE


Passing Parameters to Procedures

By default, FoxPro passes parameters by reference to procedures. Any changes made to variables in the subroutine will be made to the original variable. Additionally, FoxPro hides the original variables passed as parameters in the subroutine. For example:

   * Main program
   CLEAR
   mvar1=1
   mvar2=2
   DO ADDIT WITH mvar1  && Passing mvar1 as a parameter by reference
   ?
   ?"Value of memory variables after the routine is executed"
   DISPLAY MEMORY LIKE m*
   ?
   ?"The value of mvar1 is modified"

   PROCEDURE  ADDIT
   PARAMETER mpar1
   mpar1=mpar1+1            && Reference to mvar1 is incremented
   ?"Value of memory variables in the routine"
   DISPLAY MEMORY LIKE m*   && mvar1=2 and mpar1= @mvar1
                

To pass a parameter to a procedure by value, use parentheses around the variable. For example:

DO ADDIT WITH (mvar1)


REFERENCES

"Language Reference," version 2.5, page L3-1012

"Using FoxPro Version 2," Slater and Arnott, pages 613-614, Que, 1992


Additional query words: VFoxWin FoxDos FoxWin 2.x array

Keywords: kbenv KB101025