1 / 11

CS0004: Introduction to Programming

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

kayo
Download Presentation

CS0004: Introduction to Programming

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. CS0004: Introduction to Programming Function Procedures

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

  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: FunctionFunctionName (ByValvar1As Type, ByValvar2As Type) As ReturnDataType … some code … Returnexpression 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 Returnexpressionis 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 yAs Double) As Double Dim var2 As String … End Function Function otherFunction(ByValx As String, ByVal y As String) As Double Dim var3 As String … End Function 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

More Related