Outline

 Exit Func. & Exit Sub
 

Your current position:
Home 
  Visual Basic
    Programming Basics
     
Functions & Procedures

Functions and Procedures

In your code you may frequently need to use the same instructions, by declaring a function/procedure and typing the needed instructions into it you can replace the repeated instructions with a call to this function. Functions/Procedures are a very useful part of Visual Basic programming because your code will be smaller more stable, faster and easy to debug.

The difference between them is that functions must return a value and procedures do not.

Declaring functions, procedures in Visual Basic
To declare a function/procedure you can use the Add Procedure (this dialog can be found in the Tools menu) dialog box or
manually declare it by using the following syntax: 

[Function | Sub] [function/procedure name] ( [param1], [param2],…, [param n] ) As [return type]

  • [function/procedure name] – can be any name you want but it must not start with a number or contain a symbol (!@#$%^&*()+)
  • [param1], [param2],…, [param n] – a list of variables that will contain the function parameters. These variables are decalred exeacly like normal variables only without the “dim” keyword e.g.( Function GetUserName(MachineName As String) As String)

Exit Function, Exit Sub Statements

At some point in your code you may want to exit the function/procedure and continue execution of the code that fired it, you can do this using the Exit Function or Exit Sub statements.

Example:

 Sub SaveConfiguration() 
     ‘(first code)
     Exit Sub
     ‘(next code)
 End Sub