VB difference in the Sub procedure and the Function procedure

VB general process is divided into two types: Sub procedures and Function procedures. General procedure either stored in a form module (.frm), or may be stored in a standard module (.Bas) in. General procedure and process different events, it is not attached to an object, the object is not there a certain event-driven or called automatically by the system, but it works by calling the call statement (such as Call statement). General procedure may be called multiple times, calling the procedure is called the calling procedure.

Sub process (subroutine)

Syntax: [Public | private] Sub subroutine name ([parameter list])

                   <Statement>

                  End Sub

Sub-process has no return value, that is, it can only execute a block of statements between Sub and End Sub, sub-process can not speak of the value assigned to a variable.

Use Sub procedures must comply with the "first explanation, after calling" principle, the statement Sub procedure is to write a program segment can achieve the desired functionality, can be called repeatedly.

Subroutine call:

  • Subroutine call using Call statement: Call <subroutine name> [(<argument list columns>)]

The syntax is the parameter list is a list of actual parameters, if there are a plurality of parameters, available "," separated.

When using this statement calls the sub process, if the process itself has no arguments, the <subroutine name> <argument list> parentheses may be omitted and the back; it should be given the actual parameters, and the parameters enclosed by parentheses.

  • Using the sub-call procedure name: <subroutine name> [<argument list>]

When the subroutine call to this method, not the actual parameters enclosed in parentheses.

Function ( function procedure )

Process and Function Sub difference process are: the process function returns a value, and the sub-process has no return value.

Syntax is as follows: [Private | Public] Function <Function procedure name> [(parameter list)] [As <type>]

                        <Statement block>

                        Function name = expression

                      End Function

Function procedure calls:

Use an ordinary function calls and function procedure is the same, just write arguments to the function name and function of the process definition.

Direct use of the function name calling methods in two forms:

  • If the function returns a value, typically in the form of the following call:

The object is assigned a function name = (actual parameters 1, 2 actual parameters, ...)

  • If you give up the return value of the function (with the same call a Sub procedure using the Call statement to call Function procedures), general call in the following form:

The actual name of the function parameters 1, the actual parameters 2, ...

The difference between the Sub procedure and a Function procedure: 

1. No definition Return Value Type Sub when process definition, the process generally requires a Function "of As data type" defines the type of function return value. 

2. Sub no statement during the procedure name assignment, and Function process must have statement to the function name assigned. 

3. Call the procedure: call a Sub procedure and a Function procedure different. Call the Sub procedure is a separate statement, calling the function process is only part of the expression. Sub process and there is a little different functions, it does not return a value by name. However, like a Function procedure, Sub process may be passed to modify any variable value thereof. 

4. There are two ways to call a Sub procedure: 

The following two statements are called the Sub procedure called Fact. 

Call Fact (m, fac) 

Fact m, fac

Note that when using the Call syntax, parameters must be in parentheses. If you omit the Call keyword, you also must omit the parentheses around the argument.

Released four original articles · won praise 0 · Views 64

Guess you like

Origin blog.csdn.net/mumuxi709/article/details/103971656