1 / 28

Note The first half of these slides duplicate previous slides. The second half are original

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.

Download Presentation

Note The first half of these slides duplicate previous slides. The second half are original

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

  9. There a function call Val, which converts strings to numbers. Syntax:Numeric Value = Val(String) strAge = “5” strAgeDiff = 100 – Val(strAge) 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:

  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)

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

  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 ) 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… DimstrW, strLastLetterAs 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)

  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)

  20. Finding possible Greek names.. InStr Example Dim strS() As String = {"Gunopulos", "Papadopoulos", "Keogh", "Vlachos"} Dim intI As Integer For intI = 0 To UBound(strS) If InStr(strS(intI), "os") > 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) > 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)

  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)

  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)

  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. Mr. Dima Gunopulos has a GPA of 1.2 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

More Related