Download presentation
Presentation is loading. Please wait.
Published byBertina Martin Modified over 9 years ago
1
2002 Prentice Hall. All rights reserved. 1 Chapter 6 - Procedures Outline 6.1Introduction 6.2 Modules, Classes and Procedures 6.3 Sub Procedures 6.4 Function Procedures 6.5 Methods 6.6 Argument Promotion 6.7 Option Strict and Data Type Conversions 6.8 Value Types and Reference Types 6.9 Passing Arguments: Pass-by-Value vs. Pass-by-Reference 6.10 Duration of Identifiers 6.11 Scope Rules 6.12 Random-Number Generation 6.13 Example: Game of Chance 6.14 Recursion 6.15 Example Using Recursion: The Fibonacci Series 6.16 Recursion vs. Iteration
2
2002 Prentice Hall. All rights reserved. 2 Outline 6.17 Procedure Overloading and Optional Arguments 6.17.1 Procedure Overloading 6.17.2 Optional Arguments 6.18 Modules
3
2002 Prentice Hall. All rights reserved. 3 6.2 Modules, Classes and Procedures Framework Class Library –Provides a rich collection of “prepackaged” classes and methods for performing many operations Mathematical calculations String manipulations Character manipulations Input/output operations Error checking
4
2002 Prentice Hall. All rights reserved. 4 6.2 Modules, Classes and Procedures Programmer-defined procedures –FCL cannot provide every conceivable feature that a programmer could want –Three types of procedures Sub procedures Function procedures Event procedures –A procedure is invoked by a procedure call
5
2002 Prentice Hall. All rights reserved. 5 6.2 Modules, Classes and Procedures Division of code into procedures –Several motivations to divide code into procedures Divide-and-conquer approach makes program development more manageable Software reusability Avoids the repetition of code in a program
6
2002 Prentice Hall. All rights reserved. 6 6.2 Modules, Classes and Procedures Earlier programs had only one procedure that called FCL methods Next program contains two customized procedures
7
2002 Prentice Hall. All rights reserved. Outline 7 Payment.vb Program Output 1 ' Fig. 6.2: Payment.vb 2 ' Sub procedure that prints payment information. 3 4 Module modPayment 5 6 Sub Main() 7 8 ' call Sub procedure PrintPay 4 times 9 PrintPay(40, 10.5) 10 PrintPay(38, 21.75) 11 PrintPay(20, 13) 12 PrintPay(50, 14) 13 14 Console.ReadLine() ' prevent window from closing 15 End Sub ' Main 16 17 ' print amount of money earned in command window 18 Sub PrintPay(ByVal hours As Double, ByVal wage As Decimal) 19 20 ' pay = hours * wage 21 Console.WriteLine("The payment is {0:C}", hours * wage) 22 End Sub ' PrintPay 23 24 End Module ' modPayment The payment is $420.00 The payment is $826.50 The payment is $260.00 The payment is $700.00 PrintPay executes when it is invoked by Main PrintPay receives the values of each argument and stores them in the parameters variables hours and wage Notice that PrintPay appears within modPayment. All procedures must be defined inside a module or a class
8
2002 Prentice Hall. All rights reserved. 8 6.3 Sub Procedures Format of a procedure definition Sub procedure-name(parameter-list) declarations and statements End Sub Procedure header –The first line is known as the procedure header Procedure-name –Directly follows the Sub keyword –Can be any valid identifier –It is used to call this Sub procedure within the program Procedure body –The declarations and statements in the procedure definition form the procedure body
9
2002 Prentice Hall. All rights reserved. 9 6.4 Function Procedures Similar to Sub procedures One important difference –Function procedures return a value to the caller
10
2002 Prentice Hall. All rights reserved. Outline 10 SquareInteger.vb 1 ' Fig. 6.3: SquareInteger.vb 2 ' Function procedure to square a number. 3 4 Module modSquareInteger 5 6 Sub Main() 7 Dim i As Integer ' counter 8 9 Console.WriteLine("Number" & vbTab & "Square" & vbCrLf) 10 11 ' square numbers from 1 to 10 12 For i = 1 To 10 13 Console.WriteLine(i & vbTab & Square(i)) 14 Next 15 16 End Sub ' Main 17 18 ' Function Square is executed 19 ' only when the function is explicitly called. 20 Function Square(ByVal y As Integer) As Integer 21 Return y ^ 2 22 End Function ' Square 23 24 End Module ' modSquareInteger The For structure displays the results of squaring the Integer s from 1-10 Square is invoked with the expression Square(i) The Return statement terminates execution of the procedure and returns the result of y ^ 2
11
2002 Prentice Hall. All rights reserved. Outline 11 SquareInteger.vb Program Output Number Square 1 2 4 3 9 4 16 5 25 6 36 7 49 8 64 9 81 10 100
12
2002 Prentice Hall. All rights reserved. 12 6.4 Function Procedures Format of a Function procedure definition Function procedure-name(parameter-list) As return-type declarations and statements End Function Return-type –Indicates the data type of the result returned from the Function to its caller Return expression –Can occur anywhere in a Function –It returns exactly one value –Control returns immediately to the point at which that procedure was invoked
13
2002 Prentice Hall. All rights reserved. 13 6.5 Methods Definition of method –A method is any procedure that is contained within a class FCL methods Custom methods in programmer-defined classes
14
2002 Prentice Hall. All rights reserved. Outline 14 Maximum.vb 1 ' Fig. 6.4: Maximum.vb 2 ' Program finds the maximum of three numbers input. 3 4 Public Class FrmMaximum 5 Inherits System.Windows.Forms.Form 6 7 ' prompts for three inputs 8 Friend WithEvents lblOne As System.Windows.Forms.Label 9 Friend WithEvents lblTwo As System.Windows.Forms.Label 10 Friend WithEvents lblThree As System.Windows.Forms.Label 11 12 ' displays result 13 Friend WithEvents lblMaximum As System.Windows.Forms.Label 14 15 ' read three numbers 16 Friend WithEvents txtFirst As System.Windows.Forms.TextBox 17 Friend WithEvents txtSecond As System.Windows.Forms.TextBox 18 Friend WithEvents txtThird As System.Windows.Forms.TextBox 19 20 ' reads inputs and calculate results 21 Friend WithEvents cmdMaximum As System.Windows.Forms.Button 22 23 ' Visual Studio.NET generated code 24 25 ' obtain values in each text box, call procedure Maximum 26 Private Sub cmdMaximum_Click(ByVal sender As System.Object, _ 27 ByVal e As System.EventArgs) Handles cmdMaximum.Click 28 These are declarations of all the controls used in the GUI. Create these components visually, using the Toolbox Remember that all forms inherit from class System.Windows.Forms.Form Event handler cmdMaximum_Click Handles the event in which Button cmdMaximum is clicked
15
2002 Prentice Hall. All rights reserved. Outline 15 Maximum.vb Program Output 29 Dim value1, value2, value3 As Double 30 31 value1 = txtFirst.Text 32 value2 = txtSecond.Text 33 value3 = txtThird.Text 34 35 lblMaximum.Text = Maximum(value1, value2, value3) 36 End Sub ' cmdMaximum_Click 37 38 ' find maximum of three parameter values 39 Function Maximum(ByVal valueOne As Double, _ 40 ByVal valueTwo As Double, ByVal valueThree As Double) 41 42 Return Math.Max(Math.Max(valueOne, valueTwo), valueThree) 43 End Function ' Maximum 44 45 End Class ' FrmMaximum The values in the three TextBox es are retrieved using the Text property Call to methods defined in the class that contains the method call need only specify the method name Call to methods that are defined in a class in the FCL must include the class name and the dot (. ) operator
16
2002 Prentice Hall. All rights reserved. 16 6.5 Methods Fig. 6.5Parameter Info feature of the Visual Studio.NET IDE. Parameter Info window
17
2002 Prentice Hall. All rights reserved. 17 6.5 Methods Fig. 6.6IntelliSense feature of the Visual Studio.NET IDE.
18
2002 Prentice Hall. All rights reserved. 18 6.5 Methods Fig. 6.7Math class methods.
19
2002 Prentice Hall. All rights reserved. 19 6.5 Methods Fig. 6.7Math class methods.
20
2002 Prentice Hall. All rights reserved. 20 6.6 Argument Promotion Coercion of arguments –The forcing of arguments to be appropriate data type so that they can be passed to a procedure Widening conversion –Occurs when a type is converted to another type without losing data Narrowing conversion –Occurs when there is potential for data loss during the conversion
21
2002 Prentice Hall. All rights reserved. 21 6.6 Argument Promotion Fig. 6.8Widening conversions.
22
2002 Prentice Hall. All rights reserved. 22 6.7 Option Strict and Data-Type Conversions Option Explicit –Set to On by default –Forces the programmer to declare explicitly all variables before they are used Option strict –Set to Off by default –When set to On, it forces the programmer to perform an explicit conversion for all narrowing conversions Class Convert –The methods in class Convert change data types explicitly
23
2002 Prentice Hall. All rights reserved. 23 6.7 Option Strict and Data-Type Conversions Fig. 6.9Property Pages dialog with Option Strict set to On.
24
2002 Prentice Hall. All rights reserved. 24 6.8 Value Types and Reference Types Variable of a value type –Contains the actual data –Used for a single piece of data Integer Double Variable of a reference type –Contains a location in memory where –Known as objects Literals –Values typed directly in program code –Corresponds to one of the primitive data types
25
2002 Prentice Hall. All rights reserved. 25 6.8 Value Types and Reference Types Fig. 6.10Visual Basic primitive data types.
26
2002 Prentice Hall. All rights reserved. 26 6.8 Value Types and Reference Types Fig. 6.11Literals with type characters.
27
2002 Prentice Hall. All rights reserved. 27 6.9 Passing Arguments: Pass-by-Value vs. Pass-by-Reference Pass-by-value –The program makes a copy of the argument’s value and passes that copy to the called procedure Pass-by-reference –The caller gives the called procedure the ability to access and modify the caller’s original data directly.
28
2002 Prentice Hall. All rights reserved. Outline 28 ByRefTest.vb 1 ' Fig. 6.12: ByRefTest.vb 2 ' Demonstrates passing by reference. 3 4 Module modByRefTest 5 6 ' squares three values ByVal and ByRef, displays results 7 Sub Main() 8 Dim number1 As Integer = 2 9 10 Console.WriteLine("Passing a value-type argument by value:") 11 Console.WriteLine("Before calling SquareByValue, " & _ 12 "number1 is {0}", number1) 13 SquareByValue(number1) ' passes number1 by value 14 Console.WriteLine("After returning from SquareByValue, " & _ 15 "number1 is {0}" & vbCrLf, number1) 16 17 Dim number2 As Integer = 2 18 19 Console.WriteLine("Passing a value-type argument" & _ 20 " by reference:") 21 Console.WriteLine("Before calling SquareByReference, " & _ 22 "number2 is {0}", number2) 23 SquareByReference(number2) ' passes number2 by reference 24 Console.WriteLine("After returning from " & _ 25 "SquareByReference, number2 is {0}" & vbCrLf, number2) 26 27 Dim number3 As Integer = 2 28 When number1 is passed, a copy of the value is passed to the procedure A reference to the value stored in number2 is being passed
29
2002 Prentice Hall. All rights reserved. Outline 29 ByRefTest.vb 29 Console.WriteLine("Passing a value-type argument" & _ 30 " by reference, but in parentheses:") 31 Console.WriteLine("Before calling SquareByReference " & _ 32 "using parentheses, number3 is {0}", number3) 33 SquareByReference((number3)) ' passes number3 by value 34 Console.WriteLine("After returning from " & _ 35 "SquareByReference, number3 is {0}", number3) 36 37 End Sub ' Main 38 39 ' squares number by value (note ByVal keyword) 40 Sub SquareByValue(ByVal number As Integer) 41 Console.WriteLine("After entering SquareByValue, " & _ 42 "number is {0}", number) 43 number *= number 44 Console.WriteLine("Before exiting SquareByValue, " & _ 45 "number is {0}", number) 46 End Sub ' SquareByValue 47 48 ' squares number by reference (note ByRef keyword) 49 Sub SquareByReference(ByRef number As Integer) 50 Console.WriteLine("After entering SquareByReference" & _ 51 ", number is {0}", number) 52 number *= number 53 Console.WriteLine("Before exiting SquareByReference" & _ 54 ", number is {0}", number) 55 End Sub ' SquareByReference 56 57 End Module ' modByRefTest ByVal indicates that value-type arguments should be passed by value ByRef gives direct access to the value stored in the original variable Enclosing arguments in parenthesis forces pass-by-value
30
2002 Prentice Hall. All rights reserved. Outline 30 Program Output Passing a value-type argument by value: Before calling SquareByValue, number1 is 2 After entering SquareByValue, number is 2 Before exiting SquareByValue, number is 4 After returning from SquareByValue, number1 is 2 Passing a value-type argument by reference: Before calling SquareByReference, number2 is 2 After entering SquareByReference, number is 2 Before exiting SquareByReference, number is 4 After returning from SquareByReference, number2 is 4 Passing a value-type argument by reference, but in parentheses: Before calling SquareByReference using parentheses, number3 is 2 After entering SquareByReference, number is 2 Before exiting SquareByReference, number is 4 After returning from SquareByReference, number3 is 2
31
2002 Prentice Hall. All rights reserved. 31 6.10 Duration of Identifiers Identifier’s duration –Period during which the identifier exists in memory Identifier’s scope –Portion of a program in which the variable’s identifier can be referenced Automatic duration –Identifiers that represent local variables in a procedure have automatic duration Instance variable –A variable declared in a class –They exist as long as their containing class is loaded in memory
32
2002 Prentice Hall. All rights reserved. 32 6.11 Scope Rules Possible scopes –Class scope Begins at the class identifier after keyword Class and terminates at the End Class statement –Module scope Variable declared in a module have module scope, which is similar to class scope –Namespace scope Procedures defined in a module have namespace scope, which generally means that they may be accessed throughout a project –Block scope Identifiers declared inside a block, such as the body of a procedure definition or the body of an If/Then selection structure, have block scope
33
2002 Prentice Hall. All rights reserved. Outline 33 Scoping.vb 1 ' Fig. 6.13: Scoping.vb 2 ' Demonstrates scope rules and instance variables. 3 4 Public Class FrmScoping 5 Inherits System.Windows.Forms.Form 6 7 Friend WithEvents lblOutput As System.Windows.Forms.Label 8 9 ' Windows Form Designer generated code 10 11 ' instance variable can be used anywhere in class 12 Dim value As Integer = 1 13 14 ' demonstrates class scope and block scope 15 Private Sub FrmScoping_Load(ByVal sender As System.Object, _ 16 ByVal e As System.EventArgs) Handles MyBase.Load 17 18 ' variable local to FrmScoping_Load hides instance variable 19 Dim value As Integer = 5 20 21 lblOutput.Text = "local variable value in" & _ 22 " FrmScoping_Load is " & value 23 24 MethodA() ' MethodA has automatic local value 25 MethodB() ' MethodB uses instance variable value 26 MethodA() ' MethodA creates new automatic local value 27 MethodB() ' instance variable value retains its value 28 29 lblOutput.Text &= vbCrLf & vbCrLf & "local variable " & _ 30 "value in CScoping_Load is " & value 31 End Sub ' FrmScoping_Load 32 33 ' automatic local variable value hides instance variable 34 Sub MethodA() 35 Dim value As Integer = 25 ' initialized after each call This variable is hidden in any procedure that declares a variable named value Automatic variable value is destroyed when MethodA terminates None of the method calls modifies this variable – both methods refer to variables in other scopes
34
2002 Prentice Hall. All rights reserved. Outline 34 Scoping.vb 36 37 lblOutput.Text &= vbCrLf & vbCrLf & "local variable " & _ 38 "value in MethodA is " & value & " after entering MethodA" 39 value += 1 40 lblOutput.Text &= vbCrLf & "local variable " & _ 41 "value in MethodA is " & value & " before exiting MethodA" 42 End Sub ' MethodA 43 44 ' uses instance variable value 45 Sub MethodB() 46 lblOutput.Text &= vbCrLf & vbCrLf & "instance variable" & _ 47 " value is " & value & " after entering MethodB" 48 value *= 10 49 lblOutput.Text &= vbCrLf & "instance variable " & _ 50 "value is " & value & " before exiting MethodB" 51 End Sub ' MethodB 52 53 End Class ' FrmScoping When MethodB procedure refers to variable value, the instance variable value (line 12) is used.
35
2002 Prentice Hall. All rights reserved. 35 6.12 Random-Number Generation Random class –Produces values at random –Keyword New creates an object of a specified type and returns the object’s location in memory –Next Method Generates a positive Integer value between zero and the constant Int32.MaxValue (2,147,483,647) When a single argument is passed to Next, the values returned will be in the range from 0 to the value of that argument –Scaling By passing two arguments, the programmer is allowed to specify the bottom range too
36
2002 Prentice Hall. All rights reserved. Outline 36 RandomInteger.vb 1 ' Fig. 6.14: RandomInteger.vb 2 ' Generating random integers. 3 4 Imports System.Windows.Forms 5 6 Module modRandomInteger 7 8 Sub Main() 9 Dim randomObject As Random = New Random() 10 Dim randomNumber As Integer 11 Dim output As String = "" 12 Dim i As Integer 13 14 For i = 1 To 20 15 randomNumber = randomObject.Next(1, 7) 16 output &= randomNumber & " " 17 18 If i Mod 5 = 0 Then ' is i a multiple of 5? 19 output &= vbCrLf 20 End If 21 22 Next 23 24 MessageBox.Show(output, "20 Random Numbers from 1 to 6", _ 25 MessageBoxButtons.OK, MessageBoxIcon.Information) 26 End Sub ' Main 27 28 End Module ' modRandomInteger Note that we must use 7 as the second argument to produce integers in the range from 1-6 Go to the next line every time five numbers are generated
37
2002 Prentice Hall. All rights reserved. 37 6.14 Recursion Recursive procedure –It is a procedure that calls itself either directly or indirectly –It is called to solve a problem –The procedure knows how to solve only the simples case (base case) –For complex problems, the procedure divides the problem into a piece that it can perform and a piece that it does not know how to perform –Recursive call The procedure invokes a fresh copy of itself to work on the smaller problem
38
2002 Prentice Hall. All rights reserved. 38 6.14 Recursion Fig. 6.18Recursive evaluation of 5!. 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 5! 5 * 4! 4 * 3! 3 * 2! 2 * 1! 1 Final value = 120 5! = 5 * 24 = 120 is returned 4! = 4 * 6 = 24 is returned 3! = 3 * 2 = 6 is returned 2! = 2 * 1 = 2 is returned 1 returned (a) Procession of recursive calls(b) Values returned from each recursive call
39
2002 Prentice Hall. All rights reserved. Outline 39 Factorial.vb 1 ' Fig. 6.19: Factorial.vb 2 ' Calculating factorials using recursion. 3 4 Public Class FrmFactorial 5 Inherits System.Windows.Forms.Form 6 7 Friend WithEvents lblEnter As Label ' prompts for Integer 8 Friend WithEvents lblFactorial As Label ' indicates output 9 10 Friend WithEvents txtInput As TextBox ' reads an Integer 11 Friend WithEvents txtDisplay As TextBox ' displays output 12 13 Friend WithEvents cmdCalculate As Button ' generates output 14 15 ' Visual Studio.NET generated code 16 17 Private Sub cmdCalculate_Click(ByVal sender As System.Object, _ 18 ByVal e As System.EventArgs) Handles cmdCalculate.Click 19 20 Dim value As Integer = Convert.ToInt32(txtInput.Text) 21 Dim i As Integer 22 Dim output As String 23 24 txtDisplay.Text = "" 25 26 For i = 0 To value 27 txtDisplay.Text &= i & "! = " & Factorial(i) & vbCrLf 28 Next 29 30 End Sub ' cmdCalculate_Click Conversion from String to Integer
40
2002 Prentice Hall. All rights reserved. Outline 40 Factorial.vb 31 32 ' recursively generates factorial of number 33 Function Factorial(ByVal number As Long) As Long 34 35 If number <= 1 Then ' base case 36 Return 1 37 Else 38 Return number * Factorial(number - 1) 39 End If 40 41 End Function ' Factorial 42 43 End Class ' FrmFactorial If number is greater than 1, a recursive call to Factorial is made with a slightly simpler problem Forgetting to return a value from a recursive procedure can result in logic errors
41
2002 Prentice Hall. All rights reserved. 41 6.16 Recursion vs. Iteration Iteration –Involves an explicit repetition structure –Uses a repetition structure For, While or Do / Loop Until Recursion –Achieves repetition through repeated procedure calls –Uses a selection structure If / Then, If / Then / Else or Select –Recursive calls take time and consume additional memory
42
2002 Prentice Hall. All rights reserved. 42 6.17 Procedure Overloading and Optional Arguments Overloading –Allows multiple procedures with the same name, but differing numbers and types of arguments –The overloading of procedures that perform closely related tasks can make programs more readable and understandable Optional arguments –Defining an argument as optional allows the calling procedure to decide what arguments to pass
43
2002 Prentice Hall. All rights reserved. Outline 43 Overload.vb 1 ' Fig. 6.22: Overload.vb 2 ' Using overloaded methods. 3 4 Public Class FrmOverload 5 Inherits System.Windows.Forms.Form 6 7 Friend WithEvents outputLabel As Label 8 9 ' Visual Studio.NET generated code 10 11 Private Sub FrmOverload_Load(ByVal sender As System.Object, _ 12 ByVal e As System.EventArgs) Handles MyBase.Load 13 14 outputLabel.Text = "The square of Integer 7 is " & _ 15 square(7) & vbCrLf & "The square of Double " & _ 16 "7.5 is " & square(7.5) 17 End Sub ' FrmOverload_Load 18 19 Function Square(ByVal value As Integer) As Integer 20 Return Convert.ToInt32(value ^ 2) 21 End Function ' Square 22 23 Function Square(ByVal value As Double) As Double 24 Return value ^ 2 25 End Function ' Square 26 27 End Class ' FrmOverload The compiler uses a logical name to differ between the two Square methods The compiler might use the logical name “ Square of Integer ” “ Square of Double ” for the Square method that specifies a Double parameter
44
2002 Prentice Hall. All rights reserved. Outline 44 Overload2.vb 1 ' Fig. 6.23: Overload2.vb 2 ' Using overloaded procedures with identical signatures and 3 ' different return types. 4 5 Public Class FrmOverload2 6 Inherits System.Windows.Forms.Form 7 8 Friend WithEvents outputLabel As Label 9 10 ' Visual Studio.NET generated code 11 12 Private Sub FrmOverload2_Load(ByVal sender As System.Object, _ 13 ByVal e As System.EventArgs) Handles MyBase.Load 14 15 outputLabel.Text = "The square of Integer 7 is " & _ 16 square(7) & vbCrLf & "The square of Double " & _ 17 "7.5 is " & square(7.5) 18 End Sub ' FrmOverload2_Load 19 20 Function Square(ByVal value As Double) As Integer 21 Return Convert.ToInt32(value ^ 2) 22 End Function ' Square 23 24 Function Square(ByVal value As Double) As Double 25 Return value ^ 2 26 End Function ' Square 27 28 End Class ' FrmOverload2 Procedure calls cannot be distinguished by return type
45
2002 Prentice Hall. All rights reserved. Outline 45 Overload2.vb Program Output The creating of overloaded procedures with identical parameter lists and different return types produces a syntax error
46
2002 Prentice Hall. All rights reserved. 46 6.17.2 Optional Arguments Optional –Optional arguments are specified in the procedure header with keyword Optional Syntax errors –Not specifying a default value for an Optional parameter is a syntax error –Declaring a non- Optional parameter to the right of an Optional parameter is a syntax error
47
2002 Prentice Hall. All rights reserved. Outline 47 Power.vb 1 ' Fig 6.24 Power.vb 2 ' Calculates the power of a value, defaults to square. 3 4 Public Class FrmPower 5 Inherits System.Windows.Forms.Form 6 7 Friend WithEvents txtBase As TextBox ' reads base 8 Friend WithEvents txtPower As TextBox ' reads power 9 10 Friend WithEvents inputGroup As GroupBox 11 12 Friend WithEvents lblBase As Label ' prompts for base 13 Friend WithEvents lblPower As Label ' prompts for power 14 Friend WithEvents lblOutput As Label ' displays output 15 16 Friend WithEvents cmdCalculate As Button ' generates output 17 18 ' Visual Studio.NET generated code 19 20 ' reads input and displays result 21 Private Sub cmdCalculate_Click(ByVal sender As System.Object, _ 22 ByVal e As System.EventArgs) Handles cmdCalculate.Click 23 24 Dim value As Integer 25 26 ' call version of Power depending on power input 27 If Not txtPower.Text = "" Then 28 value = Power(Convert.ToInt32(txtBase.Text), _ 29 Convert.ToInt32(txtPower.Text)) 30 Else 31 value = Power(Convert.ToInt32(txtBase.Text)) 32 End If 33 34 lblOutput.Text = Convert.ToString(value) 35 End Sub ' cmdCalculate_Click Determines whether txtPower contains a value
48
2002 Prentice Hall. All rights reserved. Outline 48 Power.vb 36 37 ' use iteration to calculate power 38 Function Power(ByVal base As Integer, _ 39 Optional ByVal exponent As Integer = 2) As Integer 40 41 Dim total As Integer = 1 42 Dim i As Integer 43 44 For i = 1 To exponent 45 total *= base 46 Next 47 48 Return total 49 End Function ' Power 50 51 End Class ' FrmPower When omitted, the Optional argument defaults to the value 2
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.