Chapter 4 General Procedures Chapter 4 Visual Basic

Chapter 4 General Procedures Chapter 4 - Visual Basic Schneider 1

Outline & Objective n n Creating Visual Basic Sub Procedures Creating programmer-defined Function Procedures Parameter Passing Modularizing in Programming Languages Chapter 4 - Visual Basic Schneider 2

What is Modularization n Breaking the program into smaller parts A Sub procedure or Function performs a well-defined task Easier to test, debug and correct Chapter 4 - Visual Basic Schneider 3

Modularizing Programs in Visual Basic n In Visual Basic, there are three types of procedures: n n Event Sub Function Note: To distinguish procedures from event procedures, Sub and Functions are referred to as general procedures. Chapter 4 - Visual Basic Schneider 4

Passing Arguments to Subs: n When you define a Sub procedure; sometimes you need to transfer variables that are used in different Subs. This is called passing in programming languages. Chapter 4 - Visual Basic Schneider 5

Sub Procedures Properties: n n may be called may be passed data called arguments may return values to the calling program may change the data stored in a received variable Chapter 4 - Visual Basic Schneider 6

Components of Sub Procedure: n n name: used to identify the Sub procedure parameters: a Sub procedure accepts values from the caller through its parameters; it may also send values back to the caller through it’s parameters. Chapter 4 - Visual Basic Schneider 7

Sub Procedure's Name n In this text, Sub procedure names begin with uppercase letters in order to distinguish them from variable names. Chapter 4 - Visual Basic Schneider 8

Syntax of a Sub Procedure Private Sub Procedure. Name ( ) statement(s) End Sub Chapter 4 - Visual Basic Schneider 9

Creating Visual Basic Sub Procedure: n n n Activate a code window Select Add Procedure from the Tools menu Type in the name of the Sub procedure Click on Private in Scope frame Press the Enter key or click the OK button Type the statements of the Sub procedure into this window Chapter 4 - Visual Basic Schneider 10

Example of Call to a Sub Procedure: Private Sub cmd. Compute_Click() Dim sng. Num As Single sng. Num = Val(Input. Box("Enter a number: ")) Call Triple(sng. Num) End Sub Chapter 4 - Visual Basic Schneider 11

Sub Procedure Triple: Private Sub Triple(num As Single) ' Multiply the value of the number by 3 pic. Result. Print "The number is"; 3 * num End Sub Chapter 4 - Visual Basic Schneider 12

Passing Arguments to Sub Procedures n n Arguments : Variables or expressions placed in parentheses in a Call statement. Not only is the value of the argument passed to the parameter, but the value of the parameter is passed back to the argument. Chapter 4 - Visual Basic Schneider 13

Parameters n n Variables placed in parentheses after a Sub Procedure's name. When the procedure is called, the values of the corresponding arguments are placed in the parameters. Chapter 4 - Visual Basic Schneider 14

Example of Parameters n Private Sub Triple(num As Single) Chapter 4 - Visual Basic Schneider 15

Passing arguments to parameters Argument Call Triple(num ) Private Sub Triple (num As Single) Parameter Chapter 4 - Visual Basic Schneider 16

Passing Arguments to Parameters Arguments n n Call Add (x, y ) Private Sub Add ( num 1 As Single, num 2 As Single) Parameters Chapter 4 - Visual Basic Schneider 17

Passing Arguments n The Sub Procedure receives the location of the arguments, the Sub Procedure may use and modify the value of the arguments. Chapter 4 - Visual Basic Schneider 18

Important Rules for Passing Arguments to a Sub n n The number of arguments and parameters must match. The data type of each argument must match its corresponding parameter. Chapter 4 - Visual Basic Schneider 19

Review n Visual Basic has three types of procedures: n n n Event Sub Function Each Sub procedure performs a distinct task. The Call statement causes a Sub procedure to be executed. Chapter 4 - Visual Basic Schneider 20

Review n n Values can be passed between the calling program and Sub by passing arguments. The number and type of arguments in the calling program and Sub must match. Chapter 4 - Visual Basic Schneider 21

Review n n Variables that are used in a particular Sub are local variables. Values that are assigned to them are not returned to the calling module. Chapter 4 - Visual Basic Schneider 22

Common Errors n n Passing incorrect data types. Not returning the result of the computation back to the calling program. Chapter 4 - Visual Basic Schneider 23

What is a function? n n A function designed to perform a specific task also. A function designed to return a single value to the calling program. Chapter 4 - Visual Basic Schneider 24

Types of Functions n n Standard functions (built-in) programmer-defined functions Chapter 4 - Visual Basic Schneider 25

programmer-defined Function n A function designed to return a single value. The value is returned in the function itself. The arguments of a function should not be changed in the function body. Chapter 4 - Visual Basic Schneider 26

The Function Syntax Private Function. Name (parameter-list) As datatype Statement(s)…… …. . Function. Name = ……. . End Function Chapter 4 - Visual Basic Schneider 27

Example of a Function (using a function to change from Fahrenheit to Celsius) Private Sub cmd. Convert_Click() pic. Temp. C. Cls pic. Temp. C. Print Fto. C(Val(txt. Temp. F. Text)) End Sub Private Function Fto. C(t As Single) As Single ‘ Convert Fahrenheit temperature to Celsius Fto. C = (5 / 9) * (t - 32) End Function Chapter 4 - Visual Basic Schneider 28

Rule for Defining and Calling a Function n programmer-defined function must include a statement that assigns the function name a value. programmer-defined functions are called in the same way that built-in functions are called. A programmer-defined function may be called in an expression. Chapter 4 - Visual Basic Schneider 29

Common Errors n n n Passing incorrect data types Not specifying the data type of the returned value Forgetting the data type of a function's parameter Chapter 4 - Visual Basic Schneider 30

Common Errors n n n Not assigning a value to the function name inside the function definition Misspelling of the Function name Wrong invoking of the function in an expression Chapter 4 - Visual Basic Schneider 31
- Slides: 31