dimanche 25 septembre 2011
ASP: call functions and subroutines
Do you like this story?
With ASP, one of the main features we do like a lot is the possibility of creating procedures. There are two types of procedures: sub and functions.
Subroutines have a specific syntax:
<%
sub sub_name()
... ...
end sub
%>On the other hand, functions have the same syntax, but they output a result:
<%
function func_name()
... ...
end function
%>Examples
Let's create two simple examples: one subroutine and a function.In the following snippet, we are using a subroutine in order to sum two values:
<%
sub sub_sum(val1,val2)
response.write (val1+val2)
end sub
%>We can obtain the same result with a function:
<%
function func_sum(val1,val2)
func_sum = (val1+val2)
end function
%>Calling a sub and a function
To call the sub_sum, we need to do the following:<%
call sub_sum(1,1)
%>To call the func_sum, we do the following
<%
response.write (func_sum(1,1))
%>Differences? One and important!
You might wonder why we should use subroutines instead of functions and viceversa. The difference is very little but quite important. Infact, the point is that functions directly return the result. That means a function is completely independent and can be used in different situation. An example: let's say we want to sum two values in different parts of our document, but in a particular situation, we need to sum the two values and then divide it by 2. We can still use the func_sum function, and just "customize" the result in that occasion:<%
response.write (func_sum(1,1)/2)
%>I hope you've found the explanation useful.

This post was written by: Franklin Manuel
Franklin Manuel is a professional blogger, web designer and front end web developer. Follow him on Twitter
Inscription à :
Publier les commentaires (Atom)


0 Responses to “ASP: call functions and subroutines”
Enregistrer un commentaire