Functions

advertisement
CS0004: Introduction to
Programming
Function Procedures
Review
General Form of an If/Else Block…
If condition Then
some code
Else
some other code
End If
 General Form of a Select Case Block
Select Case selector
Case valueList1
action1
Case valueList2
action2
Case Else
actionOfLastResort
End Select

Review

A value list can be a…(4 things)





Literal
Variable
Expression
Range
What property holds whether a radio button or check
box is checked?

Checked
Function Procedures


Functions are used in programming to break complex
problems into smaller ones
We have encountered built-in functions in VB before



These functions output a value to where they are used.



Int(param1)
FormatNumber(param1,param2)
When a function outputs a value, it is said to return a value.
When a function is used, it is said to be called.
You can pass input to a function by supplying it with
arguments.


Arguments are the values you pass to a function
Parameters are the local variables used in the function definition.
Function Procedures


We can define our own functions:
General Form:
Function FunctionName (ByVal var1 As Type, ByVal
var2 As Type) As ReturnDataType
…
some code
…
Return expression
End Function
 FunctionName is the name of the function
 var1 and var2 are the names of the parameters
 The Types following var1 and var2 tell what type the parameters are
 ReturnDataType is the type of the value that the function returns
 The expression in Return expression is what value is returned
by the function.
Function Example 1

New Topics:




Programmer Defined Function
Parameters/arguments
Return Statement
Function Call
Function Procedures

Notes:





The code for the function is called the function definition.
When calling a function, you must supply it with the same
number of arguments as parameters defined in the function
definition.
You can create functions with no parameters. Just leave the
parentheses empty when defining the function.
A function MUST return a value.
Function names in VB:



Typically are camel case with first letter capitalized.
Follow the same naming rules as variables
Should be a verb that describe what the function does
Function Example 2

New Topics:

Multiple Parameters/Arguments
Scope





Scope refers to where a variable can be used (seen).
Scope is usually defined by where it was declared.
A variable declared in a function or event procedure can
ONLY be used in the function or event procedure where
it was declared.
A variable declared inside of a class, but not in any of its
procedures can be used anywhere in the class, including in
any of its procedures.
Parameters can be used ONLY in the function they
belong to.
Scope

Example:
Public Class frmMain
Dim var1 As Integer
Function someFunction(ByVal x As Double, ByVal y As Double) As Double
Dim var2 As String
…
End Function
Function otherFunction(ByVal x As String, ByVal y As String) As Double
Dim var3 As String
…
End Function
End Class
Function Documentation

You should document EVERY function you write:
‘ Function Name:
The name of the function
‘ Returns:
Return type and brief description of what it
‘
is
‘ Parameters:
Types and brief descriptions of what they are
‘ Description:
Brief description of what the function does
Download