130 likes | 285 Views
Functions. Procedures that always return a value. How To Define Functions. Function name ( optional parameters ) As return type Local declarations (optional) Commands to carry out functionality (optional) Return value to be returned End Function E.g. Function Pi () As Single
E N D
Functions Procedures that always return a value
How To Define Functions • Functionname (optional parameters) Asreturn type • Local declarations (optional) • Commands to carry out functionality (optional) • Returnvalue to be returned • End Function • E.g. • Function Pi () As Single • Return 3.1416 • End Function
How To Call A Function • Area = Pi() * Radius ^ 2 • E.g. • Function Pi () As Double • Return 3.1416 • End Function The function Pi() is called, after which the value returned by the function is used in the calculation of the area
How To Define Functions With Parameters The value is supplied by the calling program The result is returned to the calling program • E.g. • Function FtoC (ByVal Temp As Single) As Single • ‘ Coverts temperature value in Fahrenheit to Celsius • Return ((5 / 9) * Temp – 32) • End Function Note that you could have saved the calculated Celsius value in a variable and then returned that variable. What is shown above is a short-cut.
How To Call A Function With Parameters • E.g. • Dim F As Single, C As Single • F = txtFahrenheit.Text • C = FtoC (F) • E.g. • Function FtoC (ByVal Temp As Single) As Single • ‘ Coverts a temperature in Fahrenheit to Celsius • Return (5 / 9) * Temp – 32) • End Function
How To Define A Function With Parameters • E.g. • Function GetTax (Salary As Decimal, _ • TaxRate As Decimal) As Decimal • ‘ TaxRate is expressed as percentage; e.g. 22 meaning 22% • Return Salary * (TaxRate / 100) • End Function
How To Call A Function With Parameters • E.g. • TaxDue = GetTax (21000, 23) • Function GetTax (Salary As Decimal, _ • TaxRate As Decimal) _ • As Decimal • ‘ TaxRate is expressed as percentage; e.g. 22% • Return Salary * (TaxRate / 100) • End Function
How To Define A Function With Parameters Function Largest ( First As Integer, _ Second As Integer, _ Third As Integer) As Integer If First >= Second And First >= Third Then Return First ElseIf Second >= First And Second >= Third Then Return Second ElseIf Third >= First And Third >= Second Then Return Third End If End Function You may have several Return commands in your code
Exercise Write a function to calculate the circumference of a circle and return the result. The radius value is given as a parameter. Function CalcCircumf (ByVal Radius As Single) As Single Return 2 * Pi() * Radius End Function Note the way the Pi() function is used (or called). Pi() must be already declared and visible to this function
Exercise • Write a function to calculate the tax due on a given salary value, according to the following table. Salary is a parameter. • Salary Tax Due • up to 38000 22% of Salary • above 38000 40% of Salary • Function CalcTax (ByVal Salary As Decimal) As Decimal • If Salary <= 38000 Then • Return Salary * 0.22 • Else • Return Salary * 0.4 • End If • End Function
Exercise Write an function to increment the value of its parameter by 1, capped at 99. e.g., 67 should be incremented to 68, 98 to 99, but 105 should be reduced to (i.e., capped at) 99. Function Inc (Value As Integer) As Integer Dim NewValue As Integer NewValue = Value + 1 If NewValue > 99 Then NewValue = 99 Return NewVal End Function
Exercise Write a function to calculate the wage due from hours worked per week and hourly pay rate. Function Wage (Hours As Integer, Rate As Decimal) As Decimal Return Hours * Rate End Function
Exercise Write a function to calculate the wage due from hours worked per week and hourly pay rate; however, if hours worked is above 40, then the rate is increased by 50% for the extra hours. Function Wage (Hours As Integer, Rate As Decimal) As Decimal if Hours <= 40 Then Return Hours * Rate Else Return (40 * Rate) + ((Hours - 40) * (Rate * 1.5)) End Function