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

Default arguments (Optional parameters)

This lesson covers default arguments with VB.NET's Optional parameters, with the aim of writing flexible functions whose arguments can be left out.

Optional name As Type = value creates a default argument the caller can omit. Leave it out and the value you specified in advance is used automatically.

The sample code defines Sub Greet(Optional name As String = "Guest"), which handles both Greet("Alice") with a name and Greet() with nothing at all.

A common early stumble is the order of the parameters. Optional parameters have to come after the ordinary ones, and getting that wrong is a compile error.

In professional work, Optional parameters suit functions that usually take the same value but occasionally need a special one, and they keep the calling code simple.

๐Ÿ“– 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)