September 2017 Update
This commit is contained in:
parent
bba7bfd280
commit
ba8067c3b7
14570 changed files with 153136 additions and 63871 deletions
3
Task/Call-a-function/360-Assembly/call-a-function-1.360
Normal file
3
Task/Call-a-function/360-Assembly/call-a-function-1.360
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
X DS F
|
||||
Y DS F
|
||||
Z DS F
|
||||
7
Task/Call-a-function/360-Assembly/call-a-function-2.360
Normal file
7
Task/Call-a-function/360-Assembly/call-a-function-2.360
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
L R15,=V(MULTPLIC)
|
||||
LA R1,PARMLIST address of the paramter list
|
||||
BALR R14,R15 branch and link
|
||||
ST R0,Z Z=MULTPLIC(X,Y)
|
||||
* ...
|
||||
PARMLIST DC A(X)
|
||||
DC A(Y)
|
||||
2
Task/Call-a-function/360-Assembly/call-a-function-3.360
Normal file
2
Task/Call-a-function/360-Assembly/call-a-function-3.360
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
CALL MULTPLIC,(X,Y) call MULTPLIC(X,Y)
|
||||
ST R0,Z Z=MULTPLIC(X,Y)
|
||||
4
Task/Call-a-function/360-Assembly/call-a-function-4.360
Normal file
4
Task/Call-a-function/360-Assembly/call-a-function-4.360
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
LOAD EP=MULTPLIC load load-module
|
||||
LR R15,R0 retrieve entry address
|
||||
CALL (R15),(X,Y) call MULTPLIC(X,Y)
|
||||
ST R0,Z Z=MULTPLIC(X,Y)
|
||||
|
|
@ -1,21 +1,31 @@
|
|||
# A function without arguments: #
|
||||
f();
|
||||
# or #
|
||||
# Note functions and subroutines are called procedures (or PROCs) in Algol 68 #
|
||||
# A function called without arguments: #
|
||||
f;
|
||||
|
||||
# Algol 68 does not expect an empty parameter list for calls with no arguments, "f()" is a syntax error #
|
||||
# A function with a fixed number of arguments: #
|
||||
f(1, x);
|
||||
|
||||
# ALGOL 68 does not support optional arguments, variable numbers of arguments, or named
|
||||
arguments.
|
||||
However, some functions may take an argument "0" as an instruction to use the default value
|
||||
(sort of a pseudo-optional argument). #
|
||||
# variable number of arguments: #
|
||||
# functions that accept an array as a parameter can effectively provide variable numbers of arguments #
|
||||
# a "literal array" (called a row-display in Algol 68) can be passed, as is often the case for the I/O #
|
||||
# functions - e.g.: #
|
||||
print( ( "the result is: ", r, " after ", n, " iterations", newline ) );
|
||||
# the outer brackets indicate the parameters of print, the inner brackets indicates the contents are a "literal array" #
|
||||
|
||||
# ALGOL 68 does not support optional arguments, though in some cases an empty array could be passed to a function #
|
||||
# expecting an array, e.g.: #
|
||||
f( () );
|
||||
|
||||
# named arguments - see the Algol 68 sample in: http://rosettacode.org/wiki/Named_parameters #
|
||||
|
||||
# In "Talk:Call a function" a statement context is explained as
|
||||
"The function is used as an instruction (with a void context),
|
||||
rather than used within an expression." Based on that,
|
||||
both ALGOL examples above are already in a statement context.
|
||||
For full ALGOL compatibility, though, they should be in the form "VOID (f ());" #
|
||||
rather than used within an expression."
|
||||
Based on that, the examples above are already in a statement context.
|
||||
Technically, when a function that returns other than VOID (i.e. is not a subroutine)
|
||||
is called in a statement context, the result of the call is "voided" i.e. discarded.
|
||||
If desired, this can be made explicit using a cast, e.g.: #
|
||||
VOID(f);
|
||||
|
||||
# A function's return value being used: #
|
||||
x := f(y);
|
||||
|
|
@ -26,3 +36,4 @@ x := f(y);
|
|||
|
||||
# If the function is declared with argument(s) of mode REF MODE,
|
||||
then those arguments are being passed by reference. #
|
||||
# Technically, all parameters are passed by value, however the value of a REF MODE is a reference... #
|
||||
|
|
|
|||
38
Task/Call-a-function/ALGOL-W/call-a-function.alg
Normal file
38
Task/Call-a-function/ALGOL-W/call-a-function.alg
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
% Note, in Algol W, functions are called procedures %
|
||||
% calling a function with no parameters: %
|
||||
f;
|
||||
|
||||
% calling a function with a fixed number of parameters %
|
||||
g( 1, 2.3, "4" );
|
||||
|
||||
% Algol W does not support optional parameters in general, however constructors for records can %
|
||||
% be called wither with parameters (one for each field in the record) or no parameters #
|
||||
|
||||
% Algol W does not support variable numbers of parameters, except for the built-in I/O functions #
|
||||
% Algol W does not support named arguments %
|
||||
|
||||
% A function can be used in a statement context by calling it, as in the examples above %
|
||||
|
||||
% First class context: A function can be passed as a parameter to another procedure, e.g.: %
|
||||
v := integrate( sin, 0, 1 )
|
||||
% assuming a suitable definition of integrate %
|
||||
% Algol W does not support functions returning functions %
|
||||
|
||||
% obtaining the return value of a function: e.g.: %
|
||||
v := g( x, y, z );
|
||||
|
||||
% There is no syntactic distinction between user-defined and built-in functions %
|
||||
|
||||
% Subroutines and functions are both procedures, a subroutine is a procedure with no return type %
|
||||
% (called a proper procedure in Algol W) %
|
||||
% There is no syntactic distinction between a call to a function and a call to a subroutine %
|
||||
% other than the context %
|
||||
|
||||
% In Algol W, parameters are passed by value, result or value result. This must be stated in the %
|
||||
% definition of the function/subroutine. Value parameters are passed by value, result and value result %
|
||||
% are effectively passed by reference and assigned on function exit. Result parameters are "out" parameters %
|
||||
% and value result parameters are "in out". %
|
||||
% Algol W also has "name" parameters (not to be confused with named parameters). Functions with name %
|
||||
% parameters are somewhat like macros %
|
||||
|
||||
% Partial application is not possible in Algol W %
|
||||
30
Task/Call-a-function/C-sharp/call-a-function.cs
Normal file
30
Task/Call-a-function/C-sharp/call-a-function.cs
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
/* a function that has no argument */
|
||||
public int MyFunction();
|
||||
|
||||
/* a function with a fixed number of arguments */
|
||||
FunctionWithArguments(4, 3, 2);
|
||||
|
||||
/* a function with optional arguments */
|
||||
public void OptArg();
|
||||
|
||||
public static void Main()
|
||||
{
|
||||
OptArg(1);
|
||||
OptArg(1, 2);
|
||||
OptArg(1, 2, 3);
|
||||
}
|
||||
public void ExampleMethod(int required,
|
||||
string optionalstr = "default string",
|
||||
int optionalint = 10)
|
||||
/* If you know the first and the last parameter */
|
||||
ExampleMethod(3, optionalint: 4);
|
||||
|
||||
/* If you know all the parameter */
|
||||
ExampleMethod(3, "Hello World", 4);
|
||||
|
||||
/* Variable number of arguments use array */
|
||||
public static void UseVariableParameters(params int[] list)
|
||||
|
||||
/* Obtain return value from function */
|
||||
public internal MyFunction();
|
||||
int returnValue = MyFunction();
|
||||
25
Task/Call-a-function/Gambas/call-a-function.gambas
Normal file
25
Task/Call-a-function/Gambas/call-a-function.gambas
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
Public Sub Main()
|
||||
|
||||
Hello
|
||||
Print CopyIt("Hello ", 6)
|
||||
Print CopyIt("Hello ", 3, "!!")
|
||||
|
||||
End
|
||||
'_____________________________________________________________________________________
|
||||
Public Sub CopyIt(sString As String, siNo As Short, Optional sEnd As String) As String
|
||||
Dim siCount As Short
|
||||
Dim sNewString As String
|
||||
|
||||
For siCount = 1 To siNo
|
||||
sNewString &= sString
|
||||
Next
|
||||
|
||||
Return Trim(sNewString) & sEnd
|
||||
|
||||
End
|
||||
'_____________________________________________________________________________________
|
||||
Public Sub Hello()
|
||||
|
||||
Print "Hello world!"
|
||||
|
||||
End
|
||||
33
Task/Call-a-function/Kotlin/call-a-function.kotlin
Normal file
33
Task/Call-a-function/Kotlin/call-a-function.kotlin
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
// version 1.0.6
|
||||
|
||||
fun fun1() = println("No arguments")
|
||||
|
||||
fun fun2(i: Int) = println("One argument = $i")
|
||||
|
||||
fun fun3(i: Int, j: Int = 0) = println("One required argument = $i, one optional argument = $j")
|
||||
|
||||
fun fun4(vararg v: Int) = println("Variable number of arguments = ${v.asList()}")
|
||||
|
||||
fun fun5(i: Int) = i * i
|
||||
|
||||
fun fun6(i: Int, f: (Int) -> Int) = f(i)
|
||||
|
||||
fun fun7(i: Int): Double = i / 2.0
|
||||
|
||||
fun fun8(x: String) = { y: String -> x + " " + y }
|
||||
|
||||
fun main(args: Array<String>) {
|
||||
fun1() // no arguments
|
||||
fun2(2) // fixed number of arguments, one here
|
||||
fun3(3) // optional argument, default value used here
|
||||
fun4(4, 5, 6) // variable number of arguments
|
||||
fun3(j = 8, i = 7) // using named arguments, order unimportant
|
||||
val b = false
|
||||
if (b) fun1() else fun2(9) // statement context
|
||||
println(1 + fun6(4, ::fun5) + 3) // first class context within an expression
|
||||
println(fun5(5)) // obtaining return value
|
||||
println(Math.round(2.5)) // no distinction between built-in and user-defined functions, though former usually have a receiver
|
||||
fun1() // calling sub-routine which has a Unit return type by default
|
||||
println(fun7(11)) // calling function with a return type of Double (here explicit but can be implicit)
|
||||
println(fun8("Hello")("world")) // partial application isn't supported though you can do this
|
||||
}
|
||||
119
Task/Call-a-function/Ol/call-a-function.ol
Normal file
119
Task/Call-a-function/Ol/call-a-function.ol
Normal file
|
|
@ -0,0 +1,119 @@
|
|||
; note: sign "==>" indicates expected output
|
||||
|
||||
;;; Calling a function that requires no arguments
|
||||
(define (no-args-function)
|
||||
(print "ok."))
|
||||
)
|
||||
|
||||
(no-args-function)
|
||||
; ==> ok.
|
||||
|
||||
|
||||
;;; Calling a function with a fixed number of arguments
|
||||
(define (two-args-function a b)
|
||||
(print "a: " a)
|
||||
(print "b: " b))
|
||||
)
|
||||
|
||||
(two-args-function 8 13)
|
||||
; ==> a: 8
|
||||
; ==> b: 13
|
||||
|
||||
|
||||
;;; Calling a function with optional arguments
|
||||
(define (optional-args-function a . args)
|
||||
(print "a: " a)
|
||||
(if (null? args)
|
||||
(print "no optional arguments"))
|
||||
(if (less? 0 (length args))
|
||||
(print "b: " (car args)))
|
||||
(if (less? 1 (length args))
|
||||
(print "c: " (cadr args)))
|
||||
; etc.
|
||||
)
|
||||
|
||||
(optional-args-function 3)
|
||||
; ==> a: 3
|
||||
; ==> no optional arguments
|
||||
(optional-args-function 3 8)
|
||||
; ==> a: 3
|
||||
; ==> b: 8
|
||||
(optional-args-function 3 8 13)
|
||||
; ==> a: 3
|
||||
; ==> b: 8
|
||||
; ==> c: 13
|
||||
(optional-args-function 3 8 13 77)
|
||||
; ==> a: 3
|
||||
; ==> b: 8
|
||||
; ==> c: 13
|
||||
|
||||
|
||||
;;; Calling a function with a variable number of arguments
|
||||
; /same as optional arguments
|
||||
|
||||
|
||||
;;; Calling a function with named arguments
|
||||
; /no named arguments "from the box" provided, but it can be simulated using builtin maps (named "ff")
|
||||
(define (named-args-function args)
|
||||
(print "a: " (get args 'a 8))
|
||||
(print "b: " (get args 'b 13))
|
||||
)
|
||||
|
||||
(named-args-function #empty)
|
||||
; ==> a: 8
|
||||
; ==> b: 13
|
||||
(named-args-function (list->ff '((a . 3))))
|
||||
; ==> a: 3
|
||||
; ==> b: 13
|
||||
(named-args-function (list->ff '((b . 7))))
|
||||
; ==> a: 8
|
||||
; ==> b: 7
|
||||
(named-args-function (list->ff '((a . 3) (b . 7))))
|
||||
; ==> a: 3
|
||||
; ==> b: 7
|
||||
|
||||
|
||||
;;; Using a function in first-class context within an expression
|
||||
(define (first-class-arg-function arg a b)
|
||||
(print (arg a b))
|
||||
)
|
||||
|
||||
(first-class-arg-function + 2 3)
|
||||
; ==> 5
|
||||
(first-class-arg-function - 2 3)
|
||||
; ==> -1
|
||||
|
||||
|
||||
;;; Obtaining the return value of a function
|
||||
(define (return-value-function)
|
||||
(print "ok.")
|
||||
123)
|
||||
|
||||
(let ((result (return-value-function)))
|
||||
(print result))
|
||||
; ==> ok.
|
||||
; ==> 123
|
||||
; actually
|
||||
|
||||
|
||||
;;; Is partial application possible and how
|
||||
(define (make-partial-function n)
|
||||
(lambda (x y)
|
||||
(print (n x y)))
|
||||
)
|
||||
|
||||
(define plus (make-partial-function +))
|
||||
(plus 2 3)
|
||||
; ==> 5
|
||||
(define minus (make-partial-function -))
|
||||
(minus 2 3)
|
||||
; ==> -1
|
||||
|
||||
|
||||
; TBD:
|
||||
;;; 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
|
||||
10
Task/Call-a-function/Zkl/call-a-function-1.zkl
Normal file
10
Task/Call-a-function/Zkl/call-a-function-1.zkl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
f(); f(1,2,3,4);
|
||||
fcn f(a=1){}() // define and call f, which gets a set to 1
|
||||
fcn{vm.arglist}(1,2,3,4) // arglist is L(1,2,3,4)
|
||||
fcn{a1:=vm.nthArg(1)}(1,2,3) // a1 == 2
|
||||
(f() == True); (f() and 1 or 2)
|
||||
if (f()) println()
|
||||
f(f) // pass f to itself
|
||||
s:=f()
|
||||
fcn{}.isType(self.fcn) //True
|
||||
fcn{}.len.isType(self.fcn) //False, len is a Method
|
||||
10
Task/Call-a-function/Zkl/call-a-function-2.zkl
Normal file
10
Task/Call-a-function/Zkl/call-a-function-2.zkl
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
fcn(a,b,c).fp(1)() // call function with a always set to 1
|
||||
fcn(a,b,c).fp1(2,3)() // call function with b & c always set to 2 & 3
|
||||
fcn(a,b,c,d).fpN(3,5)() // call function with d always set to 5
|
||||
fcn{vm.arglist}.fpN(3,66)(1,2,3,4,5) //-->L(1,2,3,66,4,5)
|
||||
fcn{}.fpM("01-",5) // use a mask to select parameters
|
||||
// 1 is supplied, 0 is get upon call, - is chop arglist
|
||||
fcn{vm.arglist}.fpM("01-",66)(1,2,3,4) //-->L(1,66)
|
||||
|
||||
a:=5; f('wrap(b){a+b}) // 'wrap is syntactic sugar for .fpN
|
||||
// to create a lexical closure --> f(fcn(b,a){a+b}.fpN(1,a))
|
||||
Loading…
Add table
Add a link
Reference in a new issue