Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS0004: Introduction to Programming

Similar presentations


Presentation on theme: "CS0004: Introduction to Programming"— Presentation transcript:

1 CS0004: Introduction to Programming
Function Procedures

2 Review General Form of an If/Else Block… If condition Then some code
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

3 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

4 Function Procedures Functions are used in programming to break complex problems into smaller ones We have encountered built-in functions in VB before Int(param1) FormatNumber(param1,param2) These functions output a value to where they are used. 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.

5 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.

6 Function Example 1 New Topics: Programmer Defined Function
Parameters/arguments Return Statement Function Call

7 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

8 Function Example 2 New Topics: Multiple Parameters/Arguments

9 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.

10 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 Class

11 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 ppt "CS0004: Introduction to Programming"

Similar presentations


Ads by Google