Uploaded by Skye Khuu

Starting out with Programming Logic and Design, 5th Edition, by Tony Gaddis (Function Table)

advertisement
Table 6-2 Mathematical functions (modified from Starting out with Programming Logic and Design, 5th
Edition, by Tony Gaddis)
Function
Name
random
Description and Example Usage
Returns a random Integer
Example: random(1, 100) is a call to the random function. The arguments tell
the function to give a random number in the range of 1 through 100.
sqrt
Returns a square root of a number.
Example: Set result = sqrt(16) This statement calls the sqrt function,
passing 16 as an argument. The function returns the square root of 16, which
is then assigned to the result variable.
pow
The purpose of the pow function is to raise a number to a power. In a nutshell,
it does the same thing that we have been using the ^ operator for.
Example: Set area = pow(4, 2) This statement calls the pow function,
passing 4 and 2 as arguments. The function returns the value of 4 raised to the
power of 2, which is assigned to the area variable.
abs
Returns the absolute value of the argument.
Example: After the statement y = abs(x)executes, the variable y will contain
the absolute value of the value in x. The variable x will remain unchanged.
round
Accepts a real number as an argument and returns the value of the argument
rounded to the nearest integer.
Example: After the statement y = round(x)executes, the variable y will contain
the value of the variable x rounded to the nearest integer.
cos
Returns the cosine of the argument. The argument should be an angle
expressed in radians.
Example: After the statement y = cos(x) executes, the variable y will contain
the cosine of the angle stored in the variable x.
sin
Returns the sine of the argument.
tan
Returns the tangent of the argument.
Table 6-3 Data Type Conversion Functions
Function Name
toInteger
Description and Example Usage
The toInteger function accepts a real number as its argument and
returns that number converted to an integer. If the real number has a
fractional part, the fractional part will be thrown away. For example,
the function call toInteger(2.5) will return 2.
Example: If the following were actual code, the variable i would contain
the value 2 after these statements execute.
Declare Integer i
Declare Real r = 2.5
Set i = toInteger(r)
toReal
The toReal function accepts an integer number as its argument and
returns that number converted to a real number.
Example: If the following were actual code, the variable r would contain
the value 7.0 after these statements execute.
Declare Integer i = 7
Declare Real r
Set r = toReal(i)
currencyFormat
The function currencyFormat that accepts a Real number as an
argument and returns a string containing the number formatted as a
currency amount.
Example: Declare Real amount = 6450.879
Display currencyFormat(amount)
If this pseudocode were an actual program it would display:
$6,450.88
length
The length function returns the length of a string.
Example: In following program segment, the length of a password is checked
to make sure it is at least six characters long.
Display "Enter your new password."
Input password
If length(password) < 6 Then
Display "The password must be at least six
characters long."
End If
Function Name
Description and Example Usage
append
The append function concatenate two strings.
Example: The following pseudocode shows an example of its usage:
Declare String lastName = "Conway"
Declare String salutation = "Mr. "
Declare String properName
Set properName = append(salutation, lastName)
Display properName
toUpper
The toUpper function accepts a string as an argument and returns a
string that is a copy of the argument, but with all characters converted
to uppercase.
Example: Declare String str = "Hello World!"
Display toUpper(str)
If this pseudocode were an actual program, it would display:
HELLO WORLD!
toLower
The toLower function accepts a string as an argument and returns a
string that is a copy of the argument, but with all characters converted
to lowercase.
Example: Declare String str = "WARNING!"
Display toLower(str)
If this pseudocode were an actual program, it would display:
warning!
substring
This function returns a substring, which is a string within a string.
The substring function typically accepts three arguments: (1) a string
that you want to extract a substring from, (2) the beginning position of
the substring, and (3) the ending position of the substring.
Example: Declare String str = "New York City"
Declare String search
Set search = substring(str, 5, 7)
Display search
If this pseudocode were an actual program, it would display:
ork
Function Name
Description and Example Usage
contains
The contains function accepts two strings as arguments. It
returns True if the first string contains the second string; otherwise, the
function returns False.
Example:
Declare string1 = "four score and seven years ago"
Declare string2 = "seven"
If contains(string1, string2) Then
Display string2, " appears in the string."
Else
Display string2, " does not appear in the string."
End If
If this were actual code in a program, it would display
seven appears in the string.
stringToInteger
The stringToInteger function accepts a string as an argument,
converts it to an Integer.
Example:
The following statement converts the contents of the str variable to
an Integer and stores it in the intNumber variable.
Set intNumber = stringToInteger(str)
stringToReal
The stringToReal function works the same way as the
stringToInteger, but it converts a string to a Real.
isInteger
Test a string and then return either True or False indicating whether
the string can successfully be converted to an integer.
Example: Assume str is a String and intNumber is an Integer.
If isInteger(str) Then
Set intNumber = stringToInteger(str)
Else
Display "Invalid data"
End If
isReal
Test a string and then return either True or False indicating whether
the string can successfully be converted to a real number.
Example: Assume str is a String and realNumber is a Real).
If isReal(str) Then
Set realNumber = stringToReal(str)
Else
Display "Invalid data"
End If
Download