Chapter 5 General Procedures 5 1 Function Procedures

  • Slides: 37
Download presentation
Chapter 5 - General Procedures 5. 1 Function Procedures 5. 2 Sub Procedures, Part

Chapter 5 - General Procedures 5. 1 Function Procedures 5. 2 Sub Procedures, Part I 5. 3 Sub Procedures, Part II 5. 4 Modular Design 1

Devices for Modularity Visual Basic has two devices for breaking problems into smaller pieces:

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

5. 1 Function Procedures • User-Defined Functions Having One Parameter • User-Defined Functions Having

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 3

Some Built-In Functions Function: Int Example: Int(2. 6) is 2 Input: number Output: number

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 4

Some Built-In Functions (continued) Function: Format. Percent Example: Format. Percent(0. 12, 2) is 12.

Some Built-In Functions (continued) Function: Format. Percent Example: Format. Percent(0. 12, 2) is 12. 00% Input: number, number Output: string Function: Format. Number Example: Format. Number(12. 62, 1) is 12. 6 Input: number, number Output: string 5

What is a Procedure? Procedure declaration: A Procedure is a collection of statements that

What is a Procedure? Procedure declaration: A Procedure is a collection of statements that are grouped together to perform a task. Ø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). Function procedures return Procedure call ØSpecify the identifier values. Sub ØPlace actual parameters (arguments) in parentheses Procedures do not. ØActual data (literal, constant, variable, or expression) ØUse return value (if a function)

Function Procedures • Function procedures (aka user-defined functions) always return one value • Syntax:

Function Procedures • Function procedures (aka user-defined functions) always return one value • Syntax: Function. Name(By. Val var 1 As Type 1, By. Val var 2 As Type 2, . . . ) As Return. Data. Type statement(s) Return expression End Function 7

Example With One Parameter Function Fto. C(By. Val t As Double) As Double 'Convert

Example With One Parameter Function Fto. C(By. Val t As Double) As Double 'Convert Fahrenheit temp to Celsius Return (5 / 9) * (t - 32) End Function 8

Signature of the Fto. C Function Procedure What the author calls “header” is more

Signature of the Fto. C Function Procedure 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. 9

Header of the Fto. C Function Procedure Parameter declarations are like variable declarations. You

Header of the Fto. C Function Procedure Parameter declarations are like variable declarations. You give them a type. In addition, a parameter is either By. Val or By. Ref. More about this later. 10

Example 5. 1. 1: Form txt. Temp. F txt. Temp. C 11

Example 5. 1. 1: Form txt. Temp. F txt. Temp. C 11

Example 5. 1. 1: Code 12

Example 5. 1. 1: Code 12

Example 5. 1. 1: Output 13

Example 5. 1. 1: Output 13

Example 5. 1. 1: Code Function call Function declaration 14

Example 5. 1. 1: Code Function call Function declaration 14

Anatomy of Function Declaration and Call Function call specify use return identifier value identifier

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

Processing Sequence of a Function Call Function call 5) Return value assigned into celsius.

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

Call Stack • The. NET Framework keeps the local variables and parameters of a

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.

Using the Debugger to See Data in the Call Stack • Two debugging windows:

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

Processing has paused here…right before the call to FTo. C. Call Stack – shows

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

Processing has paused here…inside FTo. C. Call Stack – shows that btn. Convert_Click has

Processing has paused here…inside FTo. C. Call Stack – shows that btn. Convert_Click has called FTo. C. Locals – shows local variables and parameters of 20 FTo. C

Processing has returned to btn. Convert_Click from FTo. C. Call Stack –FTo. C has

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

Example With One String Parameter Function First. Name(By. Val full. Name As String) As

Example With One String Parameter Function First. Name(By. Val full. Name As String) As String 'Extract first name from full name Dim first. Space As Integer first. Space = full. Name. Index. Of(" ") Return full. Name. Substring(0, first. Space) End Function 22

Example 5. 1. 2: Form txt. Full. Name txt. First. Name 23

Example 5. 1. 2: Form txt. Full. Name txt. First. Name 23

Example 5. 1. 2: Code Private Sub btn. Determine_Click(. . . ) _ Handles

Example 5. 1. 2: Code Private Sub btn. Determine_Click(. . . ) _ Handles btn. Determine. Click Dim full. Name As String full. Name = txt. Full. Name. Text txt. First. Name. Text = First. Name(full. Name) End Sub Function First. Name(By. Val full. Name As String) _ As String Dim first. Space As Integer first. Space = name. Index. Of(" ") Return name. Substring(0, first. Space) End Function 24

Example 5. 1. 2: Output 25

Example 5. 1. 2: Output 25

User-Defined Function Having Several Parameters Function Pay(By. Val wage As Double, By. Val hrs

User-Defined Function Having Several Parameters Function Pay(By. Val wage As Double, By. Val 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 26

Example 5. 1. 3: Form txt. Wage txt. Hours txt. Earnings 27

Example 5. 1. 3: Form txt. Wage txt. Hours txt. Earnings 27

Example 5. 1. 3: Partial Code Private Sub btn. Calculate_Click(. . . ) _

Example 5. 1. 3: Partial Code Private Sub btn. Calculate_Click(. . . ) _ Handles btn. Calculate. Click Dim hourly. Wage, hours. Workded As Double hourly. Wage = CDbl(txt. Wage. Text) hours. Worked = CDbl(txt. Hours. Text) txt. Earnings. Text = Format. Currency(Pay(hourly. Wage, hours. Worked)) End Sub Function call Remember the rule with parentheses…processing order goes from inside to outside. 28

Example 5. 1. 3: Output 29

Example 5. 1. 3: Output 29

Example 5. 1. 4 30

Example 5. 1. 4 30

Scope • A variable’s scope is its visibility (which statements can use the variable).

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.

Duration • A variable’s duration refers to how long (or when) the variable exists

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.

Example 5. 1. 5 Class Scope data. . Shared by all procedures in the

Example 5. 1. 5 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. 33

Example 5. 1. 5 Local variables. . Only available within the procedure Procedure scope

Example 5. 1. 5 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. 34

Example 5. 1. 5 35

Example 5. 1. 5 35

User-Defined Function Having No Parameters Function Cost. Of. Item() As Double Dim price As

User-Defined Function Having No Parameters Function Cost. Of. Item() As Double Dim price As Double = CDbl(txt. Price. Text) Dim quantity As Integer = CDbl(txt. Quantity. Text) Dim cost = price * quantity Return cost End Function 36

Example 5. 1. 6 Function Is. Vowel. Word(By. Val word As String) As Boolean

Example 5. 1. 6 Function Is. Vowel. Word(By. Val word As String) As Boolean If word. Index. Of("A") = -1 Then Return False End If. . If word. Index. Of("U") = -1 Then Return False End If Return True End Function 37