Some languages provide the facility to declare functions and subroutines through the use of function prototyping.



Task

Demonstrate the methods available for declaring prototypes within the language. The provided solutions should include:

 An explanation of any placement restrictions for prototype declarations
 A prototype declaration for a function that does not require arguments
 A prototype declaration for a function that requires two arguments
 A prototype declaration for a function that utilizes varargs
 A prototype declaration for a function that utilizes optional arguments
 A prototype declaration for a function that utilizes named parameters
 Example of prototype declarations for subroutines or procedures (if these differ from functions)
 An explanation and example of any special forms of prototyping not covered by the above


Languages that do not provide function prototyping facilities should be omitted from this task.




' FB 1.05.0 Win64
 
' The position regarding prototypes is broadly similar to that of the C language in that functions, 
' sub-routines or operators (unless they have already been fully defined) must be declared before they can be used. 
' This is usually done near the top of a file or in a separate header file which is then 'included'.
 
' Parameter names are optional in declarations. When calling functions, using parameter names 
' (as opposed to identifying arguments by position) is not supported.
 
Type MyType ' needed for operator declaration
  i As Integer
End Type
 
Declare Function noArgs() As Integer  ' function with no argument that returns an integer 
Declare Function twoArgs(As Integer, As Integer) As Integer ' function with two arguments that returns an integer 
Declare Function atLeastOneArg CDecl(As Integer, ...) As Integer ' one mandatory integer argument followed by varargs
Declare Function optionalArg(As Integer = 0) As Integer ' function with a (single) optional argument with default value 
Declare Sub noArgs2() ' sub-routine with no argument
Declare Operator + (As MyType, As MyType) As MyType ' operator declaration (no hidden 'This' parameter for MyType)
 
 
' FreeBASIC also supports object-oriented programming and here all constructors, destructors, 
' methods (function or sub), properties and operators (having a hidden 'This' parameter) must be 
' declared within a user defined type and then defined afterwards.
 
 
Type MyType2
  Public:
    Declare Constructor(As Integer)
    Declare Destructor()
    Declare Sub MySub()   
    Declare Function MyFunction(As Integer) As Integer 
    Declare Property MyProperty As Integer
    Declare Operator Cast() As String
  Private:
    i As Integer
End Type