Procedures
|
|
The best way to develop and maintain a large program is to construct it from smaller pieces, which are more manageable than the original program, i.e., divide and conquer. Programmer-defined procedures define specific tasks that may be used at many points in a program. The statements that make up the procedure are written only once and these statements are not visible to other procedures. There are several motivations for modularizing a program into procedures:
Modules A project is made up of modules--such as form modules.
A procedure is invoked by a procedure call, which specifies the procedure name and provides information (as arguments) that the called procedure needs to do its job. Procedures allow the programmer to modularize a program. A common analogy for this is the hierarchical form of management. A boss (the caller) asks a worker (the procedure) to perform a task and return (i e., report back) the results when the task is done. The boss does not know how the worker performs its designated tasks. The worker may call other workers--the boss will be unaware of this
You've seen examples of subs prior to this. For example, when we were exploring error checking the notes provided a sub procedure designed to allow the user to correct invalid input more easily. The sub accepts as an argument the name of the textbox in which the invalid input was entered, and then returns program focus to that textbox, highlighting the invalid entry in the text box so that the user does not have to erase the existing entry before entering the correct value.
Each time that the user is required to re-enter data, this sub can be called by passing the text box name as a parameter, such as Call textHighlighter(txtTempF). Sub Procedure Definition The code representing the procedure is called the Sub procedure definition.
________________ Sub Procedure Header Sub sub_name ( ByVal/ByRef parameter list) example: Private Sub textHighlighter (ByVal txtVar As TextBox) or more detailed
________________ Sub Procedure Body Any declarations and statements the programmer places between the header and End Sub form the Sub-procedure body. Every time the Sub procedure is called (or invoked) the body is immediately executed. Execution of the Sub procedure terminates when End Sub is reached and program execution then continues with the statement immediately following the call.
________________ Procedure Calls A procedure call uses keyword Call and encloses the arguments passed in a set of parentheses. Procedure ShowPay is called with the line
which passes into ShowPay values for parameter hours (40) and parameter wage (10.00). The arguments passed can be variable names as well:
Procedure arguments, both for subs and functions, are matched up based on the number, position, and type in the argument list. This is often referred to as the procedure's signature, or external interface. ________________ Note also that procedures can also be invoked without the statement, but that is not the preferred approach. Unlike Sub procedures, which do not return a value, Function procedures return a value to the calling routine. ________________ Function Procedure Header
or more detailed
Private Function getArea (ByVal sl As Single, ByVal s2 As Single) As Single Private Function isVolunteer08 ( ) As Boolean ________________ Example The example below is a modification of the day of week program discussed earlier. This example uses a programmer-defined Function procedure dayOfWeek to determine the corresponding week day name of a particular date. Note that the dayOfWeek function calls two built-in VB functions. The result is returned through the function name.
________________ Function Procedure Body Any declarations and statements the programmer places between the header and End Function form the Function procedure body. Every time the Function procedure is called (or invoked) the body is immediately executed. ________________ Returning Values from Functions
Consider the following Function procedure:
________________
Note: Better written as Return idNumberValid ________________ Function Call When a Function procedure name is encountered at run-time, the Function procedure is called, causing its body statements to execute. After a Function is executed, control returns (along with the value returned) to the calling statement returnValue = isVolunteer08( ) Program execution then continues with the next statement after the call. getArea is called with the statement
IsVolunteer08 is invoked with the line
Be sure not to declare a local variable with the same name as a sub or function.
|
|
Methods A method is any procedure that is contained within a class. You will see more details about methods when we introduce object-oriented programming.
|