6c-FunctionProcedures - Department of Computer and

advertisement
CSCI N331 VB .NET Programming
6c – Function Procedures
Lingma Acheson
Department of Computer and Information Science, IUPUI
Function Procedures
• Sometimes a procedure produces a result,
e.g.
– CStr(intResult) will change the type of the
intResult value from an Integer type to a String
type. The result is the value in the String format.
– Add(1, 2) will produce an addition result of 3.
• A procedure that generates a result is called a
Function Procedure. A procedure that doesn’t
generate a result is called a Sub Procedure.
• We can give the result to another variable.
E.g.
intResult = Add(intInput1, intInput2)
2
lblResult.Text = CStr(intResult)
Procedures
Sub Procedures:
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)
End of event procedure
Procedure Add(variable1 and
variable2):
Create variable 3
Add variable 1 to variable 2
and store in variable 3
display result in label
End of Add procedure
Can be changed to:
Button click event procedure:
Create variables 1, 2
Create variable 3
Take the user input and store
in variable 1 and 2
variable 3 = Add(variable1 and
variable 2)
‘use the result in this procedure
display variable 3 in label
End of event procedure
Procedure Add(variable1 and
variable2)
Create variable 4
Add variable 1 to variable 2
and store in variable 4
pass the value of variable 4 out
End of Add procedure
Function Procedures
• Function - A type of procedure that produces a result
– Example:
Function procedure call and use the result:
dblResult = Add(dblInput1, dblInput2)
Function procedure definition:
‘”As Double” indicates that the function produces a double value
Private Function Add(ByVal dblValue1 As Double, ByVal
dblValue2 As Double) As Double
Dim dblValue3 As Double
dblValue3 = dblValue1 + dblValue2
Return dblValue3
End Function
– Return xxx: when the Function finishes execution, xxx is the
value to be passed out. Can be a value or a variable name.
– Functions can also take4 parameters, just like Subs
Function Procedures
– E.g.
Private Sub btnCompute_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnDisplay.Click
…
‘Function Add takes two parameters, does the addition and passes the
‘result out to the intAddResult.
intAddResult = Add(intFirst, intSecond) ‘Function call
txtAddResult.Text = CStr(intAddResult)
End Sub
‘Function takes two integer parameters and returns an integer as the result
Private Function Add(ByVal intOne As Integer, ByVal intTwo As Integer) As
Integer
Dim intResult As Integer = 0
intResult= intOne + intTwo
Return intResult ‘pass the result out
‘intResult is unnecessary. The above three lines can be replaced by the
‘following line
‘Return (intOne + intTwo)
End Function
Function Procedures
– Each function can only return one value.
E.g, In the following code, although there are two return
statements, only one return statement will be executed.
If (a condition is true) Then
return True ‘return a boolean value
Else
return False
End If
– Boolean data type revisit –
A boolean variable only has two values, True or False. It is
often used to indicate a situation is true or false. E.g.
Dim blnPass As Boolean
If (intScore >=60) Then
blnPass = True
Else blnPass = False
Download