80 likes | 307 Views
General Procedures. Chapter 4. Different Procedures. 4.1 Sub Procedures, Part I 4.2 Sub Procedures, Part II 4.3 Function Procedures 4.4 Modular Design (not on test). Sub Procedures Part 1.
E N D
General Procedures Chapter 4
Different Procedures • 4.1 Sub Procedures, Part I • 4.2 Sub Procedures, Part II • 4.3 Function Procedures • 4.4 Modular Design (not on test)
Sub Procedures Part 1 • Sub Procedure - part of a program that performs one or more related tasks, has its own name, is written as a separate part of the program. Private Sub ProcedureName() statements End Sub
Example 1 pg.143 Private Sub cmdAdd_Click() Dim num1 as single, num2 as single picResult.cls Call ExplainPurpose picResult.Print num1 = 2 num2 = 3 picResult.Print “The sum of”; num1; “and”; num2; “is”; num1 + num2 End Sub Private Sub ExplainPurpose() picResult.Print “This program displays a sentence” picResult.Print “Identifying two numbers and their sum.” End Sub
Example 2, pg.144 Private Sub cmdAdd_Click() Dim num1 as single, num2 as single picResult.cls Call ExplainPurpose picResult.Print Call Add(2,3) End Sub Private Sub ExplainPurpose() picResult.Print “This program displays a sentence” picResult.Print “Identifying two numbers and their sum.” End Sub Private Sub Add(num1 as Single, num2 as Single) picResult.Print “the sum of”; num1; “and”; num2; “is”; num1 + num2 End Sub
NOTE: Look at Practice Problem 3 on page 170 Sub Procedures Part II • Passing Values Back from Sub Procedures • passed by reference • passed by value - use this method when you want to ensure the variable will retain its original value when the Sub terminates • In the Call statement, enclose the variable with an extra parentheses…..Call Triple((amt)) • Use ByVal statement…. • Private Sub Triple(By Val num as single)
Function Procedures • Built-in Visual Basic Function • Format, Val, Int, Chr, Len, Mid, InStr (pg 178) • User-defined functions • Functions that you create and declare Private Function FunctionName (var1 as Type1,…..) as dataType statements FunctionName = expression End Function
NOTE: Look at Practice Problem 2 on page 185 Function Example 1, pg 179 Private Sub cmdConvert_Click() picTempC.cls picTemC.Print FtoC(val(txtTempF.Text)) End Sub Private Function FtoC(t as Single) as Single FtoC = (5 / 9) * (t - 32) End Function