Ad space (banner)
๐Ÿ”ทVB.NET Lessons
Lesson 6 / 68

Writing a function (a procedure)

This lesson covers writing a function (a procedure) in VB.NET, with the aim of producing code you can reuse.

Function ... As Type ... End Function defines a procedure that returns a value. Gathering repeated work into a function makes the code clearer and means a change only has to be made in one place.

The sample code defines a Greet function that takes a name argument and hands back a greeting with Return. Stating the return type explicitly, as As String, reflects how strict VB.NET is about types.

A common early stumble is that work returning no value uses Sub rather than Function. Not knowing that the keyword changes with the presence of a return value is a frequent source of syntax errors.

Stating the return type also tells the caller in advance what to expect. VB.NET additionally supports overloading — several functions of the same name differing in the number or type of their arguments.

๐Ÿ“– Reference code
โœ๏ธ Your code
Type your code, then press "Run"

๐Ÿงช This site can't compile or run VB.NET directly, so it checks on the spot whether what you typed matches the reference code (scoring happens entirely in your browser โ€” nothing is sent anywhere).

Ad space (banner)
Ad space (in-article)