June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
38
Task/Call-a-function/Fortress/call-a-function.fortress
Normal file
38
Task/Call-a-function/Fortress/call-a-function.fortress
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
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
|
||||
Loading…
Add table
Add a link
Reference in a new issue