Home  >  Article  >  Web Front-end  >  Explanation of the difference between Sub and Function in ASP_javascript skills

Explanation of the difference between Sub and Function in ASP_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:12:371509browse

What is the difference between SUB and FUNCTION, and how should their syntax be structured?
Sub: process;
Function: function, which can bring a return value
Syntax:
Sub SubName (parameter 1, parameter 2,...)
....
End Sub
Function FunctionName(parameter 1, parameter 2,...)
...
FunctionName = Return value
End Function
Call method:
Sub directly uses SubName parameter 1 , Parameter 2,...
Function If you don’t want to return a value, use FunctionName Parameter 1, Parameter 2,...
If you want to return a value, then Result = FunctionName(Parameter 1, Parameter 2,...)
The syntax is like this, this is correct
Sub SubName(parameter 1, parameter 2,...)
....
End Sub
Function FunctionName(parameter 1, parameter 2 ,...)
...
FunctionName = Return value
End Function
When calling:
sub can only be used:
SubName Parameter 1, Parameter 2,...
Function rules:
Variable=FunctionName (parameter 1, parameter 2,...)
FunctionName parameter 1, parameter 2,...
The above does not explain the root cause:
SUB Both FUNCTION and FUNCTION can have return values. So first we need to explain the return method: there are two types, process or function return, that is, assigning a return variable address with the same name as the process or function. function allocates, but sub does not. VB uses this method to distinguish, while VC uses VOID to declare without allocation. That is, if p=aa(), if aa() is sub, nothing will be obtained and an error will be reported. But the function will get a numerical value. Secondly, the parameters are returned. By default in VB, parameters are passed by address, which means they can be returned. However, if the parameter is declared as BYVAL, it cannot be returned, so there is no difference in flexibility. For example, function bb(a,b) can be called with bb m, n or p=bb(m,n). In fact, SUB can only be used if it is confirmed that it will not fail. Otherwise, FUNCTION must be used to confirm whether it is successful. Or get the return value. Therefore, when programming, you should use less SUB and less use of the SubName parameter 1, parameter 2,... calling method.
sub is a process that does not need to return a value; function is a function that needs to return a value, as follows:

Copy code The code is as follows:

Function NameOfFunction(parameter1, parameter2)
'some code
NameOfFunction = return value
End Function

Copy code The code is as follows:

Sub NameOfSubRoutine(parameter1, parameter2)
'some code...
End Sub

Except for the difference that function returns a value, there doesn’t seem to be much difference in ASP. Writing sub as FUNCTION seems to have the same effect.

sub has no return value
call sub can call sub
call function can<%=function%>
Usually you can use function to replace everything, except for some things that must use sub - -For example, the event is triggered by (private sub xxx_OnYYY). The advantage of using function is that it returns a value. If you think you can use sub, the return value of function is used to tell you whether there is an error in the program. Generally speaking, returning 0 indicates success, and other values ​​​​are error codes.
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn