Skip to content

Commit 0cc5758

Browse files
Youssef1313mairaw
authored andcommitted
Use code fence (#16716)
* Use code fence * Update sub-procedures.md
1 parent 084cfbc commit 0cc5758

File tree

1 file changed

+71
-57
lines changed

1 file changed

+71
-57
lines changed
Lines changed: 71 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
2-
title: "Sub Procedures"
2+
title: Sub procedures
33
ms.date: 07/20/2015
4-
helpviewer_keywords:
4+
helpviewer_keywords:
55
- "Sub procedures [Visual Basic], about Sub procedures"
66
- "statement blocks"
77
- "Sub procedures [Visual Basic], calling"
@@ -12,67 +12,81 @@ helpviewer_keywords:
1212
- "syntax [Visual Basic], Sub procedures"
1313
ms.assetid: 6a0a4958-ed0a-4d3d-8d31-0772c82bda58
1414
---
15-
# Sub Procedures (Visual Basic)
16-
A `Sub` procedure is a series of Visual Basic statements enclosed by the `Sub` and `End Sub` statements. The `Sub` procedure performs a task and then returns control to the calling code, but it does not return a value to the calling code.
17-
18-
Each time the procedure is called, its statements are executed, starting with the first executable statement after the `Sub` statement and ending with the first `End Sub`, `Exit Sub`, or `Return` statement encountered.
19-
20-
You can define a `Sub` procedure in modules, classes, and structures. By default, it is `Public`, which means you can call it from anywhere in your application that has access to the module, class, or structure in which you defined it. The term, *method*, describes a `Sub` or `Function` procedure that is accessed from outside its defining module, class, or structure. For more information, see [Procedures](./index.md).
21-
22-
A `Sub` procedure can take arguments, such as constants, variables, or expressions, which are passed to it by the calling code.
23-
24-
## Declaration Syntax
25-
The syntax for declaring a `Sub` procedure is as follows:
26-
27-
`[` *modifiers* `] Sub` *subname* `[(` *parameterlist* `)]`
28-
29-
`' Statements of the Sub procedure.`
30-
31-
`End Sub`
32-
33-
The `modifiers` can specify access level and information about overloading, overriding, sharing, and shadowing. For more information, see [Sub Statement](../../../../visual-basic/language-reference/statements/sub-statement.md).
34-
35-
## Parameter Declaration
36-
You declare each procedure parameter similarly to how you declare a variable, specifying the parameter name and data type. You can also specify the passing mechanism, and whether the parameter is optional or a parameter array.
37-
38-
The syntax for each parameter in the parameter list is as follows:
39-
40-
`[Optional] [ByVal | ByRef] [ParamArray]` *parametername* `As` *datatype*
41-
42-
If the parameter is optional, you must also supply a default value as part of its declaration. The syntax for specifying a default value is as follows:
43-
44-
`Optional [ByVal | ByRef]` *parametername* `As` *datatype* `=` *defaultvalue*
45-
46-
### Parameters as Local Variables
47-
When control passes to the procedure, each parameter is treated as a local variable. This means that its lifetime is the same as that of the procedure, and its scope is the whole procedure.
48-
49-
## Calling Syntax
50-
You invoke a `Sub` procedure explicitly with a stand-alone calling statement. You cannot call it by using its name in an expression. You must provide values for all arguments that are not optional, and you must enclose the argument list in parentheses. If no arguments are supplied, you can optionally omit the parentheses. The use of the `Call` keyword is optional but not recommended.
51-
52-
The syntax for a call to a `Sub` procedure is as follows:
53-
54-
`[Call]` *subname* `[(` *argumentlist* `)]`
55-
56-
You can call a `Sub` method from outside the class that defines it. First, you have to use the `New` keyword to create an instance of the class, or call a method that returns an instance of the class. For more information, see [New Operator](../../../../visual-basic/language-reference/operators/new-operator.md). Then, you can use the following syntax to call the `Sub` method on the instance object:
57-
58-
*Object*.*methodname*`[(`*argumentlist*`)]`
59-
60-
### Illustration of Declaration and Call
61-
The following `Sub` procedure tells the computer operator which task the application is about to perform, and also displays a time stamp. Instead of duplicating this code at the start of every task, the application just calls `tellOperator` from various locations. Each call passes a string in the `task` argument that identifies the task being started.
62-
63-
[!code-vb[VbVbcnProcedures#2](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbcnProcedures/VB/Class1.vb#2)]
64-
65-
The following example shows a typical call to `tellOperator`.
66-
67-
[!code-vb[VbVbcnProcedures#3](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbcnProcedures/VB/Class1.vb#3)]
68-
15+
# Sub procedures (Visual Basic)
16+
17+
A `Sub` procedure is a series of Visual Basic statements enclosed by the `Sub` and `End Sub` statements. The `Sub` procedure performs a task and then returns control to the calling code, but it does not return a value to the calling code.
18+
19+
Each time the procedure is called, its statements are executed, starting with the first executable statement after the `Sub` statement and ending with the first `End Sub`, `Exit Sub`, or `Return` statement encountered.
20+
21+
You can define a `Sub` procedure in modules, classes, and structures. By default, it is `Public`, which means you can call it from anywhere in your application that has access to the module, class, or structure in which you defined it. The term *method* describes a `Sub` or `Function` procedure that is accessed from outside its defining module, class, or structure. For more information, see [Procedures](./index.md).
22+
23+
A `Sub` procedure can take arguments, such as constants, variables, or expressions, which are passed to it by the calling code.
24+
25+
## Declaration syntax
26+
27+
The syntax for declaring a `Sub` procedure is as follows:
28+
29+
```vb
30+
[modifiers] Sub SubName[(parameterList)]
31+
' Statements of the Sub procedure.
32+
End Sub
33+
```
34+
35+
The `modifiers` can specify access level and information about overloading, overriding, sharing, and shadowing. For more information, see [Sub Statement](../../../language-reference/statements/sub-statement.md).
36+
37+
## Parameter declaration
38+
39+
You declare each procedure parameter similarly to how you declare a variable, specifying the parameter name and data type. You can also specify the passing mechanism, and whether the parameter is optional or a parameter array.
40+
41+
The syntax for each parameter in the parameter list is as follows:
42+
43+
```vb
44+
[Optional] [ByVal | ByRef] [ParamArray] parameterName As DataType
45+
```
46+
47+
If the parameter is optional, you must also supply a default value as part of its declaration. The syntax for specifying a default value is as follows:
48+
49+
```vb
50+
Optional [ByVal | ByRef] parameterName As DataType = defaultValue
51+
```
52+
53+
### Parameters as local variables
54+
55+
When control passes to the procedure, each parameter is treated as a local variable. This means that its lifetime is the same as that of the procedure, and its scope is the whole procedure.
56+
57+
## Calling syntax
58+
59+
You invoke a `Sub` procedure explicitly with a stand-alone calling statement. You cannot call it by using its name in an expression. You must provide values for all arguments that are not optional, and you must enclose the argument list in parentheses. If no arguments are supplied, you can optionally omit the parentheses. The use of the `Call` keyword is optional but not recommended.
60+
61+
The syntax for a call to a `Sub` procedure is as follows:
62+
63+
```vb
64+
[Call] SubName[(argumentlist)]
65+
```
66+
67+
You can call a `Sub` method from outside the class that defines it. First, you have to use the `New` keyword to create an instance of the class, or call a method that returns an instance of the class. For more information, see [New Operator](../../../language-reference/operators/new-operator.md). Then, you can use the following syntax to call the `Sub` method on the instance object:
68+
69+
```vb
70+
object.MethodName[(argumentList)]
71+
```
72+
73+
### Illustration of declaration and call
74+
75+
The following `Sub` procedure tells the computer operator which task the application is about to perform, and also displays a time stamp. Instead of duplicating this code at the start of every task, the application just calls `tellOperator` from various locations. Each call passes a string in the `task` argument that identifies the task being started.
76+
77+
[!code-vb[VbVbcnProcedures#2](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbcnProcedures/VB/Class1.vb#2)]
78+
79+
The following example shows a typical call to `tellOperator`.
80+
81+
[!code-vb[VbVbcnProcedures#3](~/samples/snippets/visualbasic/VS_Snippets_VBCSharp/VbVbcnProcedures/VB/Class1.vb#3)]
82+
6983
## See also
7084

7185
- [Procedures](./index.md)
7286
- [Function Procedures](./function-procedures.md)
7387
- [Property Procedures](./property-procedures.md)
7488
- [Operator Procedures](./operator-procedures.md)
7589
- [Procedure Parameters and Arguments](./procedure-parameters-and-arguments.md)
76-
- [Sub Statement](../../../../visual-basic/language-reference/statements/sub-statement.md)
90+
- [Sub Statement](../../../language-reference/statements/sub-statement.md)
7791
- [How to: Call a Procedure that Does Not Return a Value](./how-to-call-a-procedure-that-does-not-return-a-value.md)
7892
- [How to: Call an Event Handler in Visual Basic](./how-to-call-an-event-handler.md)

0 commit comments

Comments
 (0)