UCR CS 5 - Chapter 5 Subroutines and Functions

Unformatted text preview:

The Visual Basic .NET Coach1Chapter 5 – Subroutines and Functions5.1 What Are Subroutines and Functions?As your applications grow, you need to break them into separate logical units. The code associated with accomplishing each task is separated from the code the accomplishes other tasks. These actions are referred to as events and are one way of breaking up code into smaller, more logical units.Another way to break up an application is by using either functions or subroutines. Programs are made more readable by breaking large amounts of code into smaller, more concise parts. By breaking code into functions and subroutines, code can be written once and reused often. This reduces the size of the application and debugging time.Each time you repeat a calculation, you waste space and increase the likelihood of a typographical error and therefore cause your application to execute improperly.Functions and subroutines operate similarly but have one key difference. A function is used when a value is returned to the calling routine, while a subroutine is used when a desired task is needed, but no value is returned.The Visual Basic .NET Coach2Chapter 5 – Subroutines and FunctionsInvoking a SubroutineA subroutine is used when a series of steps are required but no value is returned to the routine that called the subroutine. Subroutines are invoked using a subroutine name:SubroutineName(ParameterList)Invoking a subroutine can occur with parameters:OutputMin(intValue1, intValue2)Invoking a subroutine can also occur without parameters:Message()The Visual Basic .NET Coach3Chapter 5 – Subroutines and FunctionsInvoking a Function CallA function by definition has a return value. Therefore, a function call must be assigned to a variable of the type that the function returns:VariableName = FunctionName(ParameterList)The following code calls a UCase function to return an uppercase representation of a Stringthat is passed as a parameter:strVariableName = UCase(“please uppercase this”)The Visual Basic .NET Coach4Chapter 5 – Subroutines and Functions5.2 Built-In FunctionsVisual Basic .NET provides many built-in functions to assist your coding or applications. By using built-in functions you save time in coding and debugging work that has already been provided for you.The Visual Basic .NET Coach5Chapter 5 – Subroutines and FunctionsString FunctionsFunction Name: UCaseFunction Description: Returns the String that is passed in all uppercase letters.Common Uses: UCase can be used when the desired output is required to be in all uppercase letters. It is also commonly used when you wish to validate data entered by a user against a given string.Syntax: String = UCase(String)Examples:“UPPER AND LOWERCASE”UCase(“UpPeP AnD lOwErCaSE”)“ALL UPPERCASE”UCase(“ALL UPPERCASE”)“ALL LOWERCASE”UCase(“all lowercase”)“INPUT STRING”UCase(“Input String”)Return ValueFunction callPrevious Way of Coding Validation:If (txtVote.Text = “Bush” Or txtVote.Text = “BUSH” Or _ txtVote.Text = “bush” Then ...Better Way of Coding Validation:If (UCase(txtVote.Text) = “BUSH”) Then ...The Visual Basic .NET Coach6Chapter 5 – Subroutines and FunctionsFunction Name: LCaseFunction Description: Returns the String that is passed in all lowercase letters.Common Uses: LCase is very similar in use to UCase.Syntax: String = LCase(String)Examples:“upper and lowercase”UCase(“UpPeP AnD lOwErCaSE”)“all uppercase”UCase(“ALL UPPERCASE”)“all lowercase”UCase(“all lowercase”)“input string”UCase(“Input String”)Return ValueFunction callThe Visual Basic .NET Coach7Chapter 5 – Subroutines and FunctionsFunction Name: TrimFunction Description: Returns a String with the same content, except the leading and trailing spaces are removed.Common Uses: Often when data is gathered, additional spaces may exist before the first noncharacter or after the last nonblank character. It is good practice to remove these so that data may be presented cleanly.Syntax: String = Trim(String)Examples:“Input String”Trim(“ Input String ”)“InputString”Trim(“ InputString ”)“InputString”Trim(“InputString ”)“InputString”Trim(“ InputString”)Return ValueFunction callThe Visual Basic .NET Coach8Chapter 5 – Subroutines and FunctionsFunction Name: Trim (continued)The following code will initialize two Strings. One will contain a String that has the leading and trailing spaces removed by the Trimfunction. It is displayed between two vertical bars so that it will be obvious that the spaces have been removed. The second String will be created in a similar manner; however, the spaces will not be removed.Dim strTest As StringDim strWithBlanks As StringDim strBorder As StringDim strTrimmedOutput As StringDim strUnTrimmedOutput As StringstrTest = " Hello " 'Two spaces before and afterstrBorder = "|"strTrimmedOutput = strBorder & Trim(strTest) & strBorderstrUnTrimmedOutput = strBorder & strTest & strBorderMsgBox(strTrimmedOutput)MsgBox(strUnTrimmedOutput)|Hello|| Hello |The Visual Basic .NET Coach9Chapter 5 – Subroutines and FunctionsFunction Name: SpaceFunction 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:“Hello Goodbye”“Hello” & Space(10) & “Goodbye”“”Space(0)““Space(10)““Space(5)Return ValueFunction callThe Visual Basic .NET Coach10Chapter 5 – Subroutines and FunctionsFunction Name: LenFunction Description: Returns the number of characters contained in a StringCommon Uses: Len is used to determine the size of a String.Syntax: Integer = Len(String)Examples:0Len(“”)70Len(“Hello, my name is Inigo Montoya. You killed my father. Prepare to die.”)14Len(“Iocaine Powder”)13Len(“Inconceivable”)Return ValueFunction callThe Visual Basic .NET Coach11Chapter 5 – Subroutines and FunctionsFunction Name: LeftFunction Description: Returns the first N characters of a String where N is an Integerparameter indicating the number of characters to return. If N is greater than the number of characters in the String, then the String is returned. No extra spaces are added.Common Uses: Often you are only concerned with the first few characters of a String. Leftis a great way to look at only the beginning of a


View Full Document

UCR CS 5 - Chapter 5 Subroutines and Functions

Download Chapter 5 Subroutines and Functions
Our administrator received your request to download this document. We will send you the file to your email shortly.
Loading Unlocking...
Login

Join to view Chapter 5 Subroutines and Functions and access 3M+ class-specific study document.

or
We will never post anything without your permission.
Don't have an account?
Sign Up

Join to view Chapter 5 Subroutines and Functions 2 2 and access 3M+ class-specific study document.

or

By creating an account you agree to our Privacy Policy and Terms Of Use

Already a member?