Functions and Subroutines

In general, Functions return a value, and Subroutines don't.

Visual Basic | Delphi | C++ Builder

Visual Basic 6.0

[Private|Public| Friend] [Static]Sub procedurename (arguments)
 [Exit Sub]
End Sub

Event procedures are subroutines with special names
   ControlName & Underscore & EventName
such as
except for forms. The click procedure for My_UIForm is called
depending on its type.

[Private|Public|Friend][Static]Function functionName [(arguments)] [As type]
 [functionName = returnValue  ' This is required before an Exit
  Exit Function] 
  functionName = returnValue  ' This is required
End Function

If the As clause is missing, the Function returns a variant.

Using the provided pick lists, you can easily associate event procedures with existing controls and the subroutines will be correctly named. However, when you change the name of a control, you must also manually change the name of every existing procedure that you still want to be associated with that contol.

   ' Both of these statements call a Sub named MyProc.
   Call MyProc (FirstArgument, SecondArgument)
   MyProc FirstArgument, SecondArgument

Note that when you use the Call syntax, arguments must be enclosed in parentheses. If you omit the Call keyword, you must also omit the parentheses around the arguments.

   ' All of the following statements would call a function named ToDec
   Print 10 * ToDec
   X = ToDec
   If ToDec = 10 Then Debug.Print "Out of Range"
   X = AnotherFunction(10 * ToDec)
It's also possible to call a function just like you would call a Sub procedure. The following statements both call the same function:
   Call Year(Now)
   Year Now
When you call a function this way, Visual Basic throws away the return value.

Visual Basic provides 3 types of code modules (files with code)

If a procedure call is not qualified, the program first searches for the procedure within the module making the call, then it searches the public procedures in the available standard modules.

To call a procedure in a separate form module, a form pointer must be added. For instance, a procedure named SomeSub in a form module called Form1 can be called by

   Call Form1.SomeSub(arguments)
Calling a class procedure is similar
   Dim DemoClass as New Class1
Notice that the class name (Class1) can not be used for this call.

When 2 or more modules contain public procedures with the same name, prepend the module name to the procedure name in order to reference a specific instance for the procedure, otherwise the procedure in the calling module is referenced.

   procedureName              ' References the procedure 
                              '   in the calling module
   moduleName.procedureName   ' Explicit reference


Procedure arguments are
   [Optional] [ByVal | ByRef] localName [as type] [= defaultvalue]
   Optional y As Integer = 12345
   ParamArray intNums()

Starting with VB 4.0, Named Arguments can be used. For instance, instead of

  Function testFunc(str1 as String, str2 as String) as String

  testFunc "String for Str1", "String for str2" 
You could use
  testFunc str2:="String for str2", str1:="String for Str1" 
This allows the parameters to appear in any order.

If the colon is omitted, str1="test" will compare 2 strings and return a Boolean (true or false).


Delphi is not case-sensitive.
By default, most parameters are passed by value. Use const and var to be explicit.

For Delphi 3, search the help for Procedures and Functions to get a good function overview. (Not in delphi 5)

Procedure RotatePal(p, q, r : Byte; var Up : Boolean);

   i : Byte;
   y : Byte;



Procedure Creds;          // No parameters


function Example_Function(const A: Variant): Double;
  I: Integer;
  Example_Function := 5;  // Returns the value
  Result := 0;            // The key word Result can be used
                          //   instead of the function name
  Result := Result + 5;   // Result can appear on both sides 
                          //   of the equals sign. On the right side,
                          //   it refers to the current return value.
  if a=b then exit;       // exit leaves the function or procedure

  The start of the program goes here
  after all the other procedures are defined
  The final end ends with a period

Calling a procedure



It is possible to have a variable point to a function or procedure. In the Delphi 5 help, see Procedural types: Overview for a pretty good explanation.
  TProcedure  = procedure (r : Byte);
  TCProcedure = procedure (r : Byte) of object; // Procedure in a class
  Procedure YY (r : Byte); // This is normally in another class
  property Onxx: TProcedure read fONxx write fOnxx;
  procedure MMJOY1BUTTONDOWN (var LocMessage: TMMJoyStick);
                     message MM_JOY1BUTTONDOWN;

  fOnxx : TProcedure ;  // Onxx is a variable which points to a procedure

  Onxx := YY; // Stores the address of YY in Onxx
              // This is normally done by setting a published event
              // at design time. Explicit code is needed when
              // testing components.

procedure TControl.xx(r : Byte);
    // Verify that a function is assigned before calling it
  if Assigned(fOnxx) then fOnxx(r);

C++ Builder

There is no distinction between procedures and functions except that procedures return void.
void swap( int *num1, int *num2 ); // 2 parameters, no return value

long GetTickCount();          // A function requiring no arguments
long GetTickCount( void );    // The same as above

long x_Times_y(long x, long y)
  long z = x*y;
  return z;                   // Returns the result

long Mult_x_y(long x, long y)
  return x*y;                 // Returns the result, 
                              // Optional if function returns void, but omit value

void Name(int &num1, int *num2, int num3, const int& num4);
     // num1 is by reference, the original value is modified by num1=4;
     // num2 is the address of the original, 
     //        the original is modified by *num2=4;
     // num3 is by value, the original can not be modified
     // num4 is by reference, but the original can not be modified

void Name(int n=5);    // If this is called without a parameter, 
                       //   then 5 will be used

BOOL Name(void);       // Requires no parameters
b = Name;              // Does not call the function, b=Function address (?)
b = Nane();            // This calls the function
A function must be defined before it can be called. This is normally done with prototypes. If more than one module calls a function, then the prototype is normally placed in a header file (*.h). Notice that prototypes end in a semicolon, and that definitions use - { ... }

Overloading is where several functions all have the same name but the number and/or types of the parameters are different. When the code is compiled, the function names are mangled (modified) to reflect the parameter types. Normally, mangling is transparent to the programmer. However, when calling MS Windows functions, mangling must be disabled.


C vs C++

C++ function names are mangled (modified) in order to indicate the number and types of parameters. C function names are exported just as you type them.

In order to export non-mangled function names from C++, precede each function with extern "C".

MS Windows API calls require non-mangled function names.

C does not allow parameters to be passed by reference, but the address can be passed which is almost the same thing.

Author: Robert Clemenzi -
URL: http:// / user / clemenzi / technical / Languages / Functions_Subs.htm