+ All Categories
Home > Documents > Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

Date post: 08-Jan-2018
Category:
Upload: everett-leonard
View: 221 times
Download: 0 times
Share this document with a friend
Description:
Procedures You Can Write Your Own General Purpose Procedures That Perform Specific Tasks General Purpose Procedures Are Not Triggered by Events but Called From Statements in Other Procedures
20
Week Procedures And Functions 7 A procedure is a collection of statements that performs a task
Transcript
Page 1: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

WeekProcedures And Functions7

A procedure is a collection of statements that performs a task

Page 2: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

2

Introduction A procedure is a collection of statements that

performs a task Event handlers are a type of procedure

A function is a collection of statements that performs a task and returns a value to the VB statement that executed it Functions work like intrinsic functions, such as CInt and IsNumeric

A method can be either a procedure or a function

Page 3: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

Procedures

You Can Write Your Own General Purpose Procedures That Perform Specific Tasks

General Purpose Procedures Are Not Triggered by Events but Called From Statements in Other

Procedures

Page 4: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

4

Procedure Uses

An event handler is a type of procedure Automatically executed when an event such as

a mouse click occurs General purpose procedures are triggered by

statements in other procedures, not by events Procedures help simplify & modularize code by:

Breaking it into small, manageable pieces Performing a task that is needed repeatedly Dividing a program into a set of logical tasks

Page 5: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

5

Sample Procedure

Sub DisplayMessage()'A Sub procedure that displays a message.lstOutput.Items.Add("")lstOutput.Items.Add("Hello from DisplayMessage.")lstOutput.Items.Add("")

End Sub

Private Sub btnGo_Click(ByVal sender As System.Object, _ByVal e As System.EventArgs) Handles btnGo.Click' This procedure calls the DisplayMessage procedure.lstOutput.Items.Add("Hello from btnGo_Click procedure.")lstOutput.Items.Add("Calling the DisplayMessage " & _

"procedure.")DisplayMessage() lstOutput.Items.Add("Now I am back ” _

& “in the btnGo_Click procedure.") End Sub Calls

DisplayMessage procedure

Returns to btnGo_Click

Page 6: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

6

Declaring a Procedure

AccessSpecifier is optional and establishes accessibility to the program

Sub and End are keywords ProcedureName used to refer to procedure

Use Pascal casing, capitalize 1st character of the name and each new word in the name

ParameterList is a list of variables or values being passed to the sub procedure

[AccessSpecifier] Sub ProcedureName ([ParameterList])[Statements]

End Sub

Page 7: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

7

More on the Access Specifier Private allows use only from that form Public allows use from other forms If not specified, default is Public Access specifiers won’t be used for now

Page 8: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

8

Procedures and Static Variables Variables needed only in a procedure, should be

declared within that procedure Creates a local variable with scope only within

the procedure where declared Local variable values are not saved from one

procedure call to the next To save value between procedure calls, use

Static keyword to create a static local variable Static VariableName As DataType Scope is still only within the procedure But variable exists for lifetime of application

Page 9: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

Passing Arguments to a Procedure

When calling a procedure, you can pass it values known as arguments

Page 10: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

10

Arguments Argument – a value passed to a procedure We’ve already done this with functions

Value = CInt(txtInput.Text) Calls the CInt function and passes txtInput.Text

as an argument A procedure must be declared with a parameter

list in order to accept an argument

Page 11: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

11

Passing Arguments By Value

intNumber declared as an integer argument Storage location intNumber created by procedure A value, 5 in this case, must be supplied and is

copied into the storage location for intNumber The DisplayValue procedure then executes

DisplayValue(5) ‘calls DisplayValue procedure

Sub DisplayValue(ByVal intNumber As Integer)' This procedure displays a value in a message box.MessageBox.Show(intNumber.ToString)

End Sub

Page 12: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

12

Passing Multiple Arguments

Multiple arguments separated by commas Value of first argument is copied to first Second to second, etc.

ShowSum(intValue1, intValue2) ‘calls ShowSum procedure

Sub ShowSum(ByVal intNum1 As Integer, _ByVal intNum2 As Integer)

' This procedure accepts two arguments, and prints' their sum on the form.Dim intSum As IntegerintSum = intNum1 + intNum2MessageBox.Show("The sum is " & intSum.ToString)

End Sub

Page 13: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

13

Passing Arguments ByVal or ByRef Arguments are usually passed ByVal

New storage location created for procedure Storage location gets a copy of the value Any changes in value are made to the copy Calling procedure won’t “see” the changes

Arguments can also be passed ByRef Procedure points to (references) argument’s

original storage location Any changes are made to the original value Calling procedure “sees” the changes

Page 14: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

ByVal or ByRef Argument Example

The difference between parameters passed ByVal & ByRef Passed ByVal Calling procedure does not

“see” changes made to the value of an argument

Passed ByRef Calling procedure “sees”

changes made to the value of an argument

14

Page 15: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

Functions

A Function Returns a Value to the Part of the Program That Called the

Function

Page 16: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

16

Declaring a Function

New keyword Function Also new is As DataType which states the data

type of the value to be returned Return value is specified in a Return expression

[AccessSpecifier] Function FunctionName ([ParameterList]) _As DataType

[Statements]End Function

Page 17: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

17

Function Call ExamplesngTotal = Sum(sngValue1, sngValue2)

Function Sum(ByVal sngNum1 As Single, _ByVal sngNum2 As Single) As Single

Dim sngResult As SinglesngResult = sngNum1 + sngNum2Return sngResult

End Function

sngValue1 & sngValue2 must be data type Single Data types must agree with parameter list

sngTotal must be Single, agrees with return value

Page 18: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

18

Returning Nonnumeric Values

Function IsValid(intNum As Integer) As BooleanDim blnStatus As BooleanIf intNum >= 0 And intNum <= 100 Then

blnStatus = TrueElse

blnStatus = FalseEnd IfReturn blnStatus

End Function

Function FullName(ByVal strFirst As String, _ByVal strLast As String) As String

Dim strName As StringstrName = strLast & ", " & strFirstReturn strName

End Function

Page 19: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

More About Debugging

Step IntoStep OverStep Out

Page 20: Week Procedures And Functions 7 A procedure is a collection of statements that performs a task.

20

Debugging Involving Procedures

Step Into - continue to debug by single-stepping through a procedure

Step Over - run procedure without single-stepping, continue single-step after the call

Step Out - end single-stepping in procedure, continue single-step after the call


Recommended