Data update
This commit is contained in:
parent
4bb20c9b71
commit
cbaf4c4b64
12390 changed files with 318560 additions and 27248 deletions
1
Task/Call-a-function/Ada/call-a-function-1.adb
Normal file
1
Task/Call-a-function/Ada/call-a-function-1.adb
Normal file
|
|
@ -0,0 +1 @@
|
|||
S: String := Ada.Text_IO.Get_Line;
|
||||
5
Task/Call-a-function/Ada/call-a-function-2.adb
Normal file
5
Task/Call-a-function/Ada/call-a-function-2.adb
Normal file
|
|
@ -0,0 +1,5 @@
|
|||
function F(X: Integer; Y: Integer := 0) return Integer; -- Y is optional
|
||||
...
|
||||
A : Integer := F(12);
|
||||
B : Integer := F(12, 0); -- the same as A
|
||||
C : Integer := F(12, 1); -- something different
|
||||
12
Task/Call-a-function/Ada/call-a-function-3.adb
Normal file
12
Task/Call-a-function/Ada/call-a-function-3.adb
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
type Integer_Array is array (Positive range <>) of Integer;
|
||||
function Sum(A: Integer_Array) return Integer is
|
||||
S: Integer := 0;
|
||||
begin
|
||||
for I in A'Range loop
|
||||
S := S + A(I);
|
||||
end loop;
|
||||
return S;
|
||||
end Sum;
|
||||
...
|
||||
A := Sum((1,2,3)); -- A = 6
|
||||
B := Sum((1,2,3,4)); -- B = 10
|
||||
9
Task/Call-a-function/Ada/call-a-function-4.adb
Normal file
9
Task/Call-a-function/Ada/call-a-function-4.adb
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
function H (Int: Integer;
|
||||
Fun: not null access function (X: Integer; Y: Integer)
|
||||
return Integer);
|
||||
return Integer;
|
||||
|
||||
...
|
||||
|
||||
X := H(A, F'Access) -- assuming X and A are Integers, and F is a function
|
||||
-- taking two Integers and returning an Integer.
|
||||
3
Task/Call-a-function/Ada/call-a-function-5.adb
Normal file
3
Task/Call-a-function/Ada/call-a-function-5.adb
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
Positional := H(A, F'Access);
|
||||
Named := H(Int => A, Fun => F'Access);
|
||||
Mixed := H(A, Fun=>F'Access);
|
||||
65
Task/Call-a-function/COBOL/call-a-function.cob
Normal file
65
Task/Call-a-function/COBOL/call-a-function.cob
Normal file
|
|
@ -0,0 +1,65 @@
|
|||
CALL "No-Arguments"
|
||||
|
||||
*> Fixed number of arguments.
|
||||
CALL "2-Arguments" USING Foo Bar
|
||||
|
||||
CALL "Optional-Arguments" USING Foo
|
||||
CALL "Optional-Arguments" USING Foo Bar
|
||||
*> If an optional argument is omitted and replaced with OMITTED, any following
|
||||
*> arguments can still be specified.
|
||||
CALL "Optional-Arguments" USING Foo OMITTED Bar
|
||||
*> Interestingly, even arguments not marked as optional can be omitted without
|
||||
*> a compiler warning. It is highly unlikely the function will still work,
|
||||
*> however.
|
||||
CALL "2-Arguments" USING Foo
|
||||
|
||||
*> COBOL does not support a variable number of arguments, or named arguments.
|
||||
|
||||
*> Values to return can be put in either one of the arguments or, in OpenCOBOL,
|
||||
*> the RETURN-CODE register.
|
||||
*> A standard function call cannot be done in another statement.
|
||||
CALL "Some-Func" USING Foo
|
||||
MOVE Return-Code TO Bar
|
||||
|
||||
*> Intrinsic functions can be used in any place a literal value may go (i.e. in
|
||||
*> statements) and are optionally preceded by FUNCTION.
|
||||
*> Intrinsic functions that do not take arguments may optionally have a pair of
|
||||
*> empty parentheses.
|
||||
*> Intrinsic functions cannot be defined by the user.
|
||||
MOVE FUNCTION PI TO Bar
|
||||
MOVE FUNCTION MEDIAN(4, 5, 6) TO Bar
|
||||
|
||||
*> Built-in functions/subroutines typically have prefixes indicating which
|
||||
*> compiler originally incorporated it:
|
||||
*> - C$ - ACUCOBOL-GT
|
||||
*> - CBL_ - Micro Focus
|
||||
*> - CBL_OC_ - OpenCOBOL
|
||||
*> Note: The user could name their functions similarly if they wanted to.
|
||||
CALL "C$MAKEDIR" USING Foo
|
||||
CALL "CBL_CREATE_DIR" USING Foo
|
||||
CALL "CBL_OC_NANOSLEEP" USING Bar
|
||||
*> Although some built-in functions identified by numbers.
|
||||
CALL X"F4" USING Foo Bar
|
||||
|
||||
*> Parameters can be passed in 3 different ways:
|
||||
*> - BY REFERENCE - this is the default way in OpenCOBOL and this clause may
|
||||
*> be omitted. The address of the argument is passed to the function.
|
||||
*> The function is allowed to modify the variable.
|
||||
*> - BY CONTENT - a copy is made and the function is passed the address
|
||||
*> of the copy, which it can then modify. This is recomended when
|
||||
*> passing a literal to a function.
|
||||
*> - BY VALUE - the function is passed the address of the argument (like a
|
||||
*> pointer). This is mostly used to provide compatibility with other
|
||||
*> languages, such as C.
|
||||
CALL "Modify-Arg" USING BY REFERENCE Foo *> Foo is modified.
|
||||
CALL "Modify-Arg" USING BY CONTENT Foo *> Foo is unchanged.
|
||||
CALL "C-Func" USING BY VALUE Bar
|
||||
|
||||
*> Partial application is impossible as COBOL does not support first-class
|
||||
*> functions.
|
||||
*> However, as functions are called using a string of their PROGRAM-ID,
|
||||
*> you could pass a 'function' as an argument to another function, or store
|
||||
*> it in a variable, or get it at runtime.
|
||||
ACCEPT Foo *> Get a PROGRAM-ID from the user.
|
||||
CALL "Use-Func" USING Foo
|
||||
CALL Foo USING Bar
|
||||
|
|
@ -1 +1 @@
|
|||
val fibonacci = fn x:if x < 2 { x } else { fn((x - 1)) + fn((x - 2)) }
|
||||
val fibonacci = fn(x) { if x < 2 { x } else { fn((x - 1)) + fn((x - 2)) } }
|
||||
|
|
|
|||
77
Task/Call-a-function/REXX/call-a-function.rexx
Normal file
77
Task/Call-a-function/REXX/call-a-function.rexx
Normal file
|
|
@ -0,0 +1,77 @@
|
|||
/* REXX ***************************************************************
|
||||
* 29.07.2013 Walter Pachl trying to address the task concisely
|
||||
***********************************************************************
|
||||
* f1 Calling a function that requires no arguments
|
||||
* f2 Calling a function with a fixed number of arguments
|
||||
* f3 Calling a function with optional arguments
|
||||
* f4 Calling a function with a variable number of arguments
|
||||
* f5 Calling a function with named arguments
|
||||
* f6 Using a function in statement context
|
||||
* f7 Using a function within an expression
|
||||
* f8 Obtaining the return value of a function
|
||||
* f8(...) is replaced by the returned value
|
||||
* call f8 ... returned value is in special vatiable RESULT
|
||||
* f9 Distinguishing built-in functions and user-defined functions
|
||||
* bif is enforced by using its name quoted in uppercase
|
||||
* fa,fb Distinguishing subroutines and functions
|
||||
* Stating whether arguments are passed by value or by reference
|
||||
* Arguments are passed by value
|
||||
* ooRexx supports passing by reference (Use Arg instruction)
|
||||
* Is partial application possible and how
|
||||
* no ideas
|
||||
**********************************************************************/
|
||||
say f1()
|
||||
Say f2(1,2,3)
|
||||
say f2(1,2,3,4)
|
||||
say f3(1,,,4)
|
||||
Say f4(1,2)
|
||||
Say f4(1,2,3)
|
||||
a=4700; b=11;
|
||||
Say f5('A','B')
|
||||
f6() /* returned value is used as command */
|
||||
x=f7()**2
|
||||
call f8 1,2; Say result '=' f8(1,2)
|
||||
f9: Say 'DATE'('S') date()
|
||||
call fa 11,22; Say result '=' fa(1,,
|
||||
2) /* the second comma above is for line continuation */
|
||||
Signal On Syntax
|
||||
Call fb 1,2
|
||||
x=fb(1,2)
|
||||
Exit
|
||||
f1: Return 'f1 doesn''t need an argument'
|
||||
f2: If arg()=3 Then
|
||||
Return 'f2: Sum of 3 arguments:' arg(1)+arg(2)+arg(3)
|
||||
Else
|
||||
Return 'f2: Invalid invocation:' arg() 'arguments. Needed: 3'
|
||||
f3: sum=0
|
||||
do i=1 To arg()
|
||||
If arg(i,'E')=0 Then Say 'f3: Argument' i 'omitted'
|
||||
Else sum=sum+arg(i)
|
||||
End
|
||||
Return 'f3 sum=' sum
|
||||
f4: sum=0; Do i=1 To arg(); sum=sum+arg(i); End
|
||||
Return 'f4: Sum of' arg() 'arguments is' sum
|
||||
f5: Parse Arg p1,p2
|
||||
Say 'f5: Argument 1 ('p1') contains' value(p1)
|
||||
Say 'f5: Argument 2 ('p2') contains' value(p2)
|
||||
Return 'f5: sum='value(p1)+value(p2)
|
||||
f6: Say 'f6: dir ft.rex'
|
||||
Return 'dir ft.rex'
|
||||
f7: Say 'f7 returns 7'
|
||||
Return 7
|
||||
f8: Say 'f8 returns arg(1)+arg(2)'
|
||||
Return arg(1)+arg(2)
|
||||
date: Say 'date is my date function'
|
||||
Return translate('ef/gh/abcd','DATE'('S'),'abcdefgh')
|
||||
fa: Say 'fa returns arg(1)+arg(2)'
|
||||
Return arg(1)+arg(2)
|
||||
fb: Say 'fb:' arg(1)','arg(2)
|
||||
Return
|
||||
|
||||
Syntax:
|
||||
Say 'Syntax raised in line' sigl
|
||||
Say sourceline(sigl)
|
||||
Say 'rc='rc '('errortext(rc)')'
|
||||
If sigl=39 Then
|
||||
Say 'fb cannot be invoked as function (it does not return a value'
|
||||
Exit
|
||||
38
Task/Call-a-function/Red/call-a-function.red
Normal file
38
Task/Call-a-function/Red/call-a-function.red
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
;- function without agument
|
||||
;- first example like a subroutine call
|
||||
hello: does [print "hello"]
|
||||
;- calling
|
||||
hello
|
||||
;- second return a value
|
||||
phi: func [][return to-float ((1.0 + sqrt(5.0)) / 2.0)]
|
||||
;-calling print phi
|
||||
print phi
|
||||
;-function with optional arguments
|
||||
optional: func [arg1 /opt1 /opt2][
|
||||
print arg1
|
||||
if opt1 [print "option1"]
|
||||
if opt2 [print "option2"]
|
||||
]
|
||||
;- calling
|
||||
optional 10
|
||||
optional/opt1 10
|
||||
optional/opt2 10
|
||||
optional/opt1/opt2 10
|
||||
;function with variable number of arguments
|
||||
;we can use block type to do it
|
||||
blocksum: func [b][r: 0 foreach n b [r: r + n] r]
|
||||
; calling
|
||||
print blocksum [1]
|
||||
print blocksum [1 2 3 4 5 6 7 8 9]
|
||||
;- first class function
|
||||
;- creating function from à function at run time
|
||||
square: func[n][return (n * n)]
|
||||
cube: func[n][ return (n * ( square n))]
|
||||
;-calling
|
||||
print cube 3
|
||||
;- use function as argument of another function
|
||||
print cube square 3
|
||||
;- store functions in collections
|
||||
myfuncs: [square cube]
|
||||
;-calling
|
||||
foreach f myfuncs [print reduce [f 2]]
|
||||
18
Task/Call-a-function/Uiua/call-a-function.uiua
Normal file
18
Task/Call-a-function/Uiua/call-a-function.uiua
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Generally functions have a fixed number of un-named arguments.
|
||||
JustOne ← (1) # Nonadic. Returns a 1.
|
||||
PlusOne ← +1 # Monadic. Adds 1 to its argument.
|
||||
MeanOne ← ÷ 2 + # Dyadic. Mean of its two arguments.
|
||||
# Triadic = ++, Tetradic = +++, Pentadic = ++++, etc
|
||||
|
||||
# Values are passed and returned on the stack.
|
||||
JustOne # -> 1 on the stack
|
||||
PlusOne # consumes the 1, adds 1, leaves the 2 on the stack.
|
||||
MeanOne 3 # MeanOne sees 3 and 2 on the stack and returns 2.5
|
||||
|
||||
# Some functions can take an optional suffix.
|
||||
⁅1.23456 # ->1
|
||||
⁅₂1.23456 # -> 1.23
|
||||
|
||||
# Some operators can take function packs, leading to more flexibility.
|
||||
# E.g. '⊃ fork' applies all the functions to its arguments.
|
||||
⊃(¯|+|-|÷|×|$"_ _ _"1) 2 3 # -> ¯2 5 1 1.5 6 "1 2 3"
|
||||
Loading…
Add table
Add a link
Reference in a new issue