Page

5.5.0- Jumping Structures and the Art of Reusing Code

  by NT Community Manager.
Last Updated  by NT Community Manager.  

PublicCategorized as 05. ASP Control Structures.

Not tagged.
<< 5.4.3- Looping ControlsChapter55.5.1- Procedures >>

Jumping Structures and the Art of Reusing Code

As you write more ASP code you'll find that you want to use the same code in more than one place. ASP allows you to write some code once, then run it as many times as needed to support the main body of code. These mini-programs are called procedures or functions. We will want ASP to jump away from execution of the main body of code, run through the commands of a procedure or function, before returning to execute the main body of code.

 

For example, you may have a few lines of code that insert some lines of text about how to contact the sales department. You would like to have this show up in various places on the page, but want to avoid having to rewrite the code separately each time. The solution is to put the lines of code into a procedure (sometimes called a Sub Procedure or a Sub). Then every place that you want that code to run you can invoke the subprocedure rather than rewrite the code.

 

A second example is to calculate the delivery date of a shipment. You may have to do this several times for different items on a page. In this case you want to start with information such as the ship date, and from that calculate the delivery date. There may be some branching in the procedure to accommodate the fact that there is no delivery on Sunday.

 

There are two types of procedures:

 

  • Subprocedures carry out an action. For example, a Sub would be used to carry out the actions of putting text onto a page. A sub procedure does not return values, but can be used to set variables that are in scope.
  • Functions carry out action statements and return an answer to your code. A function would be used to calculate a delivery date and return that answer to your main program.
<< 5.4.3- Looping ControlsChapter55.5.1- Procedures >>

Copyright © 2003 by Wiley Publishing, Inc.

Powered by Near-TimeTerms of Services | Privacy Policy | Security Policy |