Task

Demonstrate the different syntax and semantics provided for calling a function.



This may include:

   Calling a function that requires no arguments
   Calling a function with a fixed number of arguments
   Calling a function with optional arguments
   Calling a function with a variable number of arguments
   Calling a function with named arguments
   Using a function in statement context
   Using a function in first-class context within an expression
   Obtaining the return value of a function
   Distinguishing built-in functions and user-defined functions
   Distinguishing subroutines and functions
   Stating whether arguments are passed by value or by reference
   Is partial application possible and how


This task is not about defining functions.




// Calling a function that requires no arguments
  ModuleA    ' introduce a namespace can have modules/functions/subs, anything inside, and threads.
  Call FunctionA() ' introduce a namespace can have modules/functions/subs, anything inside.
  Call LambdaA() ' introduce a namespace can have modules/functions/subs, anything inside.
  SubAlfa()  ' subroutine this has the same scope as the caller, we can use Local statement to shadow variables/arrays
  Print @Simple()  ' simple function this has the same scope as the caller, we can use Local statement to shadow variables/arrays
  Gosub labelA  ' Statement Return used to return from a gosub, no arguments and no local statement used, the code is like any other code in the module/function/lambda scope. No jump out of scope allowed.
  Gosub 10020   ' Statement Return used to return from a gosub,, no arguments and no local statement used, the code is like any other code in the module/function/lambda scope. No jump out of scope allowed.
// Calling a function with a fixed number of arguments
  ModuleA 100
  SubAlfa(100)  ' subroutine
  Print @Simple(100)  ' simple function
  Call FunctionA(100)
  Call LambdaA(100)
//  Calling a function with optional arguments
  ModuleA ?,4
  SubAlfa(?)  ' subroutine
  Print @Simple(?)  ' simple function
  Call FunctionA(,4)
  Call LambdaA(,4)
//  Calling a function with a variable number of arguments
  Call FunctionA(1,2,3,4,5)
  Call LambdaA(1,2,3,4,5)
//  Calling a function with named arguments
  ModuleA %X=10, %Z=20
//  Using a function in statement context
  Module A
  SubAlfa()
  Call Void FunctionA()
  Call Void LambdaA()
//  Using a function in first-class context within an expression
  Print (lambda (x,y)->{=x**y}(2,3))*8=64
//  Obtaining the return value of a function
  A%=FunctionA%()  ' integer part from any numeric type. Also A% if get decimals convert to integer using school rounding (0.5, so 3.5 is 4, 2.5 is 3)
  A=FunctionA()   ' numeric or object
  A$=FunctionA$() ' string or object which return string
  A=LambdaA()
  A$=LambdaA$()
  A=@SimpleA()
  A$=@SimpleA$()   
//  Distinguishing built-in functions and user-defined functions
  Def Cos(x)=100*X     ' change the build in
  Print Cos(3), @Cos(3)  ' @Cos() is the build in anyway
  ' we can't use simple function with same name as a bultin function.
//  Distinguishing subroutines and functions
  Name without parenthesis like a command or statement is a Module
  Name with parentesis without operator is a Sub
  Name with parenthesis in an expression is a function or array (array come first), using a(*10) we say that it is function even an array a() exist.
  Name with @ as first symbol and parenthesis like @alfa() is a simple function unless has a builtin name, so is that function.
//  Stating whether arguments are passed by value or by reference
    Default:By Value
    By Reference: Using & at both sides (caller and calee)
    Function Alfa(&x) {
	=X : X++
    }
    Print Alfa(&counter)
    Subs and Simple Functions not need a by reference pass because they are in the same scope from the caller, so actuall we pass by value to be local to those entities. But we can use by reference, except for static variables, and array items. Those can only passed by reference on modules and nornal functions and lambdas (which are normal functions plus more).

//  Is partial application possible and how
A=Lambda (X) -> {
	=Lambda X (Y) -> {
		=Y**X
	}
}
Cube=A(3) : Square=A(2)
Print Cube(2)=8, Square(2)=4

OOP: Functions/Modules can be members of objects.
Objects may have a name (they live until code get out of scope)
Objects may don't have a name but one or more pointers, (they live until counting references are zero)

  ObjectA.moduleA  is a call to an object method (a module)
  pointerA=>moduleA is a call to an object by a pointer

An object may return values, and may accept parameters too. Also objects if they didn't return values other than a copy of them can be use operators defined as object functions.

Objects may have events and fire them using Call Event "simpleevent", param1, param2...

Functions and lambda (not simple functions), and object functions can be passed by reference as parameters.



