Section 7.4. Functions: Methods That Return a Value


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 110.

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 As Integer) As Integer             15        Return y ^ 2 ' return square of parameter value         16     End Function ' Square                                      17  End Module ' SquareInteger 

 Number Square 1      1 2      4 3      9 4      16 5      25 6      36 7      49 8      64 9      81 10     100 



The For...Next statement (lines 810) displays the results of squaring the integers from 110. Each iteration of the loop calculates and displays the square of control variable counter (line 9).

Function Square is invoked (line 9) with the expression Square(counter). When program control reaches this expression, the program calls Square (lines 1416). At this point, the program makes a copy of the value of counter (the argument), and program control transfers to the first line of Square. Square receives the copy of counter's value and stores it in the parameter y. Line 15 is a Return statement, which terminates execution of the method and returns the result of y ^ 2 to the calling program. The result is returned to the point in line 9 where Square was invoked. Line 9 displays in the command prompt the value of counter and the value returned by Square. This process is repeated 10 times. Note that returning a value from a function is similar to returning a value from the Get accessor of a property.

Function Declarations

The format of a function declaration is


Function method-name(parameter-list) As return-type
        declarations and statements
End Function

The method-name, parameter-list, and declarations and statements in a function declaration behave like the corresponding elements in a subroutine declaration. In the function header, the return-type indicates the type of the result returned from the function to its caller. The statement



Return expression

can occur anywhere in a function body and returns the value of expression to the caller. If necessary, Visual Basic attempts to convert the expression to the function's return-type. Functions Return exactly one value. When a Return statement is executed, control returns immediately to the point at which that function was invoked.

Common Programming Error 7.2

If the expression in a Return statement cannot be converted to the function's return-type, a runtime error is generated.


Common Programming Error 7.3

Failure to return a value from a function (e.g., by forgetting to provide a Return statement) causes the function to return the default value for the return-type, possibly producing incorrect output.




Visual BasicR 2005 for Programmers. DeitelR Developer Series
Visual Basic 2005 for Programmers (2nd Edition)
ISBN: 013225140X
EAN: 2147483647
Year: 2004
Pages: 435

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net