1 / 12

Visual Basic Programming II Lecture 6

Visual Basic Programming II Lecture 6. MIS233 Instructor – Larry Langellier. This Week – Numeric Functions. How functions work Writing your own functions Using Built-in functions Numeric conversion Mathematical Random Number generation Financial. Numeric Functions.

freira
Download Presentation

Visual Basic Programming II Lecture 6

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. Visual Basic Programming IILecture 6 MIS233 Instructor – Larry Langellier

  2. This Week – Numeric Functions • How functions work • Writing your own functions • Using Built-in functions • Numeric conversion • Mathematical • Random Number generation • Financial

  3. Numeric Functions • A Function is like a Procedure except it returns a value • Basic Syntax [Public|Private] Functionname (arg-list) Astype function statements End Function • Example Public Function Volume(Length, Width, _ Height) As Double Volume = Length * Width * Height End Function • Assign a value (=) to the function name to return the value

  4. Calling the Function • Calling a Function is similar to calling a Procedure, except since the function returns a value you must store the “return value” in a variable • Examples Dim dblBoxVolume As Double dblBoxVolume = Volume (4, 5, 6) lblVolume.Caption = Volume (2, 4, 6)

  5. Common Mistakes • Not assigning a return value inside the function • Not storing the return value to a variable when you call the function • Not separating argument values with commas lblVolume.Caption = Volume (3 * 5 * 7) • Defining the function with the keyword Sub

  6. Writing Numeric Function Procedures • Visual Basic contains a number of built-in numeric functions – which we’ll see in a little bit • You will also want to write your own numeric functions on numerous occasions • Examples • 16.1 – Convert Race Results • 16.2 – Returning the Highest/Lowest Values

  7. Int(number) removes the fractional part of number, returns the next lower number for negatives Fix(number) removes the fractional part of number Round(expression [, numdecs] ) rounds the expression to the specified number of decimals (0 is the default) CInt(expression), CLng(expression), CSng(expression), CDbl(expression), CCur(expression), CStr(expression), CVar(expression) Converts expression to the specified data type Built-In NumericConversion Functions

  8. Built-In Mathematical Functions Exp(number) Atn(angle) • raises e to a power - arctangent of an angle Log(number) Sin(angle) • computes the natural log - sine of an angle Sqr(number) Cos(angle) • the square root - cosine of an angle Abs(number) Tan(angle) • the absolute value - tangent of an angle Sgn(number) • the sign of number • Example 16.3

  9. Generating Random Numbers • Randomize( ) • Seeds the random number generator • Rnd [(number)] < 0, The same value every time > 0, The next random number value (the default) = 0, The value most recently generated Returns a value between 0 and 0.999999 • Example • an integer between 1 and 6 Int((Rnd * 6) + 1) • Examples 16.4 and 16.5

  10. Financial Functions - Annuities • Used when working with Interest and it’s effect on the value of money – let’s look these up in the Help System… FV(rate, nper, pmt, pv) • What will the future value be? IPmt(rate, per, nper, pv) • How much interest is paid/received for a specific period? NPer(rate, pmt, pv, fv) • How many payment periods will be required? Pmt(rate, nper, pv, fv) • How much should the payment be per period? PPmt(rate, per, nper, pv, fv) • How much principle is paid for a specific period? Rate(nper, pmt, pv, fv) • What interest rate will yield the desired results?

  11. Financial Functions – Cash Flow • Computations related to cash flow – a series of payments and receipts NPV(rate, values()) • Net Present Value PV(rate, nper, pmt, fv) • Present Value IRR(values()) • Internal Rate of Return MIRR(values(),finance_rate, reinvest_rate) • Modified Rate of Return • Example 16.6

  12. Details… • Next Class • Read Chapter 17: String Functions • Homework #7 is due prior to the start of class • Following Class • The Midterm Project will be due at the beginning of class

More Related