110-g1 motivation: within a program, may have to perform the same computation over and over many...

15
110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To break a program into smaller pieces easier to write reusable for other problems Procedure: a piece of code with a name e.g. Private Sub btnValidate_Click () 'the program is written here End Sub Procedure s

Upload: michael-peters

Post on 02-Jan-2016

212 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To

110-G1

Motivation:

Within a program, may have to performthe same computation over and overMany programs share the samecomputation (e.g. sorting)

To break a program into smaller pieces• easier to write• reusable for other problems

Procedure: a piece of code with a name

e.g.Private Sub btnValidate_Click ()

'the program is written hereEnd Sub

Procedures

Page 2: 110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To

110-G2

A Procedure can be a Function or a Sub

A Function gives back a valuee.g. IsNumeric is a function: gives back True

or False A Sub does not give any value back

e.g. btnClear_Click does not return any value

We can write our own Functions and Subs

Subs and Functions

Page 3: 110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To

110-G3

Functions versus Sub Procedures

• Sub Procedures– Can receive passed values (arguments)– Performs actions

• Functions– Can receive passed values (arguments)– Performs actions– Returns a value of a specific data type to the

procedure that called it originally

Page 4: 110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To

110-G4

Functions – Return Values

• To return a value to the calling procedure set up a return value

• The return value is placed by VB in a variable with the SAME name a as the Function's name

OR• Use the Return statement to return the value

Page 5: 110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To

110-G5

Examples

Public Sub DisplayTime() lblTime.Text = DateTime.Now.ToString()

End Sub

Public Function strGetTime() As String

Return DateTime.Now.ToString()

'strGetTime = DateTime.Now.ToString()

'is also OK

End Function

How are these two procedures different?

Page 6: 110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To

110-G6

Formal and actual parameters • Store in the formal parameters the input values

sent to the procedure, e.g. Public Sub PrintInfo(strName As String, intAge As Integer)

Console.WriteLine("My name is " & strName & _

". I am " & intAge & " years old.")

End Sub formal parameters

actual parameters actual parameters: what is sent to the procedure

PrintInfo("Sandra",26)

' prints My name is Sandra. I am 26 years old.

If no input, write ProcedureName()

Page 7: 110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To

110-G7

Rule for calling a method (1)• Actual and formal parameters must match– in number– in type– in order

'OK PrintInfo("Sandra",26)

'Oops! wrong orderPrintInfo(26,"Sandra")

'Boom! wrong number of argumentsPrintInfo(26)

'No way! 26.5 is not an integerPrintInfo("Sandra",26.5)

Page 8: 110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To

110-G8

Rule for calling a method (2)• Actual and formal parameters don't have to match by name,

e.g.

Public Sub PrintInfo(strName As String, _ intAge As Integer)

Console.WriteLine("My name is " & strName & _

". I am " & intAge & " years old.")

End Sub

' in some other procedure

Dim strSomeName As String = "David"Dim intSomeAge As Integer = 11PrintInfo(strSomeName, intSomeAge) 'OK

Page 9: 110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To

110-G9

Passing ByVal or ByRef

• ByVal (default)– Sends a copy, original cannot be altered

• ByRef– Sends a reference to the memory location

where the original is stored and therefore the original can be altered

Page 10: 110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To

110-G10

A Sub Example (1)Use a Sub when you don't need anything back

Example: The user inputs her name. The program displays a welcome message.

ExitWelcome

Enter your name

Delphine

Welcome to VB, Delphine!

Use a Sub to display the message

Page 11: 110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To

110-G11

Private Sub btnWelcome_Click( …) 'Call the Welcome Sub to display 'a welcome message Welcome (txtName.Text)End SubPrivate Sub Welcome(strName As String) 'Display a welcome message using the 'label lblMessage lblMessage. Text = "Welcome to VB, " _ & strName & "!" lblMessage.Visible = TrueEnd Sub

A Sub Example (2)

To use theSub:write its nameand give it its argument.

Page 12: 110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To

110-G12

A Function Example(1)Goal: Write a program to display if a year is a leap year or not.

blnLeapYear

How? : Use a function: Given the year, the function returns True if the year is aleap year and False otherwise.

The function returns a BooleanblnLeapYear(1999) is False blnLeapYear(1996) is True

ExitCheck

Is 1996 a leap year?

Yes!

Page 13: 110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To

110-G13

Private Function blnLeapYear(intYear As Integer) _As Boolean

'Check if intYear is a leap year If (intYear Mod 4 = 0 And intYear Mod 100 <> 0) _ Or (intYear Mod 400 = 0) Then Return True Else Return False 'blnLeapYear = False is OK as well End IfEnd Function

Function can be only accessed within the module

Function argument

Function type

Function Name

A Function Example (2)

Returnedvalue

Create a Function named blnLeapYear

Page 14: 110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To

110-G14

A Function Example (3)Using a function from inside a SubProcedure:Private Sub btnLeapYear_Click( ..)

'Get the year

Dim intYear As Integer

intYear = CInt(txtYear.Text)

'Use the function blnLeapYear

'to find out if the year is a leap year

If blnLeapYear(intYear) Then

lblYesNo.Text = "Yes!"

Else

lblYesNo.Text = "No!"

End If

'Display the answer

lblYesNo.Visible = True

End Sub

To use thefunction:write its nameand give it its argumentbetween ( )

Page 15: 110-G1 Motivation: Within a program, may have to perform the same computation over and over Many programs share the same computation (e.g. sorting) To

110-G15

Order of execution

Private Sub cmdLeapYear_Click(…)

...

If blnLeapYear(intYear) Then

...

End Sub

1

Private Function blnLeapYear...

...

End Function

3

52

What is blnLeapYear ?

4

Send backTrue or False

Control Flow