1 / 7

Sub Procedures

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.

kinsey
Download Presentation

Sub Procedures

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. 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.

  2. 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.

  3. 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.

  4. 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.

  5. 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.

  6. 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.

  7. 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.

More Related