Docsity

Understanding Subroutines and Functions in Visual Basic .NET, Summaries of Mathematics

Toggle sidebar
bg1
The Visual Basic .NET Coach
1
Chapter 5 – Subroutines and Functions
5.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.
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e
pf1f
pf20
pf21
pf22
pf23
pf24
pf25
pf26
pf27
pf28
pf29
pf2a
pf2b
pf2c
pf2d
pf2e
pf2f
pf30
pf31
pf32
pf33
pf34
pf35
pf36
pf37
pf38
pf39
pf3a
pf3b
pf3c
pf3d
pf3e
pf3f
pf40
pf41
pf42
pf43
pf44
pf45
pf46
pf47
pf48
pf49
pf4a
pf4b
pf4c
pf4d
pf4e
pf4f
pf50
pf51
pf52
pf53

Partial preview of the text

Download Understanding Subroutines and Functions in Visual Basic .NET and more Summaries Mathematics in PDF only on Docsity!

The Visual Basic .NET Coach

Chapter 5 – Subroutines and Functions^ 5.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 theaccomplishes other tasks.These actions are referred to as events and are one way of breaking up code into smaller, morelogical 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, moreconcise 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 atypographical 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 usedwhen a desired task is needed, but no value is returned.

The Visual Basic .NET Coach

Chapter 5 – Subroutines and Functions^ Invoking a Subroutine^ A subroutine is used when a series of steps are required but no value is returned to the routinethat 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 Coach

Chapter 5 – Subroutines and Functions^ 5.2 Built-In Functions^ Visual 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 beenprovided for you.

The Visual Basic .NET Coach

Chapter 5 – Subroutines and Functions^ String Functions^ Function Name:^ UCase^ Function 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 agiven 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 Value Function^ call Previous 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 Coach

Chapter 5 – Subroutines and Functions^ Function Name:^ Trim^ Function 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 firstnoncharacter 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 Value Function^ call

The Visual Basic .NET Coach

Chapter 5 – Subroutines and Functions^ Function 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

Trim

function.It is displayed between two vertical bars so that it will be obvious that the spaces have beenremoved.The second^ String^ will be created in a similar manner; however, the spaces will not beremoved.^ Dim^ strTest^ As StringDim^ strWithBlanks As StringDim^ strBorder As StringDim^ strTrimmedOutput As StringDim^ strUnTrimmedOutput

As String strTest =^ " Hello " 'Two spaces before and afterstrBorder =^ "|" strTrimmedOutput =

strBorder & Trim(strTest) & strBorder strUnTrimmedOutput

= strBorder &^ strTest & strBorder MsgBox(strTrimmedOutput)MsgBox(strUnTrimmedOutput)

|Hello|| Hello^ |

The Visual Basic .NET Coach

Chapter 5 – Subroutines and Functions^ Function Name:^ Len^ Function Description:

Returns the number of characters contained in a

String

Common Uses:^ Len is used to determine the size of a

String. Syntax:^ Integer = Len(String) Examples:

Len(“”)

Len(“Hello, my name is Inigo Montoya. Youkilled my father.

Prepare to die.”)

Len(“Iocaine Powder”)

Len(“Inconceivable”)

Return Value Function^ call

The Visual Basic .NET Coach

Chapter 5 – Subroutines and Functions^ Function Name:^ Left^ Function Description:

Returns the first N characters of a

String^ where N is an

Integer

parameter 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.^ Left

is a great way to look at only the beginning of a

String. Syntax:^ String = Microsoft.VisualBasic.Left(String,

Integer)

Examples:

“Beginning of String”

Microsoft.VisualBasic.Left(“Beginning of String”, 20)

Microsoft.VisualBasic.Left(“Beginning of String”, 0)

“Be”

Microsoft.VisualBasic.Left(“Beginning of String”, 2)

“Begin”

Microsoft.VisualBasic.Left(“Beginning of String”, 5)

Return Value

Function^ call

The Visual Basic .NET Coach

Chapter 5 – Subroutines and Functions^ Function Name:^ Right^ Function Description:

Returns the last N characters of a

String^ where N is an

Integer

parameter 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 last few characters of a

String.

Right^ is a great way to look at only the end of a

String. Syntax:^ String = Microsoft.VisualBasic.Right(String, Integer) Examples:

“Ending of String”

Microsoft.VisualBasic.Right(“Ending of String”, 20)

Microsoft.VisualBasic.Right(“Ending of String”, 0)

“ng”

Microsoft.VisualBasic.Right(“Ending of String”, 2)

“tring”

Microsoft.VisualBasic.Right(“Ending of String”, 5)

Return Value

Function^ call

The Visual Basic .NET Coach

Chapter 5 – Subroutines and Functions^ Function Name:^ Right

(continued) The following code shows how you might use Right to determine a person’s suffix, as in, Jr.,Sr., or Ph.D.^ Dim^ strPerson^

As^ StringDim strPerson2 As^ StringDim strPerson3 As^ StringstrPerson1 = "Nira^ Herrmann, Ph.D."strPerson2 = "John Cunningham, Sr."strPerson3 = "Bob Bruno, Jr.“'Process Person1If ("Ph.D." = Microsoft.VisualBasic.Right(strPerson1, 5)) ThenMsgBox "Person 1 has a doctorate degree."ElseIf ("Sr." = Microsoft.VisualBasic.Right(strPerson1, 3))

Then

MsgBox "Person 1 has a kid with the same name."ElseIf ("Jr." = Microsoft.VisualBasic.Right(strPerson1, 3))

Then

MsgBox "Person 1 has a father with the same name."EndIf'Person2 and Person3 code could follow

The Visual Basic .NET Coach

Chapter 5 – Subroutines and Functions^ 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(“ab ab ab”, “c”)

InStr(“ab ab ab”, “a”)

InStr(“ab ab ab”, “ab”)

InStr(“This^ is a very”, “is”)

Return Value Function^ call

The Visual Basic .NET Coach

Chapter 5 – Subroutines and Functions^ Drill 5.1^ What is the output of the following code?^ MsgBox UCase(“What

is the output?”) Answer: “WHAT IS THE OUTPUT?”

The Visual Basic .NET Coach

Chapter 5 – Subroutines and Functions^ Drill 5.3^ What is the output of the following code?^ MsgBox Microsoft.VisualBasic.Right(“What is the output?”, 4)^ Answer: “put?”

The Visual Basic .NET Coach

Chapter 5 – Subroutines and Functions^ Drill 5.4^ What is the output of the following code?^ MsgBox UCase(Microsfot.VisualBasic.Left(“What is the output?”, 4))^ Answer: “WHAT”