Pass By Reference & Optional Pass By Value in Visual Basic 2010

Pass By Reference (Call By Reference):

Functions always need some information such as variables about the state of the program to do their job. When a variable is passed to a function, it is called an argument or parameter.

Parameters are placeholders for the data we need to pass into the procedures such as functions or subroutines. Programmers use two different methods for passing in parameters to a procedure: Pass by value and Pass by reference.

Pass by Value (Call by Value) is discussed in a previous article. You can find it here!

When variables are passed by reference, only memory addresses of the variables are passed. In other words, the values passed can be modified by the subroutine and would have the new value. A variable that is expected a “return” value or output value must be passed by reference. The keyword “ByRef” is used to indicate an argument passed by reference.

Arguments are by default passed by reference in Visual Basic 2010.

Example (Pass by reference):

In the example below; the variable num1 is passed by reference to the private sub incrementValue. In the sub the value of num1 is incremented by 5 and then returned back to the main sub.


Output 1 Pass By ReferenceOutput 2 Pass By Reference





Output 3 Pass By Reference










Optional Pass By Value:

Visual Basic allows the programmer to create subroutines or functions that have optional arguments. Optional arguments that may or may not be passed; and if not passed will assume a default value. Optional arguments are specified in the procedure header with the keyword Optional.

Optional arguments that are not passed will take default values. Also you can explicitly set the value of an optional argument by assigning it a value in the procedure header. Otherwise; numeric arguments will default to 0, strings will default to “ ”, and booleans will default to False.

See also: [button link=”” size=”default” icon=”fa-gittip” side=”left” target=”” color=”a3b75f” textcolor=”ffffff”]Looping Structures in Visual Basic 2010[/button]

Example (Optional Pass By Value):

In this example; the string Title (first textbox) is optional. Additionally; if the user enters any title, the procedure will display it. Otherwise the default title “Default Title” is used instead.


When the user enters the Title:

Output 4 OptionalOutput 5 Optional









When the user does not enter the Title and default title is used:

Output 6 OptionalOutput 7 Optional









Go on and create more subs and procedures and mess up with Pass by value and Pass by reference and create Optional Pass by values too.

Moreover; stay tuned for further tutorials on Visual Basic 2010 on

Please comment and share us with your classmates and friends!


  • Leave a Reply

    %d bloggers like this: