Programming:Visual Basic Classic/Procedures and Functions - eMyTextBooks

Search:  

Free online books - just read it!

See live article   •   Programming:Visual Basic Classic/Procedures and Functions
 

Programming:Visual Basic Classic/Procedures and Functions

Table of contents

Introduction

Functions are named blocks program code that perform a specific task and return a result. The task can be as simple as calculating the sum of two numbers or as complex as launching a spacecraft. A procedure is simply a function that does not return a result, procedures are used for their side effects.

Example function:

 Public Sum(Byref a As Double, b As Double) As Double
   Sum = a + b
 End Function

Example subroutine:

 Public Sub Tell(Byref s as string)
   MsgBox s
 End Sub

You can use these two as follows:

 Dim a as Double
 Dim b as Double
 Dim c as Double
 a = 123.456
 b = 234
 c = Sum(a, b)
 Tell "The sum of " & a & " and " & b & " is " & c

The result will be a message box showing the text 'The sum of 123.456 and 234 is 357.456' unless your computer is set to use a comma as the decimal separator in which case it will say 'The sum of 123,456 and 234 is 357,456'.

Point locale
Comma locale

These small fragments exhibit a number of important features:

  • The arguments to the function are declared as [[../Glossary#Byref|ByRef]] which requires the compiler to make sure that only arguments of the specified type are used, in his case Double.
  • The function returns a value by assigning it to the function name as though the function were a variable. This contrasts with the use of the keyword return in other languages such as Python.
  • VB automatically converts numbers to strings when necessary
  • the arguments (argument list) passed to a function must be enclosed in round brackets, a parenthesis, whereas those supplied to a subroutine need not.


How to Write Procedures and functions

As you can see above a function can be quite simple. Still, it is a good idea to be aware of the formalities of writing them.

A procedure has these parts:

Visibility
Public, Friend or Private
Procedure Type
Sub, Function, Property Let, Property Get, Property Set
Name
Anything you like as long as it starts with a letter and contains only letters numbers and underscores.
Argument List
a list of the items of data that the procedure needs,
Return Type
if the type is Function or Property Get this tells the compiler what kind of thing will be returned, for instance, Double or String.
Body
all the statements that do the work.

Only the Name and the Procedure Type are absolutely required but a procedure without a body doesn't do anything!


Visibility

This seems like a very unimportant part of a procedure declaration to most people but in fact it is a very helpful feature. With it you can show that some procedures are just for use inside a module (Private), some only for use in this component (Friend) or available for the whole world (Public). You should mark procedures Private unless they will be called from outside the module. this will encourage you, and anyone who edits your program, to place related procedures in the same module which obviously makes maintenance easier.

Marking a procedure Private also means that you can have another procedure with exactly the same name in another module.

Procedure type

All procedures are either functions that return a result as the value of the function or subroutines that are called for their side effects.

Functions are declared like this:

 Private Function fHalf(ByRef y as Double) as Double
   fHalf = y / 2
 End Function

Subroutines like this:

 Private Sub sHalf(ByRef y As Double, ByRef Result As Double)
   Result = y / 2
 End Sub

The two procedures do essentially the same thing, that is, divide a number by two. The Function version does it by assigning the new value to the name of the function while the Sub version assigns it to the name of one of the arguments. This affects how you use them.

The function version can be used in an expression as follows:

 Debug.Print fHalf(10)

which will print:

 5

If you want to use the Sub you have to do something like this:

 Dim nHalf as Double
 sHalf 10, nHalf
 Debug.Print nHalf

Generally you use a Function when the result is a single thing (number, string, object) and a Sub when you either want to return several distinct things or nothing at all.

Properties are also a form of procedure. Property Get is a function, Property Let and Property Set are subroutines. For more discussion of Properties see the [[../Object Oriented Programming|Object Oriented Programming]] chapter.

[[../Data Types|Previous: Data Types]] Contents [[../Object Oriented Programming|Next: Object Oriented Programming]]


Also helps finding: visualbasicclassic, ProgrammingVisual, VisualBasic, BasicClassic, ClassicProcedures, Proceduresand, andFunctions, vishal, basi, clasic, visuel, bacic, classi, vidual, asic

   
 
  
Add to bookmarks
Top Articles
 
Ada Programming/Keywords/begin
Ada Programming/Keywords/else
Ada Programming/Keywords/for
Ada Programming/Keywords/null
Ada Programming/Keywords/pragma
Ada Programming/Keywords/procedure
Ada Programming/Keywords/return
Cookbook:Butter
Cookbook:Recipes
Cookbook:Tablespoon
Cookbook:Teaspoon
Cookbook:Vegetable
Electronics
Robotics
SA NCS:Business Studies
SA NCS:Consumer Studies
SA NCS:Geography
SA NCS:Hospitality Studies
SA NCS:Second Additional Language
SA NCS:Visual Arts
Wikibooks Pokédex:Hoenn Index
Search LiveJournal blogs for Programming:Visual Basic Classic/Procedures and Functions
 

Jobs search  •  Credit Consolidation  •  Credit Consolidation  •  Find jobs •  Chanel Handbags for Less

Copyright @ 2005 eMyTextBooks.com
This article is from Wikibooks. All text is available under the terms of the GNU Free Documentation License.