Learn to program in Visual Basic .NET, the right way

Methods - Function

What is a Function?

A Function is a block of code that returns a value.

In prior lessons, a Function was used to return String values. The Function we used in earlier lessons was the Console.ReadLine() Function.

Creating a Function

A Function is similar to a variable in that you specify the:

  • Scope (optional)
  • Name
  • Return Type

The syntax for declaring a Function is as described in the following code block:

[modifier] Function [name]() As [type]
	Return [value]
End Function

This code example is not the actual code. Everything in the square brackets is a specific component that ultimately make up the Function.

Modifier

Modifiers determine at what scope a Function can be accessed at. In later lessons, modifiers will be described more in depth, but for now the modifier will be omitted in examples.

Name

The name is user defined and follows the same naming conventions that a variable follows.

Return Type

The return type is the data type of the value that will be returned in the Function

Example

The following demonstrates how to create a Function:

Fiddle: Live Demo