Lesson 9: Functions

In previous lessons you have learned to use functions like Ubound and Weekday. In this lesson, you will learn to create your own functions using DocumentationFunction .

What is a function?

A function process inputs and return an output. It can be useful if, for example, you have a wide range of data you have processed or if you have calculations or routines in other ways that must be performed many times.

A function has the following syntax:


	Function Name(list of parameters)
	   Statement
	End Function
	
	

This way, we can make a very simple function that can add the value 1 to a number. It could look like this:

	Function AddOne(t)
	   t = t + 1
	   AddOne = t
	End Function

	
	

Our function is named AddOne and must be called with a number - e.g. 34....

	Response.Write AddOne(34)
	
	

... which (surprise!) will return 35.

The example above processes a number, but functions can work with text, dates or anything else. You can also create functions that are called by many different parameters. In this lesson you will see different examples of functions.

Example: Function with more parameters

As mentioned above, you can easily create a function that can be called with more parameters. In the next example, we'll create a function that is called with three numbers and then returns the value of the three numbers added together:


	<html>
	<head>
	<title>Functions</title>

	</head>
	<body>
	<%
	Function AddAll(number1, number2, number3)

	   AddAll = number1 + number2 + number3
	End Function

	Response.Write "123 + 654 + 9 equals " & AddAll(123,654,9)

	%>
	</body>

	</html>
	
	

Ok. Now that was almost too simple! But the point was just to show you that a function can be called with more parameters.

Example: English date and time

Let us try to make a slightly more complex function. A function that's called with a date and time returns it in the format: Wednesday, 15 February, 2012, 10:00:00 AM


	<html>
	<head>
	<title> Loops </title>
	</head>
	<body>

	<%
	Function EnglishDateTime(strDate)

	  ' Array with the English names for days of the week
	  arrDay = "Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday"
	  arrDay = Split(arrDay,",")

	  ' Array with the English names of the months

	  arrMonth = "January,February,March,April,May,June,July,August,"
	  arrMonth = arrMonth & "September,October,November,December"
	  arrMonth = Split(arrMonth,",")

	  ' The date is constructed

	  EnglishDateTime = arrDay(Weekday(strDate,vbMonday)) & ",  " & Day(strDate) "

	  EnglishDateTime = EnglishDateTime & arrMonth(Month(srDate)) & ", " & Year(strDate) 

	  EnglishDateTime = EnglishDateTime & ",  " & FormatDatetime(strDate, vbLongTime)

	End Function

	' Test function
	Response.Write EnglishDateTime(Now)

	%>
	</body>
	</html>
	
	

Please note how 'ArrMonth' and 'EnglishDateTime' are constructed over several lines. This is done so that users with a low screen resolution better can see the example. The method has no effect on the code itself.

The function above works on all web servers regardless of language. This means that you can use such a function if your website, for example, is hosted on a French server, but you want English dates.

At this stage, we will not go into more depth with functions, but now you know a little about how functions work.



<< Lesson 8: Arrays

Lesson 10: Passing variables in a URL >>