280 likes | 495 Views
Chapter 5 - General Procedures. 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design. Devices for Modularity. Visual Basic has two strategies for breaking problems into smaller pieces: Function procedures (Section 5.1)
E N D
Chapter 5 - General Procedures 5.1 Function Procedures 5.2 Sub Procedures, Part I 5.3 Sub Procedures, Part II 5.4 Modular Design
Devices for Modularity Visual Basic has two strategies for breaking problems into smaller pieces: • Function procedures (Section 5.1) • Sub procedures (Section 5.2, 5.3)
5.1 Function Procedures • User-Defined Functions • Having No Parameters • Having One Parameter • Having Multiple Parameters • Return • Any single value of any data type including Boolean
Some Built-In Functions Function: Int Example: Int(2.6) is 2 Input: double Output: integer Function: Math.Round Example: Math.Round(1.23, 1) is 1.2 Input: double, integer Output: double
Some Built-In Functions (continued) Function:FormatPercent Example: FormatPercent(0.12, 2) is 12.00% Input: double, integer Output: string Function: FormatNumber Example: FormatNumber(12.62, 1) is 12.6 Input: double, integer Output: string
Function Procedures • Function procedures (also called user-defined functions) always return one value • Syntax: FunctionFunctionName(ByValvar1 As Type1, ByValvar2 As Type2, ...) As ReturnDataType statement(s) Returnexpression End Function
Create a program that calls a function that converts a Fahrenheit temperature to Celsius. Example 1: Form txtTempF txtTempC
Creating a Function • Pick a name that describes the purpose of the function…FtoC in our case. • What is the input for this function? • Input(s) are the arguments or parameters in the function call • Temperature in Fahrenheit is the input • What is the output for the function? • This is the single value calculated and returned by the user-defined function. • Temperature in Celsius is the output
3 Ways to Make aFunction Call • Just like calling: listbox1.items.add(Math.Sqrt(9)) textbox1.text = Math.Sqrt(9) answer = Math.Sqrt(9) • We will make a call to FtoC and pass the fahrenheit temperature: listbox1.items.add(FtoC(fahrenheitTemp)) textbox1.text = FtoC(fahrenheitTemp) celsius= FtoC(fahrenheitTemp)
Defining Function FtoC • Type “Function FtoC” in the Class • Type “(t As Double)” – this single parameter identifies the fahrenheit value being passed in. • Type “As Double” at the end -- tells the data type of the value to return – in this case a celsius value which is a double. Function FtoC(ByVal t AsDouble) AsDouble 'Convert Fahrenheit temp to Celsius Return (5 / 9) * (t - 32) EndFunction The value retuned must be the same data type as defined here!
Example 1: The Complete Code Private Sub btnConvert_Click(...) _ Handles btnConvert.Click Dim fahrenheitTemp, celsiusTemp As Double fahrenheitTemp = CDbl(txtTempF.Text) celsiusTemp = FtoC(fahrenheitTemp) txtTempC.Text = CStr(celsiusTemp ) End Sub Function FtoC(ByVal t AsDouble) AsDouble Return (5 / 9) * (t - 32) EndFunction FtoC is called with 212° from txtTempF t is passed the value 212 Since (5/9)*(212-32) = 100, 100 is returned to the calling statement and assigned to celsiusTemp
Functions and Their Data Types Private Sub btnConvert_Click(...) Handles btnConvert.Click Dim fahrenheitTemp, celsiusTemp As Double fahrenheitTemp = CDbl(txtTempF.Text) celsiusTemp = FtoC(fahrenheitTemp) txtTempC.Text = CStr(celsiusTemp ) End Sub Function FtoC(ByVal t AsDouble) AsDouble Return(5 / 9) * (t - 32) EndFunction The data type of the return value must be the same as the data type at the end of function FtoCand the variable that is assigned a value from FtoC
Create a Function to return someone’s first name given their full name. Example 2: Form txtFullName txtFirstName
Defining the FirstName Function • Name the Function • FirstName • Argument to Parameter pass • fullName – someone’s full name (string) • Return Value • fristName – the person’s first name (string)
Creating the Calling Statement • The following statement calls the FirstNamefucntion with an agrument called fullName, calculates a value in the fucntion and assigns the return value to a textbox. txtFirstName.Text= FirstName(fullName)
Defining Function FirstName • Type “Function FirstName” in the Class • Type “(fullName As String)” – this single parameter identifies the full name being passed in. • Type “As String” at the end -- tells the data type of the value to return – in this case their first name. 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) EndFunction
Example 2: The Completed Code Private Sub btnDetermine_Click(...) HandlesbtnDetermine.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 = fullName.IndexOf(" ") ReturnfullName.Substring(0, firstSpace) End Function fullName is assigned“Franklin Delano Roosevelt” from txtFullName Franklin Delano Roosevelt is passed to fullName Franklin Since firstSpace = 8 and fullName.Substring(0,8) = “Franklin” is returned to the calling statement and assigned to txtFirstName
Create a Function to return someone’s earnings given their hourly wage and # of hours worked. Example 3: Passing Multiple Parameters txtWage txtHours txtEarnings
Defining the Earnings Function • Name the Function • Pay • Argument to Parameter pass • hourlyWage– dollars paid per hour (double) • hoursWorked – # of hours worked (double) • Return Value • Pay – dollars paid for the job (double)
Example 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 Function call
User-Defined Pay Function With Multiple Parameters Function Pay(ByVal wage As Double, ByVal hrs As Double) As Double Dim amt AsDouble If hrs <= 40 then amt = wage * hrs Else amt = wage * 40 +(1.5 * wage * (hrs – 40)) End If Return amt End Function Function header The dimmed data type for amt must be the same as the data type at the end of the function header!
Example 3: The Complete 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 Function Pay(ByVal wage As Double,ByVal hrs As Double) As Double Dim amt AsDouble 'amount of salary If hrs <= 40 then amt = wage * hrs Else amt = wage * 40 +(1.5 * wage * (hrs – 40)) End If Return amt End Function 14.50 45 14.50 45 Since hrs is > 40, amt = 14.50*40+(1.5*14.5*(45-40)) = 688.75 688.75
User-Defined Function Having No Parameters • Some functions like Math.Pi ( ) do not have parameters. • We will NOT create functions with inputs entered inside of the function. Function CostOfItem() As Double Dim price AsDouble = CDbl(txtPrice.Text) Dim quantity AsInteger = CDbl(txtQuantity.Text) Dim cost = price * quantity Returncost EndFunction
User-Defined Boolean-Valued Function Function IsVowelWord(ByVal word As String) As Boolean Dim found as Boolean = False If word.IndexOf("A") <> -1 Then found =True End If . . . If word.IndexOf("U") <> -1 Then found = True End If Returnfound End Function