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.




 
component call_a_function
  export Executable
  (* Declaring test functions that allow the various ways to call functions in Fortress to be demonstrated. *)
  addition(i:ZZ32, j:ZZ32): ZZ32 = i+j
  addition(i:ZZ32): ZZ32 = i+1
 
  (* Strings are concatenated by using a space as an infix operator. *)
  addition(i:String, j:String): String = i j
 
  printAString(s:String): () = println(s)
 
  (* Functions can be passed to other functions as arguments. When passing a function as an argument, the argument's type should be 
  represented as follows: "typeOfArgument(s)->returnType," which, in this case, is "String->()." You could also technically use the 
  "Any" type, but that isn't type-safe. *)
  printAString(s:String, f:String->()) = f(s)
 
  (* Defined functions can then be called as follows. *)
  var x:ZZ32 = addition(1, 2)
  var str:String = addition("This is ", "another string.")
 
  run() = do
    (* You can call built-in functions the same way that you call functions that you define. *)
    println("x at start: " x)
 
    x := addition(x, 2)
 
    println("x at middle: " x)
 
    printAString("This " "is " "a " "string.")
    printAString(str)
    printAString("\nThis is a string that is being printed by a function of the same name \nthat takes a function as an argument.\n",
      printAString)
 
    x := addition(4)
 
    println("x at end: " x)
  end
end
 