130 likes | 238 Views
Section 3.6. BUILT-IN FUNCTIONS involving numbers & strings. Functions - terminology. Functions are called from within a procedure. e.g., Sqr(x) is a function call A function receives 1 or more input values & returns a unique output value n = Left (string, 3).
E N D
Section 3.6 BUILT-IN FUNCTIONS involving numbers & strings
Functions - terminology • Functions arecalled from within a procedure. • e.g., Sqr(x) is a function call • A function receives 1 or more input values & returns auniqueoutput value n = Left (string, 3) Output assigned to n Function name input
Numeric functions • Sqr • Int • Round • Rnd
Numeric functions - Sqr • Sqr (n) : returns the square root of n. • requires exactly one positive numeric input value • may contain an expression for n • Examples: assumen = 25 Sqr (16) returns 4 Sqr (2) returns 1.414214 Sqr (n) returns 5 Sqr (n + 11) returns 6 Sqr (-4) returns an error
Numeric functions - Int • Int (n) returns the greatest integer less than or equal to n • requires exactly one integer input value • n may be a numeric expression • Examples: assume n is 10 Int (5.2) returns 5 Int (-6.7) returns -7 Int (n + 15) returns 25
Numeric functions - Round • How numbers are rounded to integers: • 23.7 rounds up to 24 • 23.4 rounds down to 23 • 23.5 rounds up to 24 n Round (n) 1.3 1 1.7 2 -1.6 -2 .2 0
Another format for Round • Round (2.357, 2) returns 2.36 • Round (1234.97899, 2) returns 1234.98 • This variation of Round is useful when our programs are processing monetary values. • Example: Private Sub Command1_Click() Picture1.Print Round(txtNum.Text, 2) End Sub
Two mathematical operators that are useful in programs • If x & y are integers, then the integer quotient of x divided by y can be found by using \ quotient = x \ y • Also, to get the remainder, use mod: remainder = x mod y • try it with: x = 26; y = 7
Numeric functions - Rnd • Used to enable your program to generate pseudorandom values; useful in the creation of games. • Rnd does not require any input value and returns a numeric value r where 0 <= r < 1 Int (Rnd * 10)will generate a value that is between 0 & 9
Some uses of Rnd • Randomly generate an integer between 1 & 10: x = 1 + Int(10 * Rnd) • Randomly generate a 0 or 1: x = Int (2 * Rnd) • Randomly generate evens between 0 & 98: x = 2 * Int (50 * Rnd) more...
More uses of Rnd • Randomly generate • a lowercase alphabetic character • an uppercase alphabetic character • the uppercase alphabetic characters between A & J
Still more examples of Rnd • Randomly generate an integer between • 1 & 10 • 3 & 23 • 0 & 100 evens only • 5 & 75 divisible by 5 • Randomly generate • a lowercase alphabetic character • an uppercase alphabetic character • p. 132: #116, 117
Lab • Page 132 # 121 • Page 133 #122, 124