You are on page 1of 3

1 IE 212: COMPUTATIONAL METHODS FOR INDUSTRIAL ENGINEERING

2 EXAMPLE FOR LECTURE NOTES #6: PASSING VALUES TO SUBS AND FUNCTIONS
3 Last modified: 3/5/2015
4
5 *** SEE GRAPHICAL USER INTERFACE IN THE BACK ***
6
7Option Explicit
8
9Sub passingValues()
10'Procedure to illustrate different ways of passing values to
11'Sub procedures and Function procedures
12
13 'Declare variables
14 Dim i As Integer
15 Dim feet As Double
16 Dim miles As Double
17
18 'Assign value to variable i
19 i=1
20
21 'Display value of variable i before call to sub
22 MsgBox "Inside Sub passingValues " & vbCrLf & _
23 "Intitial value of i: " & i
24
25 'Passing value of variable i by reference (default)
26 'to Sub procedure modifyValue
27 Call modifyValue(i)
28
29 'Display value of variable i after call to sub
30 MsgBox "Inside Sub passingValues " & vbCrLf & _
31 "Final value of i: " & i
32
33'**********************************************************
34 'Assign value to variable feet
35 feet = Range("C3").Value
36
37 'Invoke function procedure convertToMiles
38 miles = convertToMiles(feet)
39
40 'Display values in feet and miles
41 MsgBox "Feet: " & Format(feet, "#,##0.00") & vbCrLf & _
42 " Miles: " & Format(miles, "0.00")
43
44 'Display miles
45 Range("C6").Value = Format(miles, "0.00")
46
47 'Select cell C3
48 Range("C3").Select
49
50End Sub 'PassingValues
51
52Sub modifyValue(z)
53'Procedure to modify value of variable i
54'Called by passingValues procedure
55
56 'Modify value of z
57 z=z+1
58
59 'Display value of i after change
60 MsgBox "Inside Sub modifyValue " & vbCrLf & _
61 "Value of i after adding 1: " & z
62
1 1

2
63End Sub 'modifyValue
64
65Function convertToMiles(ft)
66'Procedure to convert feet to miles
67'Invoked by passingValues procedure
68
69 'Convert value in feet to miles
70 convertToMiles = ft / 5280
71
72End Function 'convertToMiles

3 2

4
73

74

You might also like