Note - PowerPoint PPT Presentation

About This Presentation
Title:

Note

Description:

'Dog' 1 'Ca' 'D' What do we need, to specify a function? A name. A parameter list. ... If (Trim(strA) = 'Male') Then. This is False! Syntax: String = Trim ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 29
Provided by: non882
Learn more at: http://www.cs.ucr.edu
Category:
Tags: dog | male | names | note

less

Transcript and Presenter's Notes

Title: Note


1
Note The first half of these slides duplicate
previous slides. The second half are original
2
Using Subroutines/Methods/Functions
The black box view of a function
2
3
2
6
5
8
3
Here is a different function..
Cat
2
1
Dog
Ca
D
4
Cat
2
  • What do we need, to specify a function?
  • A name
  • A parameter list. A list of the things the
    functions expects, in this case, a string,
    followed by an integer.
  • A return type. In this case a string

Truncate
Ca
5
Cat
  • Visual Basic has built in functions (and later we
    can write our own)
  • Each function has
  • A name
  • A parameter list
  • A return type
  • In this case, the name is UCase, the parameter
    list is a single string and the return type is a
    single string.

UCase
CAT
strFirstName "Homer" bntRedDemo.Text
UCase(strFullName)
6
strFirstName "Homer" bntRedDemo.Text
UCase(strFullName)
bntRedDemo.Text UCase(marge)
strFName John strLName Doe bntRedDemo.Text
UCase(strFName strLName )
Function call Return Value
UCase(Input String) INPUT STRING
UCase(all lowercase) ALL LOWERCASE
UCase(ALL UPPERCASE) ALL UPPERCASE
UCase(UpPeP AnD lOwErCaSE) UPPER AND LOWERCASE
Syntax String UCase(String)
7
A classic use of the UCase function is robust
data entry
strA . If (strA Male Or strA male Or
strA MALE ) Then bntRedDemo.Text You
choose male. End If
strA . If (Ucase(strA) MALE) Then
bntRedDemo.Text You choose male. End If
8
There a complimentary function to UCase called
LCase. It works exactly like you would expect
Syntax String LCase(String)
strFName John strLName Doe bntRedDemo.Text
LCase(strFName strLName )
Function call Return Value
LCase(Input String) input string
LCase(all lowercase) all lowercase
LCase(ALL UPPERCASE) all uppercase
LCase(UpPeP AnD lOwErCaSE) upper and lowercase
9
There a function call Val, which converts strings
to numbers.
Syntax Numeric Value Val(String)
strAge 5 strAgeDiff 100 Val(strAge)
Function call Return Value
Val(199.11) 199.11
Val( 199.11 ) 199.11
Val( 1 99.1 1) 199.11
Val( 199 ) 199
Val(199.11) 0
Val(1,199.11) 1
Val( ) 0
Val(123abc) 123
Val(abc123) 0
Function Name Val Function Description Returns
a numeric representation of the String value
passed to it. Val will convert a String to a
numeric until it reaches a character that is not
a numeric value, a decimal point, or a
white-space character. Once an unrecognizable
character is read, conversion stops at that
point. Syntax Numeric Value Val(String) Example
s
10
There a complementary function call Str, which
converts numbers to strings.
Syntax String Str(Numeric Value)
bntRedDemo.Text Str(25) bntRedDemo.Text
(25).ToString
These are logically equivalent (almost!)
Function Name Str Function Description Returns
a String representation of the numeric value
passed to it. By default it will place a single
space in front of the first numeric character.
11
There is a function called Trim, which removes
trailing and leading space from text.
strA Male If (strA Male) Then
bntRedDemo.Text You choose male. End If
This is False!
This is True
If (Trim(strA) Male) Then
Syntax String Trim(String)
Function call Return Value
Trim( InputString) InputString
Trim(InputString ) InputString
Trim( InputString ) InputString
Trim( Input String ) Input String
12
We can nest functions...
strA Male If ( Trim(UCase(strA) ) MALE)
Then bntRedDemo.Text You choose male. End If
Trim(Ucase(strA))
Trim(Ucase( Male))
Trim( MALE)
MALE
13
We can nest functions...
bntRedDemo.Text Str(5) Str(5)
bntRedDemo.Text Str(5) Trim(Str(5))
14
There is a function called Len, which returns the
number of characters contained in a String
strA If ( Len(strA) gt 10 ) Then
bntRedDemo.Text You have a long name! Else
If ( Len(strA) lt 1 ) The DMV requires at
least 2 letters bntRedDemo.Text This is not
a legal name! End If
Syntax Integer Len(String)
15
There is a function called Mid, which returns a
subsection of a string
Returns a specific number of characters of a
String allowing the developer to indicate where
to start and how many characters to return. The
first parameter is the source String. The second
is an Integer indicating the starting position to
copy from. The third parameter is optional and
indicates the number of characters to copy. If
the third parameter is left out, all characters
from the starting position are returned.
Syntax String Mid(String, integer_type,
integer_type )
Function call Return Value
Mid(This is the String, 6, 2) is
Mid(This is the String, 9, 3) the
Dim shtP As Short 4 Mid(This is the String, 13, shtP ) Stri
Mid(This is the String, 8) the String
Optional!
16
By using functions we can do lots of cool things
Suppose I want to get just the First letter in
someone's name
Dim strFName, strLName , strMName, strFullName As
String strFName Homer strLName
Simpson strMName Jay strFullName
strFName " " Mid(strMName, 1, 1) " "
strLName bntRedDemo.Text strFullName
17
By using functions we can do lots of cool things
Suppose I want to get just the Last letter in a
word
Dim strW, strLastLetter As String strW
Books strLastLetter Mid(strW,len(strW),1)
If ( (UCase(strLastLetter)) S) Then
bntRedDemo.Text The word is probably
plural End If
18
There is a function called Space, which returns a
String containing the number of spaces indicated
by the parameter.
Function Name Space Function Description
Returns a String containing the number of spaces
indicated by the parameter. Common Uses Often
you wish to add spaces to set the total length of
a String to an exact size. This is often used
when working with fixed-width data files. Syntax
String Space(Integer) Examples
Syntax String Space(integer_type)
Function call Return Value
Space(5)
Space(10)
Space(0)
Hello Space(10) Goodbye Hello Goodbye
19
There is a function called InStr, which looks for
a substring in a longer string, and returns its
location
Function Name InStr Function Description
Returns the position of the first occurrence of a
substring that is searched for in the String
passed. Common Uses InStr can be used to tell us
if a String has a certain substring contained
within it. It operates much like searching a
document for a word. Syntax Long InStr(String
to be Searched, Search String) Examples
Syntax Integer InStr(String,String)
Function call Return Value
InStr(This is a very, is) 3
InStr(ab ab ab, ab) 1
InStr(ab ab ab, a) 1
InStr(ab ab ab, c) 0
20
InStr Example
Finding possible Greek names..
Dim strS() As String "Gunopulos",
"Papadopoulos", "Keogh", "Vlachos" Dim intI As
Integer For intI 0 To UBound(strS) If
InStr(strS(intI), "os") gt 0 Then
bntRedDemo.Text strS(intI) " might be greek"
End If Next intI
21
Your code should be clearer, and more robust
Const CommonGreekNameSuffix "OS" For intI 0
To UBound(strS) If InStr(UCase(strS(intI)),
CommonGreekNameSuffix) gt 0 Then
bntRedDemo.Text strS(intI) " might be greek"
End If Next intI
No magic numbers (days of week example) and no
magic strings
22
There are two functions, Int and Fix which
convert real numbers to whole numbers
Function Name Int, Fix Function Description
Returns the Integer portion of the numerical
value passed to it. Common Uses Int or Fix are
used when you wish to convert a numerical value
to an integer without regard for the decimal
value. It performs a truncation of the number.
Int returns the first negative integer less than
or equal to number, whereas Fix returns the first
negative integer greater than or equal to number.
For example, Int converts -8.4 to -9, and Fix
converts -8.4 to -8.
Syntax Integer_type Int(real type) Syntax
Integer_type Fix(real type)
Function call Return Value
Int(199.11) 199
Int(0.1) 0
Int(1.5) 1
Int(0.99999) 0
Fix(199.11) 199
Fix(0.1) 0
23
There is a function IsNumeric which tests to see
if the input is a number
Function Description IsNumeric returns True if
the data type of Expression is Short, Integer,
Long, Decimal, Single, or Short. It also returns
True if Expression is a String that can be
successfully converted to a Double. Common
Uses IsNumeric can be used to verify that a
value can be evaluated as a number. Instead of
possibly getting either inaccurate results or a
run-time error, by using IsNumeric, a proactive
approach to error handling can be achieved.
Syntax Boolean IsNumeric(any type)
Function call Return Value
IsNumeric(199.11) True
IsNumeric(199.11) True
IsNumeric(ABC) False
IsNumeric(123ABC) False
IsNumeric(1,999) True
IsNumeric(1,1999) True
IsNumeric(50) False
IsNumeric(One) False
24
There is a function Rnd which produces random
numbers
Function Description Returns a pseudo random
decimal number that is greater than or equal to 0
and less than 1. By passing Rnd an optional
numeric parameter, you can further control the
type of random number you generate. Common Uses
Random numbers are required for a great many
reasons. By combining the output of the Rnd
function with some basic mathematics, random
numbers can be generated within a given range.
Syntax Single Rnd()
To produce random numbers in a range, from
lowerbound to upperbound Int((upperbound -
lowerbound 1) Rnd lowerbound)
Function call Return Value
Int(Rnd()3) Generates a random number from 0 to 2
Int(Rnd()3)1 Generates a random number from 1 to 3
Int(Rnd()6)1 Generates a random number from 1 to 6
Int(Rnd()6) 1) (Int(Rnd()6) 1) Generates a random number from 2 to 12 similar to rolling a pair of dice
25
Combined Lab/Homework Due in Class Wednesday Nov
3rd . You are encouraged to work in groups of
size two
Imagine that you have 4 parallel arrays, like
this Dim strLastNames() As String
"GUNOPULOS", "PApadopoulos", "Keogh",
"vlachos" Dim strFirstNames() As String
"dima", "JoHn", "Keogh", "MARY" Dim
blnIsMale() As Boolean True, True, True,
False Dim sngGPA() As Single 1.2, 2.4, 4.0,
3.4 Produce the following output Mr. Dima
Gunopulos has a GPA of 1.2 Mr. Eamonn Keogh has a
GPA of 4.0 Mr. John Papadopoulos has a GPA of
2.4 Ms. Mary Vlachos has a GPA of 3.4
26
Dim strLastNames() As String "GUNOPULOS",
"PApadopoulos", "Keogh", "vlachos" Dim
strFirstNames() As String "dima", "JoHn",
"Keogh", "MARY" Dim blnIsMale() As Boolean
True, True, True, False Dim sngGPA() As Single
1.2, 2.4, 4.0, 3.4 Produce the following
output Mr. Dima Gunopulos has a GPA of 1.2 Mr.
Eamonn Keogh has a GPA of 4.0 Mr. John
Papadopoulos has a GPA of 2.4 Ms. Mary Vlachos
has a GPA of 3.4
  • Note that
  • The people are sorted by last name.
  • The sex is indicated by Mr/Ms
  • The first (and only the first) letter of each
    name is capitalized
  • You must demo your code on this example, but it
    should work on any similar example of any size
    (including when you have just one person)

27
We expect meaningful variable names, and
essentially every line of code to be
commented. You can output the text to a textbox
(read the book), or send each line to a button
for one second (like my teaching examples). You
must demo the working code in lab to the TA, and
hand in a high quality print out of the code by
beginning of class.
28
Extra Credit Produce the output in aligned
fashion as shown below. This is very easy to do,
you need to use the Len and the Space functions
Mr. Dima Gunopulos has a GPA of 1.2 Mr. Eamonn
Keogh has a GPA of 4.0 Mr. John
Papadopoulos has a GPA of 2.4 Ms. Mary Vlachos
has a GPA of 3.4
Write a Comment
User Comments (0)
About PowerShow.com