What is the difference between a sub and a function, and how should their grammar be constituted?
Sub: Process;
Function: Functions, can be returned with a value
Grammar:
Sub subname (parameter 1, parameter 2,...)
....
End Sub
Function functionname (parameter 1, parameter 2,...)
...
functionname = return value
End Function
Call Method:
Sub directly with SubName parameter 1, parameter 2,...
function if you do not return a value, use the functionname parameter 1, parameter 2,...
If you want to return a value, result = functionname (parameter 1, parameter 2,...)
The syntax is this, that's right.
Sub subname (parameter 1, parameter 2,...)
....
End Sub
Function functionname (parameter 1, parameter 2,...)
...
functionname = return value
End Function
When called:
Sub can only be used:
SubName parameter 1, parameter 2,...
Functions are:
Variable =functionname (parameter 1, parameter 2,...)
functionname parameter 1, parameter 2,...
None of the above have been said at all:
Both sub and function can have return values. So let's start with the return method: There are two ways in which a procedure or function returns, that is, assigning a return variable address with the same name as a procedure or function. function assignments, but sub is not assigned. VB in this way to distinguish, VC is void declaration without distribution. If P=AA () if AA () is a sub then nothing gets, also can error. But the function will get a numeric value. Next, the parameter returns, VB in the default address to pass the parameter, namely can return. However, a declaration of ByVal as a value is not a return, so there is no difference in flexibility. such as function bb (a,b), can be called BB m, n can also P=BB (m,n). In practice, you can use a sub if the confirmation does not fail, otherwise you must use a function to confirm success or get the return value. So programming, should less use sub, less use subname parameter 1, parameter 2,... The calling method.
A sub is a procedure that does not need to return a value; a function, which needs to return a value, as follows:
Copy Code code as follows:
Function nameoffunction (Parameter1, Parameter2)
' Some code
Nameoffunction = return value
End Function
Copy Code code as follows:
Sub nameofsubroutine (Parameter1, Parameter2)
' Some code ...
End Sub
Except that the function has the difference of returning a value, there seems to be little difference in the ASP, as if the sub was written as a function.
Sub no Return value
Call Sub Callable Sub
Call function can be <%=function%>
You can usually replace everything with a function, except for certain sub--such as the trigger of an event (Private Sub xxx_onyyy). The advantage of using a function is that it returns a value, and the return value of a function is used to tell you whether there is something wrong with a sub. In general, returning 0 indicates success, and other values are error generation.