1 / 7

6b – Sub Procedure With Parameters

6b – Sub Procedure With Parameters. CSCI N331 VB .NET Programming. Lingma Acheson. Department of Computer and Information Science, IUPUI. Procedure Revisit. Add an item into the list box: ‘Contents inside the () defines what to add to the listbox lstOutput.Items. Add ( “*************” )

ling
Download Presentation

6b – Sub Procedure With Parameters

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. 6b – Sub Procedure With Parameters CSCI N331 VB .NET Programming Lingma Acheson Department of Computer and Information Science, IUPUI

  2. Procedure Revisit • Add an item into the list box: ‘Contents inside the () defines what to add to the listbox lstOutput.Items.Add(“*************”) ‘Contents inside the () defines what to show in the messagebox MessageBox.Show(“The second input is 0. Unable to perform the division.”) • We can pass data to the procedure and give some information for the procedure to use.

  3. Procedures Global Variables: ‘Create 2 global variables outside of any procedure, can be used anywhere Create variables 1, 2 Button click event procedure: Add() Multiply() End of event procedure Procedure Add(): ‘1 local variable, only used ‘inside this procedure Create variable 3 ‘use global variable 1 , and 3 Take the user input and store in variable 1 and 2 Add variable 1 to variable 2 and store the result to variable 3 End of procedure Add Procedure Multiply() … End of Multiply procedure Can be changed to the following: Button click event procedure: Create variables 1, 2 Take the user input and store in variable 1 and 2 ‘Tell the procedure what to add Add(variable1 and variable2) ‘Tell the procedure what to multiply Multiply(variable1 and variable2) End of event procedure Procedure Add(variable1 and variable2): Create variable 3 Add variable 1 to variable 2 and store in variable 3 End of Add procedure Procedure Multiply(variable1 and variable2): Create variable 3 Multiply variable 1 to variable 2 and store in variable 3 End of Multiply procedure

  4. Parameters • Sub procedure that takes parameters • Purpose: Pass data into the Sub for the Sub to use • Example: Sub call: Add(2.5, 3.1) Sub Definition: Private Sub Add(ByValdblValue1 As Double, ByVal dblValue2 As Double) Dim dblResult as Double dblResult = dblValue1 + dblValue2 End Sub

  5. Parameters • Sub procedure that takes parameters • More than one value can be passed into the Sub. If more than one, use comma to separate the values. • The number of values passed in must match the number of parameters in the definition. • The types of values passed in must match the types of parameters in the definition.

  6. Parameters • Sub procedure that takes parameters • The parameter name defined in the procedure definition doesn’t have to be the same as the actual variable name passed into the procedure.E.g.: We can call add twice with different variables. Sub call: Add(2.5, 3.1) Add(dblInput1, dblInput2) Add(dblInput3, dblInput4) Sub Definition: Private Sub Add(ByValdblValue1 As Double, ByVal dblValue2 As Double) Dim dblResult as Double dblResult = dblValue1 + dblValue2 End Sub • Sub procedures with parameters are very useful if we want to perform the same task again with different values.

  7. Sub Procedures • E.g. Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisplay.Click Dim intFirst As Integer Dim intSecond As Integer intFirst = CInt(nudFirst.Value) intSecond = CInt(nudSecond.Value) ‘intFirst and intSecond are local variables, thus they cannot be used ‘inside the Add() procedure. Their values must be passed into the Add(). Add(intFirst, intSecond) ‘Sub call End Sub ‘Sub definition, takes two parameters, both are integers Private Sub Add(ByValintOne As Integer, ByValintTwo As Integer) Dim intResult As Integer = 0 intResult= intOne + intTwo txtAddResult.Text = CStr(intResult) End Sub

More Related