Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design.

Similar presentations


Presentation on theme: "1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design."— Presentation transcript:

1 1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design

2 2 Devices for Modularity Visual Basic has two devices for breaking problems into smaller pieces: Function procedures Sub procedures

3 3 5.1 Function Procedures User-Defined Functions Having One Parameter User-Defined Functions Having Several Parameters User-Defined Functions Having No Parameters User-Defined Boolean-Valued Functions

4 4 Some Built-In Functions Function: Int Example: Int(2.6) is 2 Input: number Output: number Function: Math.Round Example: Math.Round(1.23, 1) is 1.2 Input: number, number Output: number

5 5 Some Built-In Functions (continued) Function: FormatPercent Example: FormatPercent(0.12, 2) is 12.00% Input: number, number Output: string Function: FormatNumber Example: FormatNumber(12.62, 1) is 12.6 Input: number, number Output: string

6 What is a Procedure? A Procedure is a collection of statements that are grouped together to perform a task. Function procedures return values. Sub Procedures do not. Procedure declaration:  Signature  Modifier(s) (optional) (e.g.public or private, static)  Function or Sub  Identifier  Formal Parameter List  Enclosed in parentheses  types and identifiers (like variable declaration)  Separated by commas  Return type (if a function)  Procedure Body  statements  requires return statement (if a function). Procedure call  Specify the identifier  Place actual parameters (arguments) in parentheses  Actual data (literal, constant, variable, or expression)  Use return value (if a function)

7 7 Function Procedures Function procedures (aka user-defined functions) always return one value Syntax: Function FunctionName(ByVal var1 As Type1, ByVal var2 As Type2,...) As ReturnDataType statement(s) Return expression End Function

8 Example With One Parameter Function FtoC(ByVal t As Double) As Double 'Convert Fahrenheit temp to Celsius Return (5 / 9) * (t - 32) End Function 8

9 Signature of the FtoC Function Procedure 9 What the author calls “header” is more widely known as signature. A function’s signature consists of a name, list of parameters, and return type. Subroutines do not have return types.

10 Header of the FtoC Function Procedure 10 Parameter declarations are like variable declarations. You give them a type. In addition, a parameter is either ByVal or ByRef. More about this later.

11 11 Example 5.1.1: Form txtTempF txtTempC

12 Example 5.1.1: Code 12

13 13 Example 5.1.1: Output

14 Example 5.1.1: Code 14 Function call Function declaration

15 Anatomy of Function Declaration and Call Function call return type identifier formal parameter(s) Function body return statement specify identifier Pass actual parameter(s) (arguments) use return value Function declaration

16 Processing Sequence of a Function Call Function call Function declaration 2) Pass by value: t is a copy of fahrenheitTemp. 3) Function body executes. 4) Return value sent back to calling statement (resolving the expression). 5) Return value assigned into celsiusTemp. 1) Call the function

17 Call Stack The.NET Framework keeps the local variables and parameters of a procedure in a Frame in the Call Stack. Frame = a data structure that holds the values of all the local variables and formal parameters of a procedure. Stack = a last-in, first-out data structure. Items are pushed onto the top of the stack. Items are popped off the top of the stack. When a procedure is called, its frame (local variables and parameters) are pushed onto the top of the stack. When a procedure terminates, its frame is removed from the stack.  the formal parameters and local variables of a procedure exist ONLY AS LONG AS THE PROCEDURE IS EXECUTING.

18 Using the Debugger to See Data in the Call Stack Two debugging windows: Call Stack – shows each item in the stack. This gives you a trace of procedure calls. Locals – shows all the local data (local variables and formal parameters) of the selected Procedure on the stack 18

19 19 Call Stack – shows only btnConvert_Click procedure Locals – shows local variables and parameters of btnconvert_Click Processing has paused here…right before the call to FToC.

20 20 Call Stack – shows that btnConvert_Click has called FToC. Locals – shows local variables and parameters of FToC Processing has paused here…inside FToC.

21 21 Call Stack –FToC has been popped. It’s local variables no longer exist, Locals – shows local variables and parameters of btnConvert_Click Processing has returned to btnConvert_Click from FToC.

22 Example With One String Parameter Function FirstName(ByVal fullName As String) As String 'Extract first name from full name Dim firstSpace As Integer firstSpace = fullName.IndexOf( " " ) Return fullName.Substring(0, firstSpace) End Function 22

23 23 Example 5.1.2: Form txtFullName txtFirstName

24 Example 5.1.2: Code Private Sub btnDetermine_Click(...) _ Handles btnDetermine.Click Dim fullName As String fullName = txtFullName.Text txtFirstName.Text = FirstName(fullName) End Sub Function FirstName(ByVal fullName As String) _ As String Dim firstSpace As Integer firstSpace = name.IndexOf( " " ) Return name.Substring(0, firstSpace) End Function 24

25 25 Example 5.1.2: Output

26 26 User-Defined Function Having Several Parameters Function Pay(ByVal wage As Double, ByVal hrs As Double) As Double Dim amt As Double 'amount of salary Select Case hrs Case Is <= 40 amt = wage * hrs Case Is > 40 amt = wage * 40 +(1.5 * wage * (hrs – 40)) End Select Return amt End Function

27 27 Example 5.1.3: Form txtWage txtHours txtEarnings

28 Example 5.1.3: Partial Code Private Sub btnCalculate_Click(...) _ Handles btnCalculate.Click Dim hourlyWage, hoursWorkded As Double hourlyWage = CDbl(txtWage.Text) hoursWorked = CDbl(txtHours.Text) txtEarnings.Text = FormatCurrency(Pay(hourlyWage, hoursWorked)) End Sub 28 Function call Remember the rule with parentheses…processing order goes from inside to out outside.

29 29 Example 5.1.3: Output

30 Example 5.1.4 30

31 Scope A variable’s scope is its visibility (which statements can use the variable). Variables declared in the class but outside any procedure have class scope. They can be used by any statement of any procedure within the class. Local variables and formal parameters have procedure scope. They can only be used inside the procedure in which they are declared. Variables declared inside blocks have block scope. They can be used only inside the block for which they are declared. Loop counter variables declared in the FOR statement of a FOR NEXT loop have loop scope; they can only be used within the loop for which they are declared. You can declare multiple variables of the same name as long as they are not in the same block structure. You cannot declare variables of the same name within the same block structure.

32 Duration A variable’s duration refers to how long (or when) the variable exists during processing. Goes hand in hand with scope Procedure scope variables usually exist only as long as the procedure executes (unless they are Static). Block scope exist only as long as the block executes. Loop counter variables declared in the FOR statement of a FOR NEXT throughout all repetitions of the loop, and then disappear. Class scope variables (for a form) last as long as the form is loaded.

33 Example 5.1.5 33 Class Scope data.. Shared by all procedures in the class Class scope data is NOT in the call stack. It is in the HEAP, and remains in existence as long as the form is loaded.

34 Example 5.1.5 34 Local variables.. Only available within the procedure Procedure scope data is in the call stack. It remains in existence only as long as the procedure is executing.

35 Example 5.1.5 35

36 36 User-Defined Function Having No Parameters Function CostOfItem() As Double Dim price As Double = CDbl(txtPrice.Text) Dim quantity As Integer = CDbl(txtQuantity.Text) Dim cost = price * quantity Return cost End Function

37 Function IsVowelWord(ByVal word As String) As Boolean If word.IndexOf("A") = -1 Then Return False End If. If word.IndexOf("U") = -1 Then Return False End If Return True End Function 37 Example 5.1.6


Download ppt "1 Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design."

Similar presentations


Ads by Google