[Private|Public| Friend] [Static]Sub procedurename (arguments) statements [Exit Sub] statements End Sub Event procedures are subroutines with special names ControlName & Underscore & EventName such as UIMenu_Edit_Copy_Click except for forms. The click procedure for My_UIForm is called Form_Click or MDIForm_Click depending on its type. [Private|Public|Friend][Static]Function functionName [(arguments)] [As type] statements [functionName = returnValue ' This is required before an Exit Exit Function] functionName = returnValue ' This is required End FunctionIf 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 NowWhen you call a function this way, Visual Basic throws away the return value.
Visual Basic provides 3 types of code modules (files with code)
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 DemoClass.SomeSubNotice 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
[Optional] [ByVal | ByRef] localName [as type] [= defaultvalue] Optional y As Integer = 12345 ParamArray intNums()
For Each x In intNums y = y + x Next x
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).
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); Var i : Byte; x, y : Byte; Begin End; Procedure Creds; // No parameters Begin End; function Example_Function(const A: Variant): Double; var I: Integer; begin 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 end; Begin The start of the program goes here after all the other procedures are defined The final end ends with a period End. Calling a procedure SetPal(TPal);
More
type 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; var 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); begin // Verify that a function is assigned before calling it if Assigned(fOnxx) then fOnxx(r); end;
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 BOOL b; b = Name; // Does not call the function, b=Function address (?) b = Nane(); // This calls the functionA 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.