Download presentation
Presentation is loading. Please wait.
1
Section 3.6 BUILT-IN FUNCTIONS involving numbers & strings
2
Functions - terminology §Functions are called from within a procedure. l 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) input Function name Output assigned to n
3
Numeric functions §Sqr §Int §Round §Rnd
4
Numeric functions - Sqr §Sqr (n) : returns the square root of n. l requires exactly one positive numeric input value l may contain an expression for n §Examples: assume n = 25 Sqr (16) returns 4 Sqr (2) returns 1.414214 Sqr (n) returns 5 Sqr (n + 11) returns 6 Sqr (-4) returns an error
5
Numeric functions - Int §Int (n) returns the greatest integer less than or equal to n l requires exactly one integer input value l 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
6
Numeric functions - Round §How numbers are rounded to integers: l 23.7 rounds up to 24 l 23.4 rounds down to 23 l 23.5 rounds up to 24 nRound (n) 1.3 1 1.7 2 -1.6-2.2 0
7
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
8
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
9
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
10
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...
11
More uses of Rnd §Randomly generate l a lowercase alphabetic character l an uppercase alphabetic character l the uppercase alphabetic characters between A & J
12
Still more examples of Rnd §Randomly generate an integer between l 1 & 10 l 3 & 23 l 0 & 100 evens only l 5 & 75 divisible by 5 §Randomly generate l a lowercase alphabetic character l an uppercase alphabetic character l p. 132: #116, 117
13
Lab §Page 132 # 121 §Page 133 #122, 124
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.