7.4. Functions: Methods That Return a Value

Functions (known as Function procedures in earlier versions of Visual Basic) are methods that return a value to the caller (whereas subroutines do not). The console application in Fig. 7.2 uses the function Square to calculate the squares of the integers from 1–10.

Figure 7.2. Function for squaring an integer.
					1
					' Fig. 7.2: SquareInteger.vb
					2
					' Function that squares a number.
					3
					Module SquareInteger
 4
					Sub Main()
 5        Console.WriteLine("Number" & vbTab & "Square")
 6
					7
					' square integers from 1 to 10
					8
					For counter As Integer = 1
					To
					10
					9           Console.WriteLine(counter & vbTab & Square(counter))
10
					Next
					11
					End Sub ' Main
					12
					13
					' function Square is executed when it is explicitly called
					14
					Function Square(ByVal y ...

Get Visual Basic 2005 for Programmers: Deitel Developer Series, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.