Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

68
Chapter 4 - Visual Basic Schneider 1 Chapter 4 General Procedures
  • date post

    20-Dec-2015
  • Category

    Documents

  • view

    251
  • download

    2

Transcript of Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Page 1: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 1

Chapter 4

General Procedures

Page 2: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 2

Outline & Objective

Sub Procedures Procedure Parameters Function Procedures

Page 3: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 3

What is a procedure?

A general procedure is a set of commands that is given a name so that it can be invoked by another part of the program

Procedures make a program easier to develop, test, and correct

Page 4: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 4

General Procedures

In Visual Basic, there are two types of procedures: •Sub

•Function Note: To distinguish procedures from event procedures, Sub

and Function procedures are referred to as general procedures.

Page 5: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 5

Sub Procedures Properties:

can be called (invoked) by using•call subProcedureName()

passing data called arguments

Page 6: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 6

Creating Visual Basic Sub Procedure:

Activate the code window Select Add Procedure from the Tools

menu Type in the name of the Sub procedure Click on Private in the Scope box Press the Enter key or click the OK

button Type the statements of the Sub

procedure into the code window

Page 7: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Syntax of a Sub Procedure

Private Sub ProcedureName ( ) statement(s)End Sub

Procedure Name

Statements

Example

Page 8: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 8

Components of Sub Procedure:

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

Page 9: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 9

Sub Procedure's Name

The rules for naming Sub Procedures are the same as naming variables.

In this text, Sub procedure names begin with uppercase letters in order to distinguish them from variable names.

Page 10: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Example

Page 11: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Example

Page 12: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

12

Passing Arguments

A Sub procedure receives the location of the arguments, and may use and modify the value of the arguments stored at that location.

Page 13: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 13

Passing arguments to parameters

Arguments

Arguments : Variables or expressions placed in parentheses in a Call statement.

Page 14: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

14

Passing arguments to parameters

Parameters

Parameters : Variables appearing in the sub procedure definition are called paramanters.

Page 15: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

15

Passing Arguments to Parameters

Call Add (x, y )

Private Sub Add ( num1 As Single, num2 As Single)

When the Call Statement is executed , each value of the arguments is assigned to it’s corresponding parameter variable

the value of num1 is assigned to x and the value of num2 is assigned to y

Parameters

Arguments

Page 16: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 16

Passing arguments to parameters

Call Triple(num)

Private Sub Triple (num As Single)

Argument

Parameter

Page 17: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 17

Sub Procedure Triple:

Private Sub Triple(num As Single) ' Multiply the value of the number by 3 picResult.Print "The number is"; 3 * numEnd Sub

Private Sub Triple(par As Single) ' Multiply the value of the number by 3 picResult.Print "The number is"; 3 * parEnd Sub

Private Sub cmdCompute_Click() Dim num As Single num = Val(InputBox("Enter a number:")) Call Triple(num)End Sub

cmdCompute Triple

num par/num

Page 18: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Example

Page 19: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

ExamplecmdDisplay Triple

amt num

Page 20: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Passing by value

Sometime you want to pass a variable to a sub procedure, but you want to ensure that the variable will retain its original value after the sub procedure terminates.

Such variable is said to be passed by value

Page 21: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Passing by value

There are two ways to pass the variable by value

1. In the call statements, enclose the variable in an extra pairs of parenthesis

cmdDisplay Triple

amt

num

Page 22: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

2. In the private Sub statement, precede the corresponding parameter with the word ByVal

cmdDisplay Triple

amt

num

Page 23: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Passing by value When the variable is passed by value, the

variable will retain its original valuecmdDisplay Triple

amt

num

Page 24: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

ByRef and ByVal Examples

Private Sub cmdCall_Click()Dim a As IntegerDim b As Integera = 10b = 15Print a; bCall mySub(a, b)Print a; bEnd Sub

Sub mySub(x As Integer, y As Integer) x = 11 y = 16End Sub

Output10 1511 16

cmdCall mysub

a x

b y

Page 25: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

ByRef and ByVal Examples

Private Sub cmdCall_Click()Dim a As IntegerDim b As Integera = 10b = 15Print a; bCall mySub(a, b)Print a; bEnd Sub

Sub mySub(ByRef x As Integer, ByRef y As Integer) x = 11 y = 16End Sub

Output10 1511 16

cmdCall mysub

a x

b y

Page 26: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

ByRef and ByVal Examples

Private Sub cmdCall_Click()Dim a As IntegerDim b As Integera = 10b = 15Print a; bCall mySub(a, b)Print a; bEnd Sub

Sub mySub(ByRef x As Integer, ByVal y As Integer) x = 11 y = 16End Sub

Output10 1511 15

cmdCall mysub

a x

b

y

Page 27: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

ByRef and ByVal Examples

Private Sub cmdCall_Click()Dim a As IntegerDim b As Integera = 10b = 15Print a; bCall mySub(a, b)Print a; bEnd Sub

Sub mySub(ByVal x As Integer, ByVal y As Integer) x = 11 y = 16End Sub

Output10 1510 15

cmdCall mysub

a

b

x

y

Page 28: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

ByRef and ByVal Examples

Private Sub cmdCall_Click()Dim a As IntegerDim b As Integera = 10b = 15Print a; bCall mySub(a, b)Print a; bEnd Sub

Sub mySub(ByRef y As Integer, ByRef x As Integer) x = 11 y = 16End Sub

Output10 1516 11

cmdCall mysub

a y

b x

Page 29: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 29

Important Rules for Passing Arguments to a Sub

The number of arguments and parameters must match.

The data type of each argument must match its corresponding parameter.

Page 30: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 30

Local Variables:

A variable that is used only in a specific procedure (Sub or Function).

The scope of the local variable is the Sub or Function in which that variable has been defined.

Page 31: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 31

Local Variables:

Declared within a procedure definition Private to a procedure definition Variables in different procedures are totally

independent Different procedures can have variables with the same

names; however, each variable will have its own memory location

Page 32: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 32

Advantages of Local Variables

Extremely useful for team programming

Protects against accidental side effects (change of the value of the variable in one procedure that causes an error in another)

Page 33: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Example

The variables x and y are local for the procedure cmdDisplay_Click

The variables x and y are local for the procedure pr1

cmdDisplay pr1

x

y

x

y

Page 34: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Any change of the value of parameters (x and y) will affect only on the value of its corresponding arguments (a and b) in the caller procedure

cmdCall mysub

a x

b y

x

y

z

Local variables

Page 35: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 35

Form-Level Variables

Form-level variables are visible to every procedure.

Form-level variables appear at the top of the code window.

Page 36: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 36

How to create Form-Level Variables?

Activate the code window Click on the down arrow to the right of the

object list box Click on General Click on Declaration in the procedure list box Type in Dim statements for form-level

variables

Page 37: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

The variables x and y are Form-Level variables (Global)

Note: The values of Form-level variables are visible to all procedures in the program

cmdCall mysub

X Global X Global

y Global y Global

Page 38: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

If the procedure has a local variables with the same name of the form-level variables, the procedure uses its local variable instead of the global variable

yx

x

y

0 0

05

6

0

7

8

Form_level Variables

Local

LocalThe procedure uses the global variable if it has no local variable

Page 39: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Examples

23 23 23 23 23 23

Page 40: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Form_load

In some situation we want to assign the value immediately to the Form-level variable

Visual basic has a special event procedure called Form_Load that is executed as soon as the program is run.

Page 41: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Example

Command1 pro1

X Global X Global

y Global y Global / b

x a

Page 42: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 42

What is a function?

Similar to a Sub, performs a specific task

Unlike a Sub, returns a single value to the calling program

Page 43: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Types of Functions

Standard functions (built-in)•VBasic has many bulit-in functions

(Predefined functions).

•E.g. (Left, Right, Len, Sqr, Round, …) User-defined functions: In addition to

using bulit-in functions, we can define functions of our own. These new function are called function procedures or user-defined functions

Page 44: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Built-in Functions

Print Len(“Hello”) 5 Print Left(“Hello”,3) “Hel” Print Sqr(9) 3

For example, the function Len returns the number of characters in the string. It has One parameter of type String and returns only one value of type Single

Page 45: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 45

User-Defined Function

A function returns a single value The value is returned by assigning a

value to the name of the function The values of the parameters of a

function should not be used for returning values from a function

Page 46: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 46

The Function Syntax

Private Function FunctionName(parameter-list) As dataType

Statement(s)…… ….. FunctionName = valueEnd Function Note: value must be of the type dataType specified in

the function declaration

Page 47: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 47

Rules for Defining and Calling a Function

User-defined function must include a statement that assigns the function name a value.

User-defined functions are called in the same way that built-in functions are called.

A user-defined function may be called in an expression.

Page 48: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Example of a Function

The function Max has 2 parameters of type single

What is the data type of the returned value?

Page 49: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 49

Example of a Function

Private Sub cmdDetermine_Click() Dim nom As String nom = FirstName(txtFullName.Text) picFirstName.Print "The first name is “; nom End Sub

Private Function FirstName(nom As String) As String Dim firstSpace As Integer firstSpace = InStr(nom, " ") FirstName = Left(nom, firstSpace - 1)End Function

Page 50: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 50

Another Example

Private Sub cmdDisplay_Click() Dim num As Single num = 5 picOutput.Print Triple (num) picOutput.Print numEnd Sub

Private Function Triple(x As Single) As Single Dim num As Single num = 3 Triple = num * xEnd Function

Output: 15 5

Page 51: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Example

Private sub com_click()x=fun(3) + fun( 5)Print x End sub

Private function fun(a)fun= aa=a+1fun=fun + aEnd function

18

Page 52: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Example

Private sub com_click()x=fun(3, fun(2, 1)) Print x End sub

Private function fun(a,b) as integera=a+bEnd function

0

Page 53: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Difference between calling sub-procedure and functions

To call a sub-procedure•Type the word Call followed by the

procedure name, with the arguments (if any) in parentheses

•Call pro1(5,3)

•After the procedure statements are executed, the execution continues with the line following the call statements.

Page 54: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Difference between calling sub-procedure and functions

To call a sub-procedure

Page 55: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Difference between calling sub-procedure and functions

To call a function procedure•Use its name, with the arguments (if any) in

parentheses EX: Fun(5,3)

•A function is called in an expression.

•Expression may be part of an assignment statement or an output statement

•Print Fun(5,6)

•X = 2*Fun(10,3)+3

•Print Fun(x,y)+Fun(x+1,x+2)

Page 56: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

After the function procedure statements are executed, the execution returns back to the calling point. And the function call statement (Max(10,2)) is substituted by the returned-value (10).

10

Page 57: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Any function can be rewritten as a sub and vise versa

Page 58: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 58

Common Errors

Passing incorrect data types Not specifying the data type of the

returned value Not assigning a value to the Function

name inside the Function definition Misspelling of the Function name

Page 59: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Arithmetic Operations

Operator Operation Basic expression

^ Exponentiation A ^ B * Multiplication A * B / Division A / B + Addition A + B - Subtraction A - B

The VBasic arithmetic operations are addition, subtraction, multiplication, division and Exponentiation

Page 60: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

More Arithmetic Operators

In VB, there are many other arithmetic operators such as:

Integer division (Uses the backslash, \)•(num1 \ num2) is the Quotient of num1 divided

by num2 (the integer part of the result of dividing two integers )

Remainder: (num1 Mod num2) is the whole number remainder of num1 divided by num2

17 / 2 = 8.5 17 \ 2 = 8

17 Mod 2 = 1

16 / 2 = 8 16\ 2 =8 16 Mod 2 = 0

34/7= 4.8571428571286

34 \ 7 = 4

34 Mod 7 = 6

Page 61: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Mod Rules

Chapter 4 - Visual Basic Schneider 61

The whole number remainder If the numbers are single they are rounded

first If one or both of a and b are negative the

result of the Mod will have the sign of “a” num1 Mod 1 = 0 num1 Mod num1 = 0 Num Mod 0 Division by zero Result of Mod should be less than num2

Page 62: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Mod Examples

Chapter 4 - Visual Basic Schneider 62

25 mod 718.7mod 3.2-35 mod 4-35 mod -4 35 mod -4-18.7 mod 3.2

= 4= 1= -3= -3= 3

= -1-18.7 mod -3.2 = -118.7 mod -3.2 = 13 mod 3 = 03 mod -3 = 0-3 mod 3 = 0-3 mod -3 = 0

3 mod 1 = 0-3 mod 1 = 0

Page 63: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Order of Precedence All operations inside of () are evaluated first

^ is evaluated next

* and /are at the same level of precedence and are evaluated next

\ (integer division) is evaluated next

Mod is evaluated next

+ and – have the same level of precedence and are evaluated last

Page 64: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Order of Precedence

When operators are on the same level

• Performed from left to right

Page 65: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

What is the output of the following expression?

2+3^2+(2+5*2)/3 Mod 3 + 3 2+3^2+(2+10)/3 Mod 3 + 3 2+3^2+12/3 Mod 3 + 3 2+9+12/3 Mod 3 + 3 2+9+4 Mod 3 + 3 2+9+1 + 3 15

There are 3 arithmetic operators (+ , / and Mod).

Which operator has the highest priority?

Page 66: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

1

Chapter 4 - Visual Basic Schneider 66

Private Sub cmd_Click() Dim a As Integer Dim b As Integer a = 14 b = 5 picOutput.Print a / b \ bEnd SubOutput:1. 2.82. 03. 24. 45. Infinity /Illegal Division

Page 67: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

2.

Chapter 4 - Visual Basic Schneider 67

Private Sub cmd_Click() Dim a As Integer Dim b As Integer a = 14 b = 5 picOutput.Print a mod b \ bEnd SubOutput:1. 2.82. 03. 24. 45. Infinity /Illegal Division

Page 68: Chapter 4 - Visual Basic Schneider1 Chapter 4 General Procedures.

Chapter 4 - Visual Basic Schneider 68

More Examples

Private Sub cmd_Click() Dim a As Integer Dim b As Integer a = 14 b = 5 Print a / b Print a \ b Print a Mod bEnd Sub

Output:2.824