RosettaCodeData/Task/Call-a-function/Fortress/call-a-function.fortress

39 lines
1.3 KiB
Plaintext

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