80 likes | 207 Views
Sub Procedures. A set of statements that perform specific tasks. The use of sub procedures is good programming style because a program is divided into smaller, more manageable blocks of code.
E N D
Sub Procedures • A set of statements that perform specific tasks. • The use of sub procedures is good programming style because a program is divided into smaller, more manageable blocks of code. • There is less code redundancy because the statements for a specific task need only appear once in a single Sub procedure.
Sub Procedures cont… • A Sub procedure takes the form: Sub ProcedureName() statements End Sub • ProcedureName is a name describing the task performed by the procedure. • Statements is one or more statements that perform the task. • Sub declares the procedure and End Sub ends the procedure.
Calling a Procedure • A Sub procedure must be called from another procedure in order to execute. • The Call statement takes the form: CallProcedureName() • ProcedureName is the Sub procedure name followed by parentheses. • The parentheses indicate a procedure.
Value Parameters • Data is given, or passed, to a procedure by enclosing it in parentheses in the procedure call. • Example: Call GiveHint(intSecretNumber, intGuess) • A variable or value passed to a procedure is called an argument. In the statement, intSecretNumber and intGuess are the arguments to be used by the Sub procedure GiveHint.
Value Parameters cont… • A procedure that requires arguments is declared with parameters and takes the following form: SubProcedureName(ByValparameter1Astype, …) statements End Sub • ProcedureName is the name describing the procedure. • ByVal indicates that the parameter is a value parameter, paremeter 1 is the name of the parameter, and type is the data type of the expected value. • A value parameter is only used as a local variable by the called procedure.
Reference Parameters • A procedure that sends values back to the calling procedure uses reference parameter. • Reference parameters can alter the value of the actual variables used in the procedure call.
Reference Parameters cont… • A procedure with reference parameters takes the following form: SubProcedureName(ByRefparameter1Astype,…) statements End Sub • ProcedureName is the name describing the procedure. • ByRef indicates that the parameter is by reference, parameter1 is the name of the parameter, and type is the data type of the parameter. • There can be many parameters separated by commas. • A procedure can have both reference (ByRef) and value (ByVal) parameters. statements is the body of the procedure.