Home >Web Front-end >JS Tutorial >Explanation of the difference between Sub and Function in ASP_javascript skills
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: