Differences between Sub and Function in ASP

Source: Internet
Author: User

What is the difference between SUB and FUNCTION? How should their syntax be structured?
Sub: process;
Function: a Function that can return values.
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 ,...
If the Function does not return values, use the FunctionName parameter 1, parameter 2 ,...
If you want to return a value, 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
Call time:
Sub can only be used:
SubName parameter 1, parameter 2 ,...
Function:
Variable = FunctionName (parameter 1, parameter 2 ,...)
FunctionName parameter 1, parameter 2 ,...
Not mentioned above:
Both SUB and FUNCTION can return values. Therefore, we must first clarify the return methods: There are two types of process or function return, that is, assigning the return variable address with the same name as the process or function. Function allocation, but sub is not allocated. VB uses this method to differentiate, while VC uses VOID to declare that no allocation is required. That is, if p = aa () and aa () is sub, nothing is obtained and an error is returned. However, the function returns a value. Second, the parameter is returned. By default, parameters in VB can be returned by passing parameters through the address. However, if it is declared as BYVAL, that is, passing a value parameter, it cannot be returned. Therefore, there is no difference in flexibility. For example, if function bb (a, B) is called, it can be bb m, n or p = bb (m, n ). actually, SUB can be used only if the confirmation does not fail. Otherwise, the FUNCTION must be used to confirm whether the operation is successful or obtain the return value. Therefore, we should use less SUB for programming, and less call methods for SubName parameter 1, parameter 2.
Sub is a process that does not require a return value; function is a function that requires a return value, as follows:
Copy codeThe Code is as follows:
Function NameOfFunction (parameter1, parameter2)
'Some code
NameOfFunction = return value
End Function

Copy codeThe Code is as follows:
Sub NameOfSubRoutine (parameter1, parameter2)
'Some code...
End Sub

Except for the difference that a function returns a value, there seems to be little difference in ASP. It seems that the effect of writing sub into a FUNCTION is the same.

Sub has no return value.
Call sub
Call function <% = function %>
Generally, functions can be used to replace everything, except for some sub which must be used -- for example, if an event is triggered (private sub xxx_OnYYY ). The benefit of using a function is that it returns a value. If you think that using a sub, the return value of a function is used to tell you whether a program has an error. Generally, if the return value is 0, the operation is successful. If the return value is other values, the error code is returned.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.