Call a function: Difference between revisions

Added Fortress example.
(Add WDTE example.)
(Added Fortress example.)
Line 1,192:
TYPE(MIXED) LOTS(12000)</lang>
One might hope to try <code>IT = BCHOP(LOTS.NAME,"Fred")</code> where BCHOP is a well-tested function for performing a binary search that should run swiftly. Alas, no. The successive values of NAME are not contiguous while BCHOP expects to receive an array of values that are contiguous - that is, with a "stride" of one. So, the compiler inserts code to copy all the LOTS.NAME elements into such a work area and passes the location of that to BCHOP (which searches it swiftly), then on return, the work area is copied back to LOTS.NAME just in case there had been a change. This latter can be avoided if within BCHOP its array is given the attribute INTENT(IN) for read-only but the incoming copy still means an effort of order N, while for the search the effort is just Log(N). This can have a less-than-subtle effect if large arrays are involved.
 
=={{header|Fortress}}==
<lang fortress>
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
</lang>
 
=={{header|Gambas}}==