Visual Basic Code Examples Pdf

last modified July 6, 2020

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:

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. ADO Code Examples in Visual Basic.; 2 minutes to read; r; D; M; c; C; In this article. Use the following code examples to learn how to use the ADO methods, properties, and events when writing in Visual Basic.

Visual Basic 2008 Code Examples Pdf

  • 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.

This example shows basic usage of procedures. In our program, we have twoprocedures. The Main() procedure and the user defined SimpleProcedure(). As we already know, the Main() procedure is the entry point of a Visual Basic program.

Each procedure has a name. Inside the Main() procedure,we call our user defined SimpleProcedure() procedure.

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 ofthe procedure are executed.

Procedures can take optional parameters.

In the above example, we pass some values to the Addition()procedure.

Here we call the Addition() procedure and pass two parameters to it.These parameters are two Integer values.

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 keywordspecifies how we pass the values to the procedure. In our case, the procedureobtains 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.

In the preceding example, we use two math functions and onestring function. Built-in functions help programmers do somecommon tasks.

In the following example, we have a user definedfunction.

Two values are passed to the function. We add these two values and return theresult to the Main() function.

Addition function is called. The function returns a result and thisresult is assigned to the result variable.

This is the Addition function signature and its body. It alsoincludes a return data type, for the returned value. In ourcase is is an Integer. Values are returnedto the caller with the Return keyword.

Passing parameters by value, by reference

Visual Basic supports two ways of passing parameters to functions. By valueand by reference. For this, we have two keywords. ByVal andByRef. When we pass arguments by value, the functionworks 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.

Visual

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 modifythe data, e.g. do some reductions or raises to the salaries, we might pass by reference.

The following two examples cover both concepts.

The Swap() procedure swaps the numbers between the a and b variables. The original variables are not affected.

At the beginning, these two variables are initiated.

We call the Swap() procedure. The procedure takes a and b variables as parameters.

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.

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.

In this example, calling the Swap() procedure will change the original values.

Visual basic code examples pdf download

Now we use the ByRef keyword to indicate thatwe pass parameters by reference.

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. 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.

In this code example, we calculate the factorial of twonumbers.

Inside the body of the factorial function, we call the factorial functionwith a modified argument. The function calls itself.

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 procedurescope. It is valid only in this particular procedure.A variable declared inside a module has a module scope. It isvalid everywhere in the module.

Free

In the preceding example, we declare two variables. Variablea has the module scope, variable b has the procedure scope.

Examples

Variable a is declared inside the Example module, outside thetwo procedures. It is valid in both procedures.

The variable b is declared in the Main() procedure.It is valid only there. It is not valid in the secondprocedure.

The statement printing the b variable is commented. If we uncommentedthe line, the example would not compile.

In the preceding example, we have declared a variable with thesame name in two different scopes. They do not collide.The variable declared inside the Main() procedure overrides theone, declared in the module scope.

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.

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 eachcall of the function.

After 5 function calls the x variable equals to 1.

Visual Basic Code Examples For Excel

The static variables are initiated only once, when the function is first called. They retain their value afterwards.

After 5 consecutive calls, the x is equal to 5.

Free Visual Basic Code Examples

Static variables are created with the Statickeyword.

Visual Basic Source Code Examples Pdf

Running the example.

Visual Basic Examples Pdf

In this part of the Visual Basic tutorial, we covered procedures and functions.