note the first half of these slides duplicate previous slides. the second half are original
DESCRIPTION
Note The first half of these slides duplicate previous slides. The second half are original. Using Subroutines/Methods/Functions. The black box view of a function. 2. 3. 2. 6. 5. 8. Here is a different function. “Cat”. 2. 1. “Dog”. “Ca”. “D”. “Cat”. 2. - PowerPoint PPT PresentationTRANSCRIPT
Note
The first half of these slides duplicate previous slides. The second half are original
Using Subroutines/Methods/FunctionsUsing Subroutines/Methods/Functions
The black box view of a function
2 3 6 2
5 8
Here is a different function..
“Cat” 2 “Dog” 1
“Ca” “D”
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
“Cat” 2
“Ca”
Truncate
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.
“Cat”
“CAT”
UCase
strFirstName = "Homer"
bntRedDemo.Text = UCase(strFullName)
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”
strFirstName = "Homer"
bntRedDemo.Text = UCase(strFullName)
bntRedDemo.Text = UCase(“marge”)
strFName = “John”
strLName = “Doe”
bntRedDemo.Text = UCase(strFName & “ ” & strLName )
Syntax: String = UCase(String)Syntax: String = UCase(String)
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
A classic use of the UCase function is robust data entry…
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”
There a complimentary function to UCase called LCase. It works exactly like you would expect …
Syntax: String = LCase(String)Syntax: String = LCase(String)
strAge = “5”
strAgeDiff = 100 – Val(strAge)
There a function call Val, which converts strings to numbers.
Syntax: Numeric Value = Val(String)Syntax: Numeric Value = Val(String)
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)
Examples:
bntRedDemo.Text = Str(25)
bntRedDemo.Text = (25).ToString
There a complementary function call Str, which converts numbers to strings.
Syntax: String = Str(Numeric Value)Syntax: String = Str(Numeric Value)
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.
These are logically equivalent (almost!)
These are logically equivalent (almost!)
There is a function called Trim, which removes trailing and leading space from text.
Function call Return Value
Trim(“ InputString”) “InputString”
Trim(“InputString ”) “InputString”
Trim(“ InputString ”) “InputString”
Trim(“ Input String ”) “Input String”
strA = “ Male”
If (strA = “Male”) Then bntRedDemo.Text = “You choose male.”End If
If (Trim(strA) = “Male”) Then
This is False!This is False!
Syntax: String = Trim(String)Syntax: String = Trim(String)
This is TrueThis is True
We can nest functions...
strA = “ Male”
If ( Trim(UCase(strA) ) = “MALE”) Then bntRedDemo.Text = “You choose male.”End If
Trim(Ucase(“ Male”))
Trim(Ucase(strA))
Trim(“ MALE”)
“MALE”
We can nest functions...
bntRedDemo.Text = Str(5) & Str(5)
bntRedDemo.Text = Str(5) & Trim(Str(5))
There is a function called Len, which returns the number of characters contained in a String
strA = …
If ( Len(strA) > 10 ) Then bntRedDemo.Text = “You have a long name!”
Else If ( Len(strA) <= 1 ) ‘The DMV requires at least 2 letters bntRedDemo.Text = “This is not a legal name!”
End If
Syntax: Integer = Len(String)Syntax: Integer = Len(String)
There is a function called Mid, which returns a subsection of a string…
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”
Syntax: String = Mid(String, integer_type, integer_type )Syntax: String = Mid(String, integer_type, integer_type )
Optional!
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.
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
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
There is a function called Space, which returns a String containing the number of spaces indicated by the parameter.
Syntax: String = Space(integer_type)Syntax: String = Space(integer_type)
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:
Function call Return Value
Space(5) “ “
Space(10) “ “
Space(0) “”
“Hello” & Space(10) & “Goodbye” “Hello Goodbye”
There is a function called InStr, which looks for a substring in a longer string, and returns its location
Syntax: Integer = InStr(String,String)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
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:
InStr Example
Dim strS() As String = {"Gunopulos", "Papadopoulos", "Keogh", "Vlachos"}
Dim intI As IntegerFor intI = 0 To UBound(strS)
If InStr(strS(intI), "os") > 0 Then bntRedDemo.Text = strS(intI) & " might be greek" End If
Next intI
Finding possible Greek names..
Your code should be clearer, and more robust
Const CommonGreekNameSuffix = "OS"
For intI = 0 To UBound(strS)
If InStr(UCase(strS(intI)), CommonGreekNameSuffix) > 0 Then bntRedDemo.Text = strS(intI) & " might be greek" End If
Next intI
No magic numbers (days of week example) and no magic strings
There are two functions, Int and Fix which convert real numbers to whole numbers
Syntax: Integer_type = Int(real type)
Syntax: Integer_type = Fix(real type)
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
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.
There is a function IsNumeric which tests to see if the input is a number
Syntax: Boolean = IsNumeric(any type)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
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.
There is a function Rnd which produces random numbers
Syntax: Single = Rnd()Syntax: Single = Rnd()
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.
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
To produce random numbers in a range, from lowerbound to upperbound…
Int((upperbound - lowerbound + 1) * Rnd + lowerbound)
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.2Mr. Eamonn Keogh has a GPA of 4.0Mr. John Papadopoulos has a GPA of 2.4Ms. Mary Vlachos has a GPA of 3.4
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.2Mr. Eamonn Keogh has a GPA of 4.0Mr. John Papadopoulos has a GPA of 2.4Ms. 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)
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.
Mr. Dima Gunopulos has a GPA of 1.2
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
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