Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN...
-
date post
22-Dec-2015 -
Category
Documents
-
view
214 -
download
2
Transcript of Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN...
![Page 1: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.](https://reader030.fdocuments.in/reader030/viewer/2022032523/56649d775503460f94a59a99/html5/thumbnails/1.jpg)
Promoting Code Reuse
Often in programming, multiple procedures will perform the same operation
IN OTHER WORDS – the same piece of code will do the same thing somewhere else in the application
Rather than retyping the code twice (or even more than twice….), code can be written in a general
(sub/function) procedure and can be called into use from other procedures (therefore promoting
reuse)
![Page 2: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.](https://reader030.fdocuments.in/reader030/viewer/2022032523/56649d775503460f94a59a99/html5/thumbnails/2.jpg)
Promoting Code ReuseClick each command button triggering the click event procedure associated with each one
![Page 3: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.](https://reader030.fdocuments.in/reader030/viewer/2022032523/56649d775503460f94a59a99/html5/thumbnails/3.jpg)
Promoting Code Reuse
Now when we click on the hello command button we trigger the click event procedure associated with that command button but also call the other procedures which execute in turn producing the same output
![Page 4: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.](https://reader030.fdocuments.in/reader030/viewer/2022032523/56649d775503460f94a59a99/html5/thumbnails/4.jpg)
Creating a ‘Sub Procedure’
The coding of a new procedure is similar to the other event procedures that we have been coding,
but is not in itself attached to any event
Therefore, this code cannot be executed unless a CALL is specified from another procedure, for the
General Sub Procedure
To call a Sub Procedure, just give the procedure name
![Page 5: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.](https://reader030.fdocuments.in/reader030/viewer/2022032523/56649d775503460f94a59a99/html5/thumbnails/5.jpg)
Creating a ‘Sub Procedure’
• 2 ways to create a General Sub Procedure
• [1] simply type in the procedure definition in the code window
• [2] use the tools / add procedure menu command
![Page 6: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.](https://reader030.fdocuments.in/reader030/viewer/2022032523/56649d775503460f94a59a99/html5/thumbnails/6.jpg)
Type sub myprocedure and press return on the keyboard and the procedure is created
![Page 7: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.](https://reader030.fdocuments.in/reader030/viewer/2022032523/56649d775503460f94a59a99/html5/thumbnails/7.jpg)
Very Important - visibility• Sub procedures are by default Public in all
modules, which means they can be called from anywhere in the application
• So what is the scope of this newly created procedure?
![Page 8: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.](https://reader030.fdocuments.in/reader030/viewer/2022032523/56649d775503460f94a59a99/html5/thumbnails/8.jpg)
Very Important - visibility
So you should put the PRIVATE keyword in front of the sub keyword to reduce the procedures visibility
![Page 9: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.](https://reader030.fdocuments.in/reader030/viewer/2022032523/56649d775503460f94a59a99/html5/thumbnails/9.jpg)
tools / add procedure menu command
![Page 10: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.](https://reader030.fdocuments.in/reader030/viewer/2022032523/56649d775503460f94a59a99/html5/thumbnails/10.jpg)
Creating a ‘Sub Procedure’
The Sub statement declares the name, arguments, and code that will form the body of a subroutine / sub procedure as follows:
[Private | Public] Sub ProcedureName [(arguments)]
statements
End Sub
![Page 11: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.](https://reader030.fdocuments.in/reader030/viewer/2022032523/56649d775503460f94a59a99/html5/thumbnails/11.jpg)
Advantages of a ‘Sub Procedure’
Code is easier to write and debug - test individual modules
Code is maintainable
Improves code structure
Decreases the size of individual procedures and often the size of the program itself
Disadvantages of a ‘Sub Procedure’
Requires extra preparatory work
Need to understand logical design of code structure
May require more CPU time and memory
![Page 12: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.](https://reader030.fdocuments.in/reader030/viewer/2022032523/56649d775503460f94a59a99/html5/thumbnails/12.jpg)
Calling a ‘Sub Procedure’
The use of the keyword Call is optional but required in certain cases:
ProcedureName
Call ProcedureName
Call ProcedureName ( )
An Event Procedure can also be called
Command1_Click
Call Command1_Click
Call Command1_Click ( )
![Page 13: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.](https://reader030.fdocuments.in/reader030/viewer/2022032523/56649d775503460f94a59a99/html5/thumbnails/13.jpg)
Scope of Variables: General Sub Procedures
Example 1
![Page 14: Promoting Code Reuse Often in programming, multiple procedures will perform the same operation IN OTHER WORDS – the same piece of code will do the same.](https://reader030.fdocuments.in/reader030/viewer/2022032523/56649d775503460f94a59a99/html5/thumbnails/14.jpg)
Example 1Scope of Variables:
General Sub Procedures