140 likes | 353 Views
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.
E N D
Visual Basic Programming IILecture 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 • 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
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)
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
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
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
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
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
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?
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
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