1 / 13

Functions

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

ping
Download Presentation

Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Functions Procedures that always return a value

  2. 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

  3. 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

  4. 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.

  5. 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

  6. 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

  7. 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

  8. 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

  9. 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

  10. 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

  11. 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

  12. 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

  13. 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

More Related