Presentation is loading. Please wait.

Presentation is loading. Please wait.

5.2 Sub Procedures, Part I Defining and Calling Sub Procedures

Similar presentations


Presentation on theme: "5.2 Sub Procedures, Part I Defining and Calling Sub Procedures"— Presentation transcript:

1 5.2 Sub Procedures, Part I Defining and Calling Sub Procedures
Variables and Expressions as Arguments Sub Procedures Calling Other Sub Procedures

2 General Form of Sub Procedure
Sub procedures have no return type.

3 Calling a Sub Procedure
The statement that invokes a Sub procedure is referred to as a calling statement. A calling statement looks like this: ProcedureName(arg1, arg2,..., argN) When you call a Sub procedure you get no data back. Therefore you can’t use it in an expression.

4 Passing Values DisplaySum(2, 3) Sub DisplaySum(ByVal num1 As Double, ByVal num2 _ As Double) Dim z As Double z = num1 + num2 lstOutput.Items.Add("The sum of " & num1 & " and " & num2 & " is " & z & ".") End Sub In the Sub procedure, 2 will be stored in num1 and 3 will be stored in num2

5 Arguments and Parameters
Sum(2, 3) Sub DisplaySum(ByVal num1 As Double, ByVal num2 _ As Double) Arguments (actual parameters) Formal parameters You don’ t have to type this. ByVal is the default

6 Several Calling Statements
DisplaySum(2, 3) DisplaySum(4, 6) DisplaySum(7, 8) Output: The sum of 2 and 3 is 5. The sum of 4 and 6 is 10 The sum of 7 and 8 is 15.

7 Example 5.2.1

8 Example 5.2.1 Calls to DisplaySum.
You can pass literals, variables, or any other expression as arguments, as long as their data types match the formal parameter declarations.

9 Passing Strings and Numbers
Demo("CA", 38) Sub Demo(ByVal state As String, ByVal pop _ As Double) lstOutput.Items.Add = state & " has population " & pop & " million." End Sub Note: The statement Demo(38, "CA") would not be valid. The types of the arguments must be in the same order as the types of the parameters.

10 Variables and Expressions as Arguments
Dim s As String = "CA" Dim p As Double = 19 Demo(s, 2 * p) Sub Demo(ByVal state As String, ByVal pop _ As Double) lstOutput.Items.Add = state & " has population " & pop & " million." End Sub Note: The argument names need not match the parameter names. For instance, s versus state. An argument can be ANY expression that satisfies the type of the formal parameter.

11 Calls to CalculateDensity.
Example 5.2.2 Calls to CalculateDensity.

12 Example 5.2.2 If you have to do the same task in several parts of the program, it’s good to make a procedure out of it. That way you don’t have to repeat the code in multiple places.

13 Sub Procedure Having No Parameters
Sub DescribeTask() lstBox.Items.Clear() lstBox.Items.Add("This program displays") lstBox.Items.Add("the name and population") lstBox.Items.Add("of a state.") End Sub

14 Sub Procedure Calling Another Sub Procedure
Private Sub btnDisplay_Click(...) Handles _ btnDisplay.Click Demo("CA", 37) End Sub Sub Demo(ByVal state As String, ByVal pop _ As Double) DescribeTask() lstOutput.Items.Add("") lstOutput.Items.Add = state & " has population " & pop & " million."

15 Output This program displays the name and population of a state.
CA has population 37 million.

16 Example 5.2.3

17 Example 5.2.3 Modularity – take the big task and break it into subtasks. Here, the two subtasks are implemented in the procedures DescribeTask and CalculateDensity.

18 Example 5.2.4 This example is modified a bit. Not exactly identical with the textbook.

19 Calls to Sub procedures.
Example 5.2.4 Calls to Sub procedures.

20 Stack 1 deep.

21 Stack 2 deep.

22 You can select another frame from the stack to see the local data of that procedure. The green highlighting indicates the statement where the selected procedure is paused at.

23 Stack 3 deep.

24 Stack 2 deep.

25 Stack 1 deep.

26 Stack 2 deep.

27 Stack 1 deep.


Download ppt "5.2 Sub Procedures, Part I Defining and Calling Sub Procedures"

Similar presentations


Ads by Google