1 / 13

Section 3.6

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

mandel
Télécharger la présentation

Section 3.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. Section 3.6 BUILT-IN FUNCTIONS involving numbers & strings

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

  3. Numeric functions • Sqr • Int • Round • Rnd

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

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

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

  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 • a lowercase alphabetic character • an uppercase alphabetic character • the uppercase alphabetic characters between A & J

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

  13. Lab • Page 132 # 121 • Page 133 #122, 124

More Related