Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub.

Similar presentations


Presentation on theme: "© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub."— Presentation transcript:

1 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub Procedures and Functions

2 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 2 Introduction In Visual Basic.NET There Are Two Broad Categories of Procedures: Sub Procedures and Function Procedures

3 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 3 Chapter 6 Topics A Sub Procedure is a collection of statements that performs a task  The word Sub is an abbreviation for the older term “subroutine”  Event procedures belong to this category A Function procedure is a collection of statements that performs a task and then returns a value to the part of the program that executed it  Function procedures work like intrinsic functions, such as Val and IsNumeric

4 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 4 Sub Procedures You Can Write Your Own General Purpose Sub Procedures That Perform Specific Tasks General Purpose Sub Procedures Are Not Triggered by Events but Called From Statements in Other Procedures

5 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 5 Sub Procedure Uses As Event handling code As General code that is executed by statements in other procedures

6 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 6 Uses for Sub Procedures To Modularize one's program  Break it into small, manageable hunks To Simplify one's program  Create individual sub procedures to perform tasks that are done repeatedly

7 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 7 Sample Sub Procedure Sub DisplayMessage() 'A Sub procedure that displays a message. lstOutput.Items.Add("") lstOutput.Items.Add("Hello from DisplayMessage.") lstOutput.Items.Add("") End Sub Private Sub btnGo_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles btnGo.Click ' This procedure calls the DisplayMessage procedure. lstOutput.Items.Add("Hello from btnGo_Click procedure.") lstOutput.Items.Add("Calling the DisplayMessage " & _ "procedure.") DisplayMessage() lstOutput.Items.Add("Now I am back in the btnGo_Click procedure.") End Sub

8 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 8 Declaring a Sub Procedure AccessSpecifier gives accessibility to the program ParameterList is a list of variable values that are being passed to the sub procedure 'Sub' and 'End' are keywords [AccessSpecifier] Sub ProcedureName ([ParameterList]) [Statement(s)] End Sub

9 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 9 Local Variables Within the Sub Procedure, declare the variable names that are needed  These work as they do in other procedures  They are only accessible from inside the sub procedure in which they are declared  (Their values are not saved from one call to the sub procedure to the next)

10 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 10 Static Local Variables Declared with These local variables do maintain their values from one call to the sub procedure to the next Static VariableName As DataType

11 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 11 Passing Values to a Sub Procedure When a Procedure Is Called, Values May Be Passed to It

12 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 12 Passing Parameters By Value, Example The value, 5, is copied into the storage location of 'number' in DisplayValue Then DisplayValue executes DisplayValue(5) Sub DisplayValue(ByVal number As Integer) ' This procedure displays a value in a message box. MessageBox.Show(number.ToString) End Sub

13 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 13 Passing Multiple Parameters Value of first argument is copied to first Second to second, etc. ShowSum(5, 10) Sub ShowSum(ByVal num1 As Integer, ByVal num2 As Integer) ' This procedure accepts two arguments, and prints ' their sum on the form. Dim sum As Integer sum = num1 + num2 MessageBox.Show("The sum is " & sum.ToString) End Sub

14 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 14 Passing Arguments by Reference Keyword 'ByVal' is used to indicate passing a parameter by value (value is copied) Keyword 'ByRef' indicates passing a parameter in another way:  Use of the corresponding variable in the sub procedure refers to the corresponding storage location in the calling program  There is no corresponding storage location in the sub procedure (to receive a copy)

15 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 15 Function Procedures A Function Procedure Returns a Value to the Part of the Program That Called the Function Procedure

16 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 16 Declaring a Function Procedure New keyword 'Function' Plus 'As DataType' qualifier to state the data type of the returned value Return value is specified in a Return expression [AccessSpecifier] Function FunctionName ([ParameterList]) _ As DataType [Statements] End Function

17 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 17 Arguments Passed and Value Returned total = Sum(value1, value2) Function Sum(ByVal num1 As Single, ByVal num2 As Single) _ As Single Dim result As Single result = num1 + num2 Return result End Function

18 © 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 18 Returning Nonnumeric Values Function IsValid(num As Integer) As Boolean Dim status As Boolean If num >= 0 And num <= 100 Then status = True Else status = False End If Return status End Function Function FullName(ByVal first As String, ByVal last As String)_ As String Dim name As String name = last & ", " & first Return name End Function


Download ppt "© 2006 ITT Educational Services Inc. Introduction to Computer Programming: Unit 10: Chapter 6: Slide 1 Unit 10 Sub Procedures and Functions Chapter 6 Sub."

Similar presentations


Ads by Google