- GUI
- Windows API tutorial
- Introduction to Windows API
- Windows API main functions
- System functions in Windows API
- Strings in Windows API
- Date & time in Windows API
- A window in Windows API
- First steps in UI
- Windows API menus
- Windows API dialogs
- Windows API controls I
- Windows API controls II
- Windows API controls III
- Advanced controls in Windows API
- Custom controls in Windows API
- The GDI in Windows API
- PyQt4 tutorial
- PyQt5 tutorial
- Qt4 tutorial
- Introduction to Qt4 toolkit
- Qt4 utility classes
- Strings in Qt4
- Date and time in Qt4
- Working with files and directories in Qt4
- First programs in Qt4
- Menus and toolbars in Qt4
- Layout management in Qt4
- Events and signals in Qt4
- Qt4 Widgets
- Qt4 Widgets II
- Painting in Qt4
- Custom widget in Qt4
- The Breakout game in Qt4
- Qt5 tutorial
- Introduction to Qt5 toolkit
- Strings in Qt5
- Date and time in Qt5
- Containers in Qt5
- Working with files and directories in Qt5
- First programs in Qt5
- Menus and toolbars in Qt5
- Layout management in Qt5
- Events and signals in Qt5
- Qt5 Widgets
- Qt5 Widgets II
- Painting in Qt5
- Custom widget in Qt5
- Snake in Qt5
- The Breakout game in Qt5
- PySide tutorial
- Tkinter tutorial
- Tcl/Tk tutorial
- Qt Quick tutorial
- Java Swing tutorial
- JavaFX tutorial
- Java SWT tutorial
- wxWidgets tutorial
- Introduction to wxWidgets
- wxWidgets helper classes
- First programs in wxWidgets
- Menus and toolbars in wxWidgets
- Layout management in wxWidgets
- Events in wxWidgets
- Dialogs in wxWidgets
- wxWidgets widgets
- wxWidgets widgets II
- Drag and Drop in wxWidgets
- Device Contexts in wxWidgets
- Custom widgets in wxWidgets
- The Tetris game in wxWidgets
- wxPython tutorial
- Introduction to wxPython
- First Steps
- Menus and toolbars
- Layout management in wxPython
- Events in wxPython
- wxPython dialogs
- Widgets
- Advanced widgets in wxPython
- Drag and drop in wxPython
- Internationalisation
- Application skeletons in wxPython
- The GDI
- Mapping modes
- Creating custom widgets
- Tips and Tricks
- wxPython Gripts
- The Tetris game in wxPython
- C# Winforms Mono tutorial
- Java Gnome tutorial
- Introduction to Java Gnome
- First steps in Java Gnome
- Layout management in Java Gnome
- Layout management II in Java Gnome
- Menus in Java Gnome
- Toolbars in Java Gnome
- Events in Java Gnome
- Widgets in Java Gnome
- Widgets II in Java Gnome
- Advanced widgets in Java Gnome
- Dialogs in Java Gnome
- Pango in Java Gnome
- Drawing with Cairo in Java Gnome
- Drawing with Cairo II
- Nibbles in Java Gnome
- QtJambi tutorial
- GTK+ tutorial
- Ruby GTK tutorial
- GTK# tutorial
- Visual Basic GTK# tutorial
- PyGTK tutorial
- Introduction to PyGTK
- First steps in PyGTK
- Layout management in PyGTK
- Menus in PyGTK
- Toolbars in PyGTK
- Signals & events in PyGTK
- Widgets in PyGTK
- Widgets II in PyGTK
- Advanced widgets in PyGTK
- Dialogs in PyGTK
- Pango
- Pango II
- Drawing with Cairo in PyGTK
- Drawing with Cairo II
- Snake game in PyGTK
- Custom widget in PyGTK
- PHP GTK tutorial
- C# Qyoto tutorial
- Ruby Qt tutorial
- Visual Basic Qyoto tutorial
- Mono IronPython Winforms tutorial
- Introduction
- First steps in IronPython Mono Winforms
- Layout management
- Menus and toolbars
- Basic Controls in Mono Winforms
- Basic Controls II in Mono Winforms
- Advanced Controls in Mono Winforms
- Dialogs
- Drag & drop in Mono Winforms
- Painting
- Painting II in IronPython Mono Winforms
- Snake in IronPython Mono Winforms
- The Tetris game in IronPython Mono Winforms
- FreeBASIC GTK tutorial
- Jython Swing tutorial
- JRuby Swing tutorial
- Visual Basic Winforms tutorial
- JavaScript GTK tutorial
- Ruby HTTPClient tutorial
- Ruby Faraday tutorial
- Ruby Net::HTTP tutorial
- Java 2D games tutorial
- Java 2D tutorial
- Cairo graphics tutorial
- PyCairo tutorial
- HTML5 canvas tutorial
- Python tutorial
- Python language
- Interactive Python
- Python lexical structure
- Python data types
- Strings in Python
- Python lists
- Python dictionaries
- Python operators
- Keywords in Python
- Functions in Python
- Files in Python
- Object-oriented programming in Python
- Modules
- Packages in Python
- Exceptions in Python
- Iterators and Generators
- Introspection in Python
- Ruby tutorial
- PHP tutorial
- Visual Basic tutorial
- Visual Basic
- Visual Basic lexical structure
- Basics
- Visual Basic data types
- Strings in Visual Basic
- Operators
- Flow control
- Visual Basic arrays
- Procedures & functions in Visual Basic
- Organizing code in Visual Basic
- Object-oriented programming
- Object-oriented programming II in Visual Basic
- Collections in Visual Basic
- Input & output
- Tcl tutorial
- C# tutorial
- Java tutorial
- AWK tutorial
- Jetty tutorial
- Tomcat Derby tutorial
- Jtwig tutorial
- Android tutorial
- Introduction to Android development
- First Android application
- Android Button widgets
- Android Intents
- Layout management in Android
- Android Spinner widget
- SeekBar widget
- Android ProgressBar widget
- Android ListView widget
- Android Pickers
- Android menus
- Dialogs
- Drawing in Android
- Java EE 5 tutorials
- Introduction
- Installing Java
- Installing NetBeans 6
- Java Application Servers
- Resin CGIServlet
- JavaServer Pages, (JSPs)
- Implicit objects in JSPs
- Shopping cart
- JSP & MySQL Database
- Java Servlets
- Sending email in a Servlet
- Creating a captcha in a Servlet
- DataSource & DriverManager
- Java Beans
- Custom JSP tags
- Object relational mapping with iBATIS
- Jsoup tutorial
- MySQL tutorial
- MySQL quick tutorial
- MySQL storage engines
- MySQL data types
- Creating, altering and dropping tables in MySQL
- MySQL expressions
- Inserting, updating, and deleting data in MySQL
- The SELECT statement in MySQL
- MySQL subqueries
- MySQL constraints
- Exporting and importing data in MySQL
- Joining tables in MySQL
- MySQL functions
- Views in MySQL
- Transactions in MySQL
- MySQL stored routines
- MySQL Python tutorial
- MySQL Perl tutorial
- MySQL C API programming tutorial
- MySQL Visual Basic tutorial
- MySQL PHP tutorial
- MySQL Java tutorial
- MySQL Ruby tutorial
- MySQL C# tutorial
- SQLite tutorial
- SQLite C tutorial
- SQLite PHP tutorial
- SQLite Python tutorial
- SQLite Perl tutorial
- SQLite Ruby tutorial
- SQLite C# tutorial
- SQLite Visual Basic tutorial
- PostgreSQL C tutorial
- PostgreSQL Python tutorial
- PostgreSQL Ruby tutorial
- PostgreSQL PHP tutorial
- PostgreSQL Java tutorial
- Apache Derby tutorial
- SQLAlchemy tutorial
- MongoDB PHP tutorial
- MongoDB Java tutorial
- MongoDB JavaScript tutorial
- MongoDB Ruby tutorial
- Spring JdbcTemplate tutorial
- JDBI tutorial
Procedures & functions in Visual Basic
In this part of the tutorial, you will learn Visual Basic procedures & functions.
We use procedures and functions to create modular programs. Visual Basic statements are grouped in a block enclosed by Sub
, Function
and matching End
statements. The difference between the two is that functions return values, procedures do not.
A procedure and function is a piece of code in a larger program. They perform a specific task. The advantages of using procedures and functions are:
- Reducing duplication of code
- Decomposing complex problems into simpler pieces
- Improving clarity of the code
- Reuse of code
- Information hiding
Procedures
A procedure is a block of Visual Basic statements inside Sub
, End Sub
statements. Procedures do not return values.
Option Strict On Module Example Sub Main() SimpleProcedure() End Sub Sub SimpleProcedure() Console.WriteLine("Simple procedure") End Sub End Module
This example shows basic usage of procedures. In our program, we have two procedures. The Main()
procedure and the user defined SimpleProcedure()
. As we already know, the Main()
procedure is the entry point of a Visual Basic program.
SimpleProcedure()
Each procedure has a name. Inside the Main()
procedure, we call our user defined SimpleProcedure()
procedure.
Sub SimpleProcedure() Console.WriteLine("Simple procedure") End Sub
Procedures are defined outside the Main()
procedure. Procedure name follows the Sub
statement. When we call a procedure inside the Visual Basic program, the control is given to that procedure. Statements inside the block of the procedure are executed.
Procedures can take optional parameters.
Option Strict On Module Example Sub Main() Dim x As Integer = 55 Dim y As Integer = 32 Addition(x, y) End Sub Sub Addition(ByVal k As Integer, ByVal l As Integer) Console.WriteLine(k+l) End Sub End Module
In the above example, we pass some values to the Addition()
procedure.
Addition(x, y)
Here we call the Addition()
procedure and pass two parameters to it. These parameters are two Integer values.
Sub Addition(ByVal k As Integer, ByVal l As Integer) Console.WriteLine(k+l) End Sub
We define a procedure signature. A procedure signature is a way of describing the parameters and parameter types with which a legal call to the function can be made. It contains the name of the procedure, its parameters and their type, and in case of functions also the return value. The ByVal
keyword specifies how we pass the values to the procedure. In our case, the procedure obtains two numerical values, 55 and 32. These numbers are added and the result is printed to the console.
Functions
A function is a block of Visual Basic statements inside Function
, End Function
statements. Functions return values.
There are two basic types of functions. Built-in functions and user defined ones. The built-in functions are part of the Visual Basic language. There are various mathematical, string or conversion functions.
Option Strict On Module Example Sub Main() Console.WriteLine(Math.Abs(-23)) Console.WriteLine(Math.Round(34.56)) Console.WriteLine("ZetCode has {0} characters", _ Len("ZetCode")) End Sub End Module
In the preceding example, we use two math functions and one string function. Built-in functions help programmers do some common tasks.
In the following example, we have a user defined function.
Option Strict On Module Example Dim x As Integer = 55 Dim y As Integer = 32 Dim result As Integer Sub Main() result = Addition(x, y) Console.WriteLine(Addition(x, y)) End Sub Function Addition(ByVal k As Integer, _ ByVal l As Integer) As Integer Return k+l End Function End Module
Two values are passed to the function. We add these two values and return the result to the Main()
function.
result = Addition(x, y)
Addition function is called. The function returns a result and this result is assigned to the result variable.
Function Addition(ByVal k As Integer, _ ByVal l As Integer) As Integer Return k+l End Function
This is the Addition function signature and its body. It also includes a return data type, for the returned value. In our case is is an Integer
. Values are returned to the caller with the Return
keyword.
Passing parameters by value, by reference
Visual Basic supports two ways of passing parameters to functions. By value and by reference. For this, we have two keywords. ByVal
and ByRef
. When we pass arguments by value, the function works only with the copies of the values. This may lead to performance overheads, when we work with large amounts of data.
When we pass values by reference, the function receives a reference to the actual values. The original values are affected, when modified. This way of passing values is more time and space efficient. On the other hand, it is more error prone.
Which way of passing arguments should we use? It depends on the situation. Say we have a set of data, salaries of employees. If we want to compute some statistics of the data, we do not need to modify them. We pass by values. If we work with large amounts of data and the speed of computation is critical, we pass by reference. If we want to modify the data, e.g. do some reductions or raises to the salaries, we might pass by reference.
The following two examples cover both concepts.
Option Strict On Module Example Dim a As Byte = 4 Dim b As Byte = 7 Sub Main() Console.WriteLine("Outside Swap procedure") Console.WriteLine("a is {0}", a) Console.WriteLine("b is {0}", b) Swap(a, b) Console.WriteLine("Outside Swap procedure") Console.WriteLine("a is {0}", a) Console.WriteLine("b is {0}", b) End Sub Sub Swap(ByVal a As Byte, ByVal b As Byte) Dim temp As Byte temp = a a = b b = temp Console.WriteLine("Inside Swap procedure") Console.WriteLine("a is {0}", a) Console.WriteLine("b is {0}", b) End Sub End Module
The Swap()
procedure swaps the numbers between the a
and b
variables. The original variables are not affected.
Dim a As Byte = 4 Dim b As Byte = 7
At the beginning, these two variables are initiated.
Swap(a, b)
We call the Swap()
procedure. The procedure takes a
and b
variables as parameters.
temp = a a = b b = temp
Inside the Swap()
procedure, we change the values. Note that the a
and b
variables are defined locally. They are valid only inside the Swap()
procedure.
$ ./swap1.exe Outside Swap procedure a is 4 b is 7 Inside Swap procedure a is 7 b is 4 Outside Swap procedure a is 4 b is 7
The output shows that the original variables were not affected.
The next code example passes values to the function by reference. The original variables are changed inside the Swap()
procedure.
Option Strict On Module Example Dim a As Byte = 4 Dim b As Byte = 7 Sub Main() Console.WriteLine("Outside Swap procedure") Console.WriteLine("a is {0}", a) Console.WriteLine("b is {0}", b) Swap(a, b) Console.WriteLine("Outside Swap procedure") Console.WriteLine("a is {0}", a) Console.WriteLine("b is {0}", b) End Sub Sub Swap(ByRef a As Byte, ByRef b As Byte) Dim temp As Byte temp = a a = b b = temp Console.WriteLine("Inside Swap procedure") Console.WriteLine("a is {0}", a) Console.WriteLine("b is {0}", b) End Sub End Module
In this example, calling the Swap()
procedure will change the original values.
Sub Swap(ByRef a As Byte, ByRef b As Byte) ... End Sub
Now we use the ByRef
keyword to indicate that we pass parameters by reference.
$ ./swap2.exe Outside Swap procedure a is 4 b is 7 Inside Swap procedure a is 7 b is 4 Outside Swap procedure a is 7 b is 4
Here we see that the Swap()
procedure really changed the values of the variables.
Recursion
Recursion, in mathematics and computer science, is a method of defining functions in which the function being defined is applied within its own definition. (Wikipedia) In other words, a recursive function calls itself to do its job. Recursion is a widely used approach to solve many programming tasks.
A typical example is calculation of the factorial.
Option Strict On Module Example Sub Main() Console.WriteLine(Factorial(4)) Console.WriteLine(Factorial(10)) End Sub Function Factorial(ByVal n As UShort) As UShort If (n=0) Return 1 Else Return n * Factorial(n-1) End If End Function End Module
In this code example, we calculate the factorial of two numbers.
Return n * Factorial(n-1)
Inside the body of the factorial function, we call the factorial function with a modified argument. The function calls itself.
$ ./recursion.exe 24 3628800
These are the results.
Module scope, procedure scope
A scope is the range in which a variable can be referenced. A variable which is declared inside the procedure has a procedure scope. It is valid only in this particular procedure. A variable declared inside a module has a module scope. It is valid everywhere in the module.
Option Strict On Module Example Dim a As Byte = 2 Sub Main() Dim b As Byte = 3 Console.WriteLine(a) SimpleProcedure() End Sub Sub SimpleProcedure() Console.WriteLine(a) ' Console.WriteLine(b) End Sub End Module
In the preceding example, we declare two variables. Variable a has the module scope, variable b has the procedure scope.
Dim a As Byte = 2
Variable a is declared inside the Example module, outside the two procedures. It is valid in both procedures.
Sub Main() Dim b As Byte = 3 ... End Sub
The variable b is declared in the Main()
procedure. It is valid only there. It is not valid in the second procedure.
Sub SimpleProcedure() Console.WriteLine(a) ' Console.WriteLine(b) End Sub
The statement printing the b
variable is commented. If we uncommented the line, the example would not compile.
Option Strict On Module Example Dim a As Byte = 2 Sub Main() Dim a As Byte = 3 Console.WriteLine(a) End Sub End Module
In the preceding example, we have declared a variable with the same name in two different scopes. They do not collide. The variable declared inside the Main()
procedure overrides the one, declared in the module scope.
$ ./scope2.exe 3
Output.
Static variables
A static variable is a variable that has been allocated statically, whose lifetime extends across the entire run of the program. (Wikipedia) The default, local variables do not retain their value within consecutive calls of the function.
Option Strict On Module Example Sub Main() NonStatic() NonStatic() NonStatic() NonStatic() Console.WriteLine(NonStatic) End Sub Function NonStatic() As Integer Dim x As Integer = 0 x += 1 Return x End Function End Module
In the above example, we have a normal, non-static variable. We increment the variable each time the function is called. We call the function 5 times. However, non-static variables are initiated for each call of the function.
$ ./nonstatic.exe 1
After 5 function calls the x
variable equals to 1.
The static variables are initiated only once, when the function is first called. They retain their value afterwards.
Option Strict On Module Example Sub Main() StaticFunction() StaticFunction() StaticFunction() StaticFunction() Console.WriteLine(StaticFunction) End Sub Function StaticFunction() As Integer Dim Static x As Integer = 0 x += 1 Return x End Function End Module
After 5 consecutive calls, the x
is equal to 5.
Dim Static x As Byte = 0
Static variables are created with the Static
keyword.
$ ./static.exe 5
Running the example.
In this part of the Visual Basic tutorial, we covered procedures and functions.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论