Download presentation
Presentation is loading. Please wait.
Published byMichael Peters Modified over 9 years ago
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 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 Procedures
2
110-G2 A Procedurecan be a Function or a Sub A Function gives back a value e.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
3
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
4
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
5
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?
6
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()
7
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 order PrintInfo(26,"Sandra") 'Boom! wrong number of arguments PrintInfo(26) 'No way! 26.5 is not an integer PrintInfo("Sandra",26.5)
8
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 = 11 PrintInfo(strSomeName, intSomeAge) 'OK
9
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
10
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. Exit Welcome Enter your name Delphine Welcome to VB, Delphine! Use a Sub to display the message
11
110-G11 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 A Sub Example (2) To use the Sub: write its name and give it its argument.
12
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 a leap year and False otherwise. The function returns a Boolean blnLeapYear(1999) is False blnLeapYear(1996) is True Exit Check Is 1996 a leap year? Yes!
13
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 If End Function Function can be only accessed within the module Function argument Function type Function Name A Function Example (2) Returned value Create a Function named blnLeapYear
14
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 the function: write its name and give it its argument between ( )
15
110-G15 Order of execution Private Sub cmdLeapYear_Click(…)... If blnLeapYear(intYear) Then... End Sub 1 Private Function blnLeapYear...... End Function 3 5 2 What is blnLeapYear ? 4 Send back True or False Control Flow
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.