150 likes | 286 Views
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. e.g. Private Sub btnValidate_Click () 'the program is written here
E N D
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 e.g. Private Sub btnValidate_Click () 'the program is written here End Sub Procedures Motivation: Procedure: a piece of code with a name 110-G1
A Procedure can be a Function or a Sub A Functiongives back a value e.g. IsNumeric is a function: gives back True or False A Subdoes not give any value back e.g. btnClear_Click does not return any value We can write our ownFunctions and Subs Subs and Functions 110-G2
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 110-G3
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 110-G4
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? 110-G5
actual parameters Formal and actual parameters • Store in the formal parameters the input values sent to the procedure, e.g. Public Sub PrintInfo(strName AsString, intAge AsInteger) Console.WriteLine("My name is " & strName & _ ". I am " & intAge & " years old.") End Sub formal 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() 110-G6
Rule for calling a method (1) • Actual and formal parameters must match • in number • in type • in order 'OKPrintInfo("Sandra",26) 'Oops! wrong orderPrintInfo(26,"Sandra") 'Boom! wrong number of argumentsPrintInfo(26) 'No way! 26.5 is not an integerPrintInfo("Sandra",26.5) 110-G7
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 = 11 PrintInfo(strSomeName, intSomeAge) 'OK 110-G8
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 110-G9
A Sub Example (1) Enter your name Delphine Welcome Exit Use a Sub to display the message Use a Sub when you don't need anything back Example: The user inputs her name. The program displays a welcome message. Welcome to VB, Delphine! 110-G10
A Sub Example (2) To use the Sub: write its name and give it its argument. Private Sub btnWelcome_Click( …) 'Call the Welcome Sub to display 'a welcome message Welcome (txtName.Text) End Sub Private Sub Welcome(strName As String) 'Display a welcome message using the 'label lblMessage lblMessage. Text = "Welcome to VB, " _ & strName & "!" lblMessage.Visible = True End Sub 110-G11
A Function Example(1) How? : Use a function: Given the year, the function returns True if the year is a leap year and False otherwise. blnLeapYear The function returns a Boolean Goal: Write a program to display if a year is a leap year or not. Is 1996 a leap year? Yes! Check Exit blnLeapYear(1999) isFalse blnLeapYear(1996) isTrue 110-G12
A Function Example (2) Function can be only accessed within the module Function argument Function Name Create a Function named blnLeapYear Function type Returned value 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 ReturnTrue Else ReturnFalse 'blnLeapYear = False is OK as well End If End Function 110-G13
A Function Example (3) To use the function: write its name and give it its argument between ( ) Using a function from inside a SubProcedure: Private Sub btnLeapYear_Click( ..) 'Get the year DimintYearAs 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 110-G14
Order of execution Control Flow 1 What is blnLeapYear ? 5 2 4 3 Send back True or False Private Sub cmdLeapYear_Click(…) ... If blnLeapYear(intYear) Then ... End Sub Private Function blnLeapYear... ... End Function 110-G15