1 / 20

5.3 Sub Procedures, Part II

5.3 Sub Procedures, Part II. Passing by Value Passing by Reference Sub Procedures that Return a Single Value Debugging. ByVal and ByRef . Parameters in Sub procedure headers are proceeded by ByVal or ByRef ByVal stands for By Value ByRef stands for By Reference. Passing by Value .

thais
Download Presentation

5.3 Sub Procedures, Part II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 5.3 Sub Procedures, Part II • Passing by Value • Passing by Reference • Sub Procedures that Return a Single Value • Debugging

  2. ByVal and ByRef • Parameters in Sub procedure headers are proceeded by ByVal or ByRef • ByVal stands for By Value • ByRef stands for By Reference

  3. Passing by Value • When a variable argument is passed to a ByVal parameter, a copy of the value of the argument is passed. • After the Sub procedure terminates, the original value of the actual parameter that was passed is unchanged, even if the procedure changes the formal parameter value.

  4. Passing by Reference • When a variable argument is passed to a ByRef parameter, the actual parameter (argument) and the formal parameter are one and the same…they are the same location in memory. • After the Sub procedure terminates, the original value of the actual parameter that was passed will be changed when the formal parameter is changed.

  5. Example 5.3.1 Sub procedure has a ByVal parameter.

  6. About to call. Note the value in variable amt, the actual parameter (argument).

  7. In the called Sub procedure. Formal parameter num has a copy of actual parameter amt.

  8. Formal parameter num’s value has changed.

  9. But the value in amt remains the same. This is because amt and val are two different memory locations.

  10. Therefore, any change to a ByVal formal parameter has no effect on its associated actual parameter.

  11. Example 5.3.1 (modified) Let’s try the Sub procedure with a ByRef parameter.

  12. About to call. Note the value in variable amt, the actual parameter (argument).

  13. In the called Sub procedure. Formal parameter num is identical to actual parameter amt.

  14. Formal parameter num’s value has changed. Formal parameter num’s value has changed.

  15. Since num is a ByRef parameter, the value in amt changes also. This is because amt and val are the same memory location.

  16. Therefore, any change to a ByRef formal parameter remains in the associated actual parameter after the called procedure terminates.

  17. Example 5.3.2

  18. Example 5.3.2 Sub (or Function) procedures with ByRef parameters can be used to “return” values. If you want a procedure to “return” more than one value, you can use ByRef parameters for this.

  19. Example 5.3.3

  20. Example 5.3.3 If you want to swap the values in two variables, you need to have a temporary variable.

More Related