6a – sub procedures

8
6a – Sub Procedures Lingma Acheson Department of Computer and Information Science, IUPUI CSCI N331 VB .NET Programming

Upload: raheem

Post on 20-Jan-2016

20 views

Category:

Documents


0 download

DESCRIPTION

6a – Sub Procedures. CSCI N331 VB .NET Programming. Lingma Acheson. Department of Computer and Information Science, IUPUI. Procedures. procedure – a unit of code that executes when called from another place. Not a new face – ‘Sub procedure definition – a button click event - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: 6a – Sub Procedures

6a – Sub Procedures

Lingma AchesonDepartment of Computer and Information Science, IUPUI

CSCI N331 VB .NET Programming

Page 2: 6a – Sub Procedures

Procedures• procedure – a unit of code that executes when

called from another place.• Not a new face –

‘Sub procedure definition – a button click eventPrivate Sub btnDisplay_Click(ByVal sender As System.Object,

ByVal e As System.EventArgs) Handles btnDisplay.Click

…. End Sub Or:

lstOutput.Items.Clear() ‘sub procedure call• procedure definition – Defines what to execute

when the procedure is called.• procedure call – Defines when to execute the

procedure.

2

Page 3: 6a – Sub Procedures

Procedures• A procedure must have a name following by (). E.g. lstOutput.Items.Clear()

lstOutput.Items.Add(“******”) CDbl(txtInputOne.Text)• Purpose of using procedures – make your code

well- organized and easy to understand.

3

Page 4: 6a – Sub Procedures

ProceduresWithout Using Procedures:Button click event: Create variables Take two user inputs, and store them to variables Perform the addition and display the result Perform the subtraction and display the result Perform the multiplication and display the result Perform the division and display the result (handle divide by 0 situation here)End of event

Using Procedures:Create variablesButton click event: Take two user inputs, and store them to variables Add() Subtract() Multiply() Divide()End of eventDefine Add(): Perform the addition and display the resultDefine Subtract(): Perform the subtraction and display the result…Define Divide() Perform the division and display the result(handle divide by 0 situation here)

Page 5: 6a – Sub Procedures

Sub Procedures• Sub procedure – execute a block of codes

when called• Define a Sub procedure: Private Sub name()

‘actions here … End Sub• The key word Private means this Sub can only

be used by this Form.• The Sub procedure call must match with the

Sub procedure definition.

5

Page 6: 6a – Sub Procedures

Sub Procedures• E.g. a user defined Sub definition and Sub call –

Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click

intFirst = CInt(nudFirst.Value) intSecond = CInt(nudSecond.Value)

Add() ‘Sub call. The program will look for a Sub definition with the same

‘name to decide what to do

End Sub

‘Sub definitionPrivate Sub Add()

Dim intResult As Integer = 0‘intFirst and intSecond are global variables, so they can used by

both Subs.

intResult= intFirst + intSecondtxtAddResult.Text = CStr(intResult)

End Sub …

6

Page 7: 6a – Sub Procedures

7

Local Variables and Global Variables

• Local Variables:– Purpose: for a specific procedure to use– Scope: only used inside that procedure– Where: created inside a procedure

• Global Variables:– Purpose: a variable shared by different procedures– Scope: the form, also called form variables, can be

used anywhere in the same form– Where: created at the beginning of the Class,

outside of any procedures

Page 8: 6a – Sub Procedures

Local Variables and Global Variables

Local Variables:Add Button click event procedure: ‘3 local variables, only used ‘inside this procedure Create variables 1, 2, and 3 Take user input and store in variable 1 and 2 Add variable 1 to variable 2 and store the result to variable 3End of event procedure

Global Variables:‘Create 2 global variables outside of any procedure, can be used anywhereCreate variables 1, 2 Add Button click event procedure: Add()End of event procedureProcedure Add(): ‘1 local variable, only used ‘inside this procedure Create variable 3 ‘use global variable 1 and 2 Take the user input and store in variable 1 and 2 ‘use global variable 1 and 2, and ‘local variable 3 Add variable 1 to variable 2 and store the result to variable 3End of Add procedure