Call a function: Difference between revisions
Call a function in PureBasic |
Puppydrum64 (talk | contribs) |
||
Line 67:
CALL (R15),(X,Y) call MULTPLIC(X,Y)
ST R0,Z Z=MULTPLIC(X,Y)</lang>
=={{header|6502 Assembly}}==
To call a function, you use <code>JSR</code> followed by the pointer to its beginning. Most of the time this will be a labeled line of code that your assembler will convert to an actual memory address for you during the assembly process.
<lang 6502asm>JSR myFunction</lang>
Function arguments are often passed in through registers, the zero page, or the stack. Given how awkward it is to work with the stack on the 6502, it's best not to use the hardware stack as a means of parameter passing. CC65 uses a software stack in the upper half of zero page, which is indexed using the X register.
<lang 6502asm>sum:
;adds the values in zero page address $00 and $01, outputs to accumulator.
LDA $00 ;load the byte stored at memory address $0000
CLC
ADC $01 ;add the byte at memory address $0001
RTS ;return</lang>
The return value is usually stored in the accumulator if it will fit in 8 bits. If not, it's often stored in a dedicated section of the zero page. Since the 6502 has very few registers and all are 8-bit, it's common to set aside a few zero page memory addresses for holding 16-bit return values.
The closest thing 6502 has to "built-in functions" are the interrupt vectors whose pointers are stored at the very end of memory. They are, in order: Non-maskable interrupt (NMI), reset, and IRQ (Interrupt Request). They are no different than other functions except they end in <code>RTI</code> rather than <code>RTS</code>. With "bare-metal programming" like on the NES this is all you have, but most computers of the 80s had some sort of kernel or operating system that had pre-defined functions you could use simply by <code>JSR</code>ing their memory address. The actual memory locations of these, and what they did, varies by implementation.
=={{header|68000 Assembly}}==
To call a function, you use <code>JSR</code> followed by the pointer to its beginning. Most of the time this will be a labeled line of code that your assembler will convert to an actual memory address for you during the assembly process.
<lang 68000devpac>JSR myFunction</lang>
Function arguments are often passed in through the stack. When looking at the function in C or a similar language that compiles to 68000 Assembly, the arguments are pushed in the reverse order they are listed. Return values typically go into the D0 register if they're 32-bit or smaller. The CPU does not enforce this, so it's up to the programmer or compiler to use calling conventions to ensure compatibility between software.
=={{header|8086 Assembly}}==
|
Revision as of 23:58, 14 June 2022
![Task](http://static.miraheze.org/rosettacodewiki/thumb/b/ba/Rcode-button-task-crushed.png/64px-Rcode-button-task-crushed.png)
You are encouraged to solve this task according to the task description, using any language you may know.
- Task
Demonstrate the different syntax and semantics provided for calling a function.
This may include:
- Calling a function that requires no arguments
- Calling a function with a fixed number of arguments
- Calling a function with optional arguments
- Calling a function with a variable number of arguments
- Calling a function with named arguments
- 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
- Is partial application possible and how
This task is not about defining functions.
11l
<lang 11l>F no_args() {} // call no_args()
F fixed_args(x, y)
print(‘x=#., y=#.’.format(x, y))
// call fixed_args(1, 2) // x=1, y=2
// named arguments fixed_args(x' 1, y' 2)
F opt_args(x = 1)
print(x)
// calls opt_args() // 1 opt_args(3.141) // 3.141</lang>
360 Assembly
Due to assembler, argument are passed by reference.
With:
<lang 360asm>X DS F
Y DS F
Z DS F</lang>
If you do not want to use the CALL macro instruction and for a link-edited object-module:
<lang 360asm> 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)</lang>
If you call a link-edited object-module: <lang 360asm> CALL MULTPLIC,(X,Y) call MULTPLIC(X,Y)
ST R0,Z Z=MULTPLIC(X,Y)</lang>
If you call an load-module at execution time: <lang 360asm> 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)</lang>
6502 Assembly
To call a function, you use JSR
followed by the pointer to its beginning. Most of the time this will be a labeled line of code that your assembler will convert to an actual memory address for you during the assembly process.
<lang 6502asm>JSR myFunction</lang>
Function arguments are often passed in through registers, the zero page, or the stack. Given how awkward it is to work with the stack on the 6502, it's best not to use the hardware stack as a means of parameter passing. CC65 uses a software stack in the upper half of zero page, which is indexed using the X register. <lang 6502asm>sum:
- adds the values in zero page address $00 and $01, outputs to accumulator.
LDA $00 ;load the byte stored at memory address $0000 CLC ADC $01 ;add the byte at memory address $0001 RTS ;return</lang>
The return value is usually stored in the accumulator if it will fit in 8 bits. If not, it's often stored in a dedicated section of the zero page. Since the 6502 has very few registers and all are 8-bit, it's common to set aside a few zero page memory addresses for holding 16-bit return values.
The closest thing 6502 has to "built-in functions" are the interrupt vectors whose pointers are stored at the very end of memory. They are, in order: Non-maskable interrupt (NMI), reset, and IRQ (Interrupt Request). They are no different than other functions except they end in RTI
rather than RTS
. With "bare-metal programming" like on the NES this is all you have, but most computers of the 80s had some sort of kernel or operating system that had pre-defined functions you could use simply by JSR
ing their memory address. The actual memory locations of these, and what they did, varies by implementation.
68000 Assembly
To call a function, you use JSR
followed by the pointer to its beginning. Most of the time this will be a labeled line of code that your assembler will convert to an actual memory address for you during the assembly process.
<lang 68000devpac>JSR myFunction</lang>
Function arguments are often passed in through the stack. When looking at the function in C or a similar language that compiles to 68000 Assembly, the arguments are pushed in the reverse order they are listed. Return values typically go into the D0 register if they're 32-bit or smaller. The CPU does not enforce this, so it's up to the programmer or compiler to use calling conventions to ensure compatibility between software.
8086 Assembly
A function that requires no arguments is simply CALL
ed:
<lang asm>call foo</lang>
Functions with a fixed number of arguments have their arguments pushed onto the stack prior to the call. This is how C compilers generate 8086 assembly code. Assembly written by a person can use the stack or registers to pass arguments. Passing via registers is faster but more prone to errors and clobbering, which can cause other functions to not operate correctly.
<lang asm>push ax ;second argument push bx ;first argument - typically arguments are pushed in the reverse order they are listed. call foo pop bx pop ax
foo: push bp mov bp,sp
- now bp+4 = the value pushed from BX, and bp+6 = the value pushed from AX</lang>
The 8086 cannot support named arguments directly. However it is possible to label a section of RAM, and use that as the argument for a function.
<lang asm>foo: ld ax,word ptr[ds:bar] ;load from bar, which is a 16 bit storage location in the data segment (DS), into AX</lang>
Built-in functions are typically called using the INT
instruction. This instruction takes a numeric constant as its primary argument, and the value in AH
as a selector of sorts. This command is used to exit a program and return to MS-DOS:
<lang asm>mov AH,4Ch
mov AL,00h
int 21h</lang>
AArch64 Assembly
<lang AArch64 Assembly> /* ARM assembly AARCH64 Raspberry PI 3B */ /* program callfonct.s */
/*******************************************/ /* Constantes file */ /*******************************************/ /* for this file see task include a file in language AArch64 assembly*/ .include "../includeConstantesARM64.inc"
/***********************/ /* Initialized data */ /***********************/ .data szMessage: .asciz "Hello. \n" // message szRetourLigne: .asciz "\n" szMessResult: .asciz "Resultat : " // message result
/***********************/ /* No Initialized data */ /***********************/ .bss sZoneConv: .skip 100
.text .global main main:
ldr x0,=szMessage // adresse of message short program bl affichageMess // call function with 1 parameter (x0)
// call function with parameters in register mov x0,#5 mov x1,#10 bl fonction1 // call function with 2 parameters (x0,x1) ldr x1,qAdrsZoneConv // result in x0 bl conversion10S // call function with 2 parameter (x0,x1) ldr x0,=szMessResult bl affichageMess // call function with 1 parameter (x0) ldr x0,qAdrsZoneConv bl affichageMess ldr x0,qAdrszRetourLigne bl affichageMess // call function with parameters on stack mov x0,#5 mov x1,#10 stp x0,x1,[sp,-16]! // store registers on stack bl fonction2 // call function with 2 parameters on the stack // result in x0 ldr x1,qAdrsZoneConv bl conversion10S // call function with 2 parameter (x0,x1) ldr x0,=szMessResult bl affichageMess // call function with 1 parameter (x0) ldr x0,qAdrsZoneConv bl affichageMess ldr x0,qAdrszRetourLigne bl affichageMess // end of program mov x0, #0 // return code mov x8, #EXIT // request to exit program svc 0 // perform the system call
qAdrsZoneConv: .quad sZoneConv qAdrszRetourLigne: .quad szRetourLigne /******************************************************************/ /* call function parameter in register */ /******************************************************************/ /* x0 value one */ /* x1 value two */ /* return in x0 */ fonction1:
stp x2,lr,[sp,-16]! // save registers mov x2,#20 mul x0,x0,x2 add x0,x0,x1 ldp x2,lr,[sp],16 // restaur 2 registres ret // retour adresse lr x30
/******************************************************************/ /* call function parameter in the stack */ /******************************************************************/ /* return in x0 */ fonction2:
stp fp,lr,[sp,-16]! // save registers add fp,sp,#16 // address parameters in the stack stp x1,x2,[sp,-16]! // save others registers ldr x0,[fp] // second paraméter ldr x1,[fp,#8] // first parameter mov x2,#-20 mul x0,x0,x2 add x0,x0,x1 ldp x1,x2,[sp],16 // restaur 2 registres ldp fp,lr,[sp],16 // restaur 2 registres add sp,sp,#16 // very important, for stack aligned ret // retour adresse lr x30
/********************************************************/
/* File Include fonctions */
/********************************************************/
/* for this file see task include a file in language AArch64 assembly */
.include "../includeARM64.inc"
</lang>
- Output:
Hello. Resultat : +110 Resultat : -90
ActionScript
<lang actionscript> myfunction(); /* function with no arguments in statement context */
myfunction(6,b); // function with two arguments in statement context stringit("apples"); //function with a string argument</lang>
Ada
- Ada provides two kinds of subroutines: procedures without return values and functions with return values. The return values of functions must be used by the callers. If you don't want to deal with the return value, call a procedure instead.
- As a rule of thumb, an Ada compiler is free to pass arguments either by value or by reference. Parameters have a mode however: either 'in' or 'out' or 'in out'. It is prohibited to write anything to an 'in' parameter. The next language Standard, Ada 2012, will support functions with 'out' and 'in out' mode parameters, so far, only procedures could have parameters with non-'in' modes. So any of the following statements for Ada functions holds for Ada procedures as well.
- There are no differences between calling built-in vs. user defined functions.
- Functions without parameters can be called by omitting the parameter list (no empty brackets!):<lang Ada>S: String := Ada.Text_IO.Get_Line;</lang>
- Ada supports functions with optional parameters:<lang Ada>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</lang>
- If the number of parameters of F were fixed to two (by omitting the ":= 0" in the specification), then B and C would be OK, but A wouldn't.
- Ada does not support functions with a variable number of arguments. But a function argument can be an unconstrained array with as many values as you want:<lang Ada>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</lang>
- One can realize first-class functions by defining an access to a function as a parameter:<lang Ada>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.</lang>
- The caller is free to use either positional parameters or named parameters, or a mixture of both (with positional parameters first) <lang Ada>Positional := H(A, F'Access);
Named := H(Int => A, Fun => F'Access); Mixed := H(A, Fun=>F'Access); </lang>
ALGOL 68
<lang algol68># 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);
- 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, 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);
- There is no distinction between built-in functions and user-defined functions. #
- A subroutine is simply a function that returns VOID. #
- 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... #</lang>
See First-Class Functions for an example of first-class functions in ALGOL 68.
See Partial Function Application for an example of partial function application in ALGOL 68.
See Optional Parameters for an example of optional parameters in Algol 68.
See Named Parameters for an example of named parameters in Algol 68.
ALGOL W
<lang algolw>% 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 %</lang>
AntLang
AntLang provides two ways to apply a function. One way is infix application. <lang AntLang>2*2+9</lang> Infix application is right associative, so x f y g z means x f (y g z) and not (x f y) g z. You can break this rule using parenthesis. The other way is prefix application. <lang AntLang>*[2;+[2;9]] echo["Hello!"] time[]</lang>
ARM Assembly
<lang ARM Assembly>
/* ARM assembly Raspberry PI */ /* program callfonct.s */
/* Constantes */ .equ STDOUT, 1 .equ WRITE, 4 .equ EXIT, 1
/***********************/ /* Initialized data */ /***********************/ .data szMessage: .asciz "Hello. \n" @ message szRetourLigne: .asciz "\n" szMessResult: .ascii "Resultat : " @ message result sMessValeur: .fill 12, 1, ' '
.asciz "\n"
/***********************/ /* No Initialized data */ /***********************/ .bss iValeur: .skip 4 @ reserve 4 bytes in memory
.text .global main main:
ldr r0,=szMessage @ adresse of message short program bl affichageMess @ call function with 1 parameter (r0)
@ call function with parameters in register mov r0,#5 mov r1,#10 bl fonction1 @ call function with 2 parameters (r0,r1) ldr r1,=sMessValeur @ result in r0 bl conversion10S @ call function with 2 parameter (r0,r1) ldr r0,=szMessResult bl affichageMess @ call function with 1 parameter (r0)
@ call function with parameters on stack mov r0,#5 mov r1,#10 push {r0,r1} bl fonction2 @ call function with 2 parameters on the stack @ result in r0 ldr r1,=sMessValeur bl conversion10S @ call function with 2 parameter (r0,r1) ldr r0,=szMessResult bl affichageMess @ call function with 1 parameter (r0) /* end of program */ mov r0, #0 @ return code mov r7, #EXIT @ request to exit program swi 0 @ perform the system call
/******************************************************************/ /* call function parameter in register */ /******************************************************************/ /* r0 value one */ /* r1 value two */ /* return in r0 */ fonction1:
push {fp,lr} /* save des 2 registres */ push {r1,r2} /* save des autres registres */ mov r2,#20 mul r0,r2 add r0,r0,r1 pop {r1,r2} /* restaur des autres registres */ pop {fp,lr} /* restaur des 2 registres */ bx lr /* retour procedure */
/******************************************************************/ /* call function parameter in the stack */ /******************************************************************/ /* return in r0 */ fonction2:
push {fp,lr} /* save des 2 registres */ add fp,sp,#8 /* address parameters in the stack*/ push {r1,r2} /* save des autres registres */ ldr r0,[fp] ldr r1,[fp,#4] mov r2,#-20 mul r0,r2 add r0,r0,r1 pop {r1,r2} /* restaur des autres registres */ pop {fp,lr} /* restaur des 2 registres */ add sp,#8 /* very important, for stack aligned */ bx lr /* retour procedure */
/******************************************************************/ /* affichage des messages avec calcul longueur */ /******************************************************************/ /* r0 contient l adresse du message */ affichageMess:
push {fp,lr} /* save des 2 registres */ push {r0,r1,r2,r7} /* save des autres registres */ mov r2,#0 /* compteur longueur */
1: /*calcul de la longueur */
ldrb r1,[r0,r2] /* recup octet position debut + indice */ cmp r1,#0 /* si 0 c est fini */ beq 1f add r2,r2,#1 /* sinon on ajoute 1 */ b 1b
1: /* donc ici r2 contient la longueur du message */
mov r1,r0 /* adresse du message en r1 */ mov r0,#STDOUT /* code pour écrire sur la sortie standard Linux */ mov r7, #WRITE /* code de l appel systeme 'write' */ swi #0 /* appel systeme */ pop {r0,r1,r2,r7} /* restaur des autres registres */ pop {fp,lr} /* restaur des 2 registres */ bx lr /* retour procedure */
/***************************************************/ /* conversion registre en décimal signé */ /***************************************************/ /* r0 contient le registre */ /* r1 contient l adresse de la zone de conversion */ conversion10S:
push {fp,lr} /* save des 2 registres frame et retour */ push {r0-r5} /* save autres registres */ mov r2,r1 /* debut zone stockage */ mov r5,#'+' /* par defaut le signe est + */ cmp r0,#0 /* nombre négatif ? */ movlt r5,#'-' /* oui le signe est - */ mvnlt r0,r0 /* et inversion en valeur positive */ addlt r0,#1
mov r4,#10 /* longueur de la zone */
1: /* debut de boucle de conversion */
bl divisionpar10 /* division */ add r1,#48 /* ajout de 48 au reste pour conversion ascii */ strb r1,[r2,r4] /* stockage du byte en début de zone r5 + la position r4 */ sub r4,r4,#1 /* position précedente */ cmp r0,#0 bne 1b /* boucle si quotient different de zéro */ strb r5,[r2,r4] /* stockage du signe à la position courante */ subs r4,r4,#1 /* position précedente */ blt 100f /* si r4 < 0 fin */ /* sinon il faut completer le debut de la zone avec des blancs */ mov r3,#' ' /* caractere espace */
2:
strb r3,[r2,r4] /* stockage du byte */ subs r4,r4,#1 /* position précedente */ bge 2b /* boucle si r4 plus grand ou egal a zero */
100: /* fin standard de la fonction */
pop {r0-r5} /*restaur des autres registres */ pop {fp,lr} /* restaur des 2 registres frame et retour */ bx lr
/***************************************************/ /* division par 10 signé */ /* Thanks to http://thinkingeek.com/arm-assembler-raspberry-pi/* /* and http://www.hackersdelight.org/ */ /***************************************************/ /* r0 contient le dividende */ /* r0 retourne le quotient */ /* r1 retourne le reste */ divisionpar10:
/* r0 contains the argument to be divided by 10 */ push {r2-r4} /* save autres registres */ mov r4,r0 ldr r3, .Ls_magic_number_10 /* r1 <- magic_number */ smull r1, r2, r3, r0 /* r1 <- Lower32Bits(r1*r0). r2 <- Upper32Bits(r1*r0) */ mov r2, r2, ASR #2 /* r2 <- r2 >> 2 */ mov r1, r0, LSR #31 /* r1 <- r0 >> 31 */ add r0, r2, r1 /* r0 <- r2 + r1 */ add r2,r0,r0, lsl #2 /* r2 <- r0 * 5 */ sub r1,r4,r2, lsl #1 /* r1 <- r4 - (r2 * 2) = r4 - (r0 * 10) */ pop {r2-r4} bx lr /* leave function */ .align 4
.Ls_magic_number_10: .word 0x66666667
</lang>
Arturo
<lang rebol>printHello: $[][ print "Hello World!" ]
sayHello: $[name][ print ["Hello" name "!"] ]
printAll: $[args][ loop args [arg][ print arg ] ]
getNumber: $[][3]
- Calling a function that requires no arguments
printHello
- Calling a function with a fixed number of arguments
sayHello "John"
- Calling a function with a variable number of arguments
printAll ["one" "two" "three"]
- Using a function in statement context
if true [printHello] print getNumber
- Using a function in first-class context within an expression
if getNumber=3 [print "yep, it worked"]
- Obtaining the return value of a function
num: getNumber
print num </lang>
- Output:
Hello World! Hello John ! one two three Hello World! 3 yep, it worked 3
AutoHotkey
<lang AHK>; Call a function without arguments: f()
- Call a function with a fixed number of arguments
f("string", var, 15.5)
- Call a function with optional arguments
f("string", var, 15.5)
- Call a function with a variable number of arguments
f("string", var, 15.5)
- Call a function with named arguments
; AutoHotkey does not have named arguments. However, in v1.1+, ; we can pass an object to the function:
f({named: "string", otherName: var, thirdName: 15.5})
- Use a function in statement context
f(1), f(2) ; What is statement context?
- No first-class functions in AHK
- Obtaining the return value of a function
varThatGetsReturnValue := f(1, "a")
- Cannot distinguish built-in functions
- Subroutines are called with GoSub; functions are called as above.
- Subroutines cannot be passed variables
- Stating whether arguments are passed by value or by reference
- [v1.1.01+]
- The IsByRef() function can be used to determine
- whether the caller supplied a variable for a given ByRef parameter.
- A variable cannot be passed by value to a byRef parameter. Instead, do this
f(tmp := varIdoNotWantChanged)
- the function f will receive the value of varIdoNotWantChanged, but any
- modifications will be made to the variable tmp.
- Partial application is impossible.
</lang>
AWK
The awk interpreter reads the entire script prior to processing, so functions can be called from sections of code appearing before the definition.
<lang awk>BEGIN {
sayhello() # Call a function with no parameters in statement context b=squareit(3) # Obtain the return value from a function with a single parameter in first class context
}</lang>
In awk, scalar values are passed by value, but arrays are passed by reference. Note that if a function has no arguments, then empty parentheses are required.
The awk extraction and reporting language does not support the use of named parameters.
Axe
In Axe, up to six arguments are passed as the variables r₁ through r₆. As with all variables in Axe, these exist in the global scope, which makes nested function calls and recursion quite difficult. <lang axe>NOARG() ARGS(1,5,42)</lang>
Since arguments are simply global variables, they are always optional and can be omitted from right to left. <lang axe>OPARG(1,2,3,4,5,6) OPARG(1,2,3) OPARG()</lang>
Somewhat similar to TI-83 BASIC, the last evaluated expression becomes the return value of the function. However, this is distinct from the Ans variable. Return values can be captured just like any other expression. <lang axe>MATHS(2,4)→A Disp GETSTR()</lang>
User-defined functions can be distinguished from language-defined functions by the fact that language-defined function names are composed of atomic tokens (usually with built-in parentheses) whereas user-defined function names are composed of individual characters. Also, because only uppercase letters are available by default in the OS, most user-defined names are all uppercase while language-defined names are mixed case. <lang axe>USER() axeFunc()</lang>
BASIC256
<lang basic256>function Copialo$ (txt$, siNo, final$) nuevaCadena$ = ""
for cont = 1 to siNo nuevaCadena$ += txt$ next cont
return trim(nuevaCadena$) + final$ end function
subroutine Saludo() print "Hola mundo!" end subroutine
subroutine testCadenas (txt$) for cont = 1 to length(txt$) print mid(txt$, cont, 1); ""; next cont end subroutine
subroutine testNumeros (a, b, c) print a, b, c end subroutine
call Saludo() print Copialo$("Saludos ", 6, "") print Copialo$("Saludos ", 3, "!!") print call testNumeros(1, 2, 3) call testNumeros(1, 2, 0) print call testCadenas("1, 2, 3, 4, cadena, 6, 7, 8, \#incluye texto\#") end</lang>
- Output:
Igual que la entrada de FreeBASIC.
Batch File
Batch files do not have a traditional "function" system like OOP languages, however this is the closest thing to it. The only difference between a block of code and a function is the way method you choose to invoke it. It's also worth noting that all batch files can be called from any other batch file, performing a function. A function should be put somewhere in the code where it will not be parsed unless the script is redirected there.
<lang dos>
- http://rosettacode.org/wiki/Call_a_function
- Demonstrate the different syntax and semantics provided for calling a function.
@echo off
echo Calling myFunction1 call:myFunction1 echo.
echo Calling myFunction2 11 8 call:myFunction2 11 8 echo.
echo Calling myFunction3 /fi and saving the output into %%filecount%% call:myFunction3 /fi echo.%filecount% echo.
echo Calling myFunction4 1 2 3 4 5 call:myFunction4 1 2 3 4 5 echo.
echo Calling myFunction5 "filename=test.file" "filepath=C:\Test Directory\" call:myFunction5 "filename=test.file" "filepath=C:\Test Directory\" echo. echo Calling myFunction5 "filepath=C:\Test Directory\" "filename=test.file" call:myFunction5 "filepath=C:\Test Directory\" "filename=test.file" echo.
pause>nul exit
- Requires no arguments
- myFunction1
echo myFunction1 has been called. goto:eof
- Fixed number of arguments (%a% & %b%)
- myFunction2
:: Returns %a% + %b% setlocal set /a c=%~1+%~2 endlocal & echo %c% goto:eof
- Optional arguments
- myFunction3
:: Returns the amount of folders + files in the current directory :: /fi Returns only file count :: /fo Returns only folder count
setlocal set count=0
if "%~1"=="" set "command=dir /b" if "%~1"=="/fi" set "command=dir /b /A-d" if "%~1"=="/fo" set "command=dir /b /Ad"
for /f "usebackq" %%i in (`%command%`) do set /a count+=1
endlocal & set filecount=%count% goto:eof
- Variable number of arguments
- myFunction4
:: Returns sum of arguments setlocal :myFunction4loop set sum=0 for %%i in (%*) do set /a sum+=%%i endlocal & echo %sum% goto:eof
- Named Arguments (filepath=[path] & filename=[name])
- myFunction5
:: Returns the complete path based off the 2 arguments if "%~1"=="" then goto:eof setlocal enabledelayedexpansion set "param=%~1"
for /l %%i in (1,1,2) do ( for /f "tokens=1,2 delims==" %%j in ("!param!") do set %%j=%%k set "param=%~2" ) endlocal & echo.%filepath%%filename% goto:eof </lang> Output:
Calling myFunction1 myFunction1 has been called. Calling myFunction2 11 8 19 Calling myFunction3 /fi and saving the output into %filecount% 1 Calling myFunction4 1 2 3 4 5 15 Calling myFunction5 "filename=test.file" "filepath=C:\Test Directory\" C:\Test Directory\test.file Calling myFunction5 "filepath=C:\Test Directory\" "filename=test.file" C:\Test Directory\test.file
BBC BASIC
BBC BASIC distinguishes between functions (which return one value), procedures (which may return an arbitrary number of values including zero), and subroutines. Functions can be built-in or user-defined. A call to a built-in function (for example, the square root function) is an expression: <lang bbcbasic>PRINT SQR(2)</lang> The parentheses can often be omitted: <lang bbcbasic>PRINT SQR 2</lang> The name of a user-defined function must begin with FN. A call to it is also an expression: <lang bbcbasic>PRINT FN_foo(bar$, baz%)</lang> (The sigils $ and % identify the variables' types.) A function that takes no arguments can be called omitting the parentheses: <lang bbcbasic>PRINT FN_foo</lang> The name of a procedure must begin with PROC. A call to it is a statement, not an expression: <lang bbcbasic>PROC_foo</lang> If it has arguments, they come in parentheses just as with a function: <lang bbcbasic>PROC_foo(bar$, baz%, quux)</lang> Note that you cannot tell from this syntax which of the variables bar$, baz%, and quux are arguments provided to the procedure and which of them are return values from it. You have to look at where it is defined: <lang bbcbasic>DEF PROC_foo(a$, RETURN b%, RETURN c)</lang> Subroutines are provided for compatibility with older, unstructured dialects of BASIC; otherwise they are never really used. They require statements to be numbered, and they can neither receive arguments nor return values: they can only manipulate global variables. The GOSUB and RETURN statements in fact mirror assembly language 'jump to subroutine' and 'return from subroutine' instructions quite closely. <lang bbcbasic>200 GOSUB 30050</lang>
BQN
BQN's functions take one or two arguments, and the behave like the primitive functions of the language.
Calling a function that requires no arguments: BQN does not have zero argument functions. Having a function that does so is done with the help of a dummy argument, like so:
<lang BQN>{𝕊 ·: 1 + 1}0</lang>
The dot symbol ·
indicates that the argument is nothing, and hence is discarded. Hence, the zero provided to it is discarded, and 1 + 1 = 2 is returned.
Calling a function with a fixed number of arguments: BQN functions always take 1 or two arguments, and their names must always start with a capital letter. A function is called like a primitive function, by writing its name or the function itself between the arguments. For example, given a function named F
:
<lang bqn>F 1</lang> is an example of a single argument call.
<lang bqn>2 F 1</lang> is an example of a two argument call.
Calling a function with optional arguments: optional arguments are not supported by BQN.
Calling a function with a variable number of arguments: A function supporting variable arguments can be made by taking an array as any of the arguments.
Calling a function with named arguments: BQN has block headers, which destructure an input array into given variables using pattern matching. These can then be referenced later by the names given.
<lang bqn>{
𝕊 one‿two‿three: one∾two∾three
}</lang>
Given a three element array, the above example will concatenate them all together.
Using a function in statement context: BQN user defined functions have the same syntactic roles as primitive functions in an expression, so they can be used like any primitive.
<lang bqn>1 {𝕨+𝕩} 2</lang> is the same as <lang bqn>1 + 2</lang>
Using a function in first-class context within an expression: BQN supports lisp-style functional programming, and hence supports first class usage of functions.
<lang bqn>⟨+, -, ∾⟩</lang> is an example of a list of functions, which can later be called with the help of a higher order function.
Obtaining the return value of a function: A block function will always return the value of the last statement within it. To obtain the return value of a function, you can assign it to a variable, or modify an existing variable with the return value.
<lang bqn>var ← Func # insert arg here</lang>
Arguments are passed to BQN functions by value only.
Partial Application: BQN has two combinators for this purpose. Before (⊸
) returns a function with a constant left argument, and After (⟜
) returns a function with a constant right argument.
<lang bqn>+⟜2</lang> will add two to the number given to it. <lang bqn>2⊸-</lang> will subtract its input from two.
Bracmat
- Calling a function that requires no arguments:
Strictly speaking, all Bracmat functions receive at least one argument. But empty strings are valid expressions, so you can do
<lang bracmat>aFunctionWithoutArguments$</lang> or <lang bracmat>aFunctionWithoutArguments'</lang>
Both function calls pass the right and side of the $
or '
operator. This is in fact still something: an empty string.
The $
operator always evaluates its right hand side before passing it to the function, while the '
does not. Therefore it is slightly faster to use the functionName'
variant.
- Calling a function with a fixed number of arguments:
Bracmat passes exactly one argument to a function, called arg
. The argument can be any Bracmat expression. In patterns, if a function call expression is a pattern component, a second argument sjt
is added, the part of the subject that the pattern component is going trying to match.
- Calling a function with optional arguments
There is no special syntax for that. It is up to the programmer to define a datastructure with a variable part, e.g., a list.
- Calling a function with a variable number of arguments
Same answer.
- Calling a function with named arguments
There is no special syntax for that. You could pass a list of (name.value) pairs.
- Using a function in statement context
A f...
You can do
<lang bracmat>func$!myargument;</lang>
The ;
marks the end of a Bracmat statement.
- Using a function in first-class context within an expression
(Copied from JavaScript:) Bracmat functions are first-class citizens; they can be stored in variables and passed as arguments. Assigning to a variable yourfunc
can be done in a few ways. The most common one is
<lang bracmat>(yourfunc=local vars.function body)</lang>
If there is already a function myfunc
that you want to assign to yourfunc
as well, do
<lang bracmat>('$myfunc:(=?yourfunc))</lang>
- Obtaining the return value of a function
<lang bracmat>myfunc$!myarg:?myresult</lang>
Notice that the returned value can be any evaluated expression.
- Distinguishing built-in functions and user-defined functions
You cannot list built-in functions that are implemented directly in C. Nor can such functions be passed as arguments or assigned to variables. There are also a number of built-in functions that are written in Bracmat. They are nothing special and can be deleted or redefined. You can see a list of all currently defined functions that are written in Bracmat with the function call cat'
.
- Distinguishing subroutines and functions
You can ignore the return value of a function myfunc
as follows:
<lang bracmat>myfunc$!myarg&yourfunc$!yourarg</lang>
But notice that if myfunc
fails, the above expression returns the value produced by myfunc
! To also ignore the success/failure of a function, do
<lang bracmat>`(myfunc$!myarg)&yourfunc$!yourarg</lang>
- Stating whether arguments are passed by value or by reference
Values are passed by reference, or by value if the reference counter, which is a very small integer, overflows. Most values are immutable, so for those there is no practical difference between passing by reference or value. The single exception of a mutable value is always passed by reference, and has an enormous reference counter. (The binary operator =
introduces a mutable value and can be used for an object oriented style of programming.)
- Is partial application possible and how
There is no special syntax for that, but you can write a function that e.g., can take a list with one or with two elements and that returns a function in the first case.
<lang bracmat>( ( plus
= a b . !arg:%?a ?b & !b: & '(.!arg+$a) | !a+!b )
& out$("1+2, not partial:" plus$(1 2)) & out$("1+2, partial:" (plus$1)$2) );</lang>
Output:
1+2, not partial: 3 1+2, partial: 3
C
<lang c>/* function with no argument */ f();
/* fix number of arguments */ g(1, 2, 3);
/* Optional arguments: err...
Feel free to make sense of the following. I can't. */
int op_arg(); int main() { op_arg(1); op_arg(1, 2); op_arg(1, 2, 3); return 0; } int op_arg(int a, int b) { printf("%d %d %d\n", a, b, (&b)[1]); return a; } /* end of sensible code */
/* Variadic function: how the args list is handled solely depends on the function */ void h(int a, ...) { va_list ap; va_start(ap); ... } /* call it as: (if you feed it something it doesn't expect, don't count on it working) */ h(1, 2, 3, 4, "abcd", (void*)0);
/* named arguments: this is only possible through some pre-processor abuse
- /
struct v_args {
int arg1; int arg2; char _sentinel;
};
void _v(struct v_args args) {
printf("%d, %d\n", args.arg1, args.arg2);
}
- define v(...) _v((struct v_args){__VA_ARGS__})
v(.arg2 = 5, .arg1 = 17); // prints "17,5" /* NOTE the above implementation gives us optional typesafe optional arguments as well (unspecified arguments are initialized to zero)*/ v(.arg2=1); // prints "0,1" v(); // prints "0,0"
/* as a first-class object (i.e. function pointer) */ printf("%p", f); /* that's the f() above */
/* return value */ double a = asin(1);
/* built-in functions: no such thing. Compiler may interally give special treatment
to bread-and-butter functions such as memcpy(), but that's not a C built-in per se */
/* subroutines: no such thing. You can goto places, but I doubt that counts. */
/* Scalar values are passed by value by default. However, arrays are passed by reference. */ /* Pointers *sort of* work like references, though. */</lang>
C#
<lang c sharp> /* 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(); </lang>
C++
<lang C++>
/* function with no arguments */ foo(); </lang>
<lang C++> /* passing arguments by value*/ /* function with one argument */ bar(arg1); /* function with multiple arguments */ baz(arg1, arg2); </lang>
<lang C++> /* get return value of a function */ variable = function(args); </lang>
<lang C++>
- include <iostream>
using namespace std; /* passing arguments by reference */ void f(int &y) /* variable is now passed by reference */ { y++; } int main() { int x = 0; cout<<"x = "<<x<<endl; /* should produce result "x = 0" */ f(x); /* call function f */ cout<<"x = "<<x<<endl; /* should produce result "x = 1" */ } </lang>
COBOL
<lang cobol>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</lang>
Clojure
Note: I ran all these examples in the REPL so there is no printed output; instead I have denoted the value of each expression evaluated using `;=>'.
Calling a function that requires no arguments <lang clojure> (defn one []
"Function that takes no arguments and returns 1" 1)
(one); => 1 </lang> Calling a function with a fixed number of arguments <lang clojure> (defn total-cost [item-price num-items]
"Returns the total price to buy the given number of items" (* item-price num-items))
(total-cost 1 5); => 5 </lang> Calling a function with optional arguments The syntax is exactly the same for the calling code; here's an example of the exact same function as above, except now it takes an optional third argument (discount-percentage) <lang clojure> (defn total-cost-with-discount [item-price num-items & [discount-percentage]]
"Returns total price to buy the items after discount is applied (if given)" (let [discount (or discount-percentage 0)] ;; Assign discount to either the discount-percentage (if given) or default 0 if not (-> item-price (* num-items) ;; Calculate total cost (* (- 100 discount)) ;; Apply discount (/ 100.0))))
- Now we can use the function without the optional arguments, and see the same behaviour as our total-cost function
(total-cost-with-discount 1 5); => 5
- Or we can add the third parameter to calculate the cost with 20% discount
(total-cost-with-discount 1 5 20); => 4 </lang> Calling a function with a variable number of arguments You can use the optional argument syntax seen above to implement variable numbers, but here's another way to do it by writing multiple functions with different arguments all in one.
Once again, calling the function is the same, but you need to know what types of arguments are expected for each arity. <lang clojure> (defn make-address
([city place-name] (str place-name ", " city)) ([city street house-number] (str house-number " " street ", " city)) ([city street house-number apartment] (str house-number " " street ", Apt. " apartment ", " city)))
- To call the function you just need to pass whatever arguments you are supplying as you would with a fixed number
- First case- the queen doesn't need a street name
(make-address "London" "Buckingham Palace"); => "Buckingham Palace, London"
- Second case
(make-address "London" "Downing Street" 10); => "10 Downing Street, London"
- Third case
(make-address "London" "Baker Street" 221 "B"); => "221 Baker Street, Apt. B, London" </lang> Calling a function with named arguments The way to do this in clojure is to pass the arguments as a map and destructure them by name in the function definition. The syntax is the same, but it requires you to pass a single map argument containing all of your arguments and their names. <lang clojure> (defn make-mailing-label [{:keys [name address country]}]
"Returns the correct text to mail a letter to the addressee" (str name "\n" address "\n" (or country "UK"))) ;; If country is nil, assume it is the UK
- We can call it with all three arguments in a map to get mickey's international address
(make-mailing-label {:name "Mickey Mouse"
:address "1 Disney Avenue, Los Angeles" :country "USA"}); => "Mickey Mouse\n1 Disney Avenue, Los Angeles\nUSA"
- Or we can call it with fewer arguments for domestic mail
(make-mailing-label {:name "Her Majesty"
:address "Buckingham Palace, London"}); => "Her Majesty\nBuckingham Palace, London\nUK"
</lang> Using a function in statement context I'm not really sure what this means - you can use a function to assign a variable, but there aren't really statements <lang clojure> (defn multiply-by-10 [number]
(* 10 number))
(def fifty (multiply-by-10 5))
fifty; => 50 </lang>
Using a function in first-class context within an expression
Functions are always first-class, here are some examples of how they can be used in first class context:
You can use one function to create another <lang clojure> (defn make-discount-function [discount-percent]
"Returns a function that takes a price and applies the given discount" (fn [price] (-> price (* (- 100 discount-percent)) (/ 100.0))))
- Now we can create a '20% off' function to calculate prices with your discount card
(def discount-20pc (make-discount-function 20))
- Use the function to calculate some discount prices
(discount-20pc 100); => 80 (discount-20pc 5); => 4
- Your friend has a better discount card, we can use the same function to create their discount card function
(def discount-50pc (make-discount-function 50)) (discount-50pc 100); => 50 (discount-50pc 5); => 2.5 </lang>
You can store functions in collections as if they were variables <lang clojure>
- Continuing on the same example, let's imagine Anna has a 20% discount card and Bill has 50%. Charlie pays full price
- We can store their discount functions in a map
(def discount-cards {"Anna" discount-20pc
"Bill" discount-50pc "Charlie" identity}) ;; Identity returns whatever value was passed to the function (in this case it will be price)
- Now we can access them by cardholder name in another function
(defn calculate-discounted-price [price shopper-name]
"Applies the correct discount for the person" (let [discount-fn (get discount-cards shopper-name)] ;; Get the right discount function (discount-fn price))) ;; Apply discount function to the price
(calculate-discounted-price 100 "Anna"); => 80 (calculate-discounted-price 100 "Bill"); => 50 (calculate-discounted-price 100 "Charlie"); => 100 </lang> You can pass functions as arguments to other functions <lang clojure>;; Here we have two functions to format a price depending on the country
(defn format-price-uk [price]
(str "£" price))
(defn format-price-us [price]
(str "$" price))
- And one function that takes a price formatting function as an argument
(defn format-receipt [item-name price price-formatting-function]
"Return the item name and price formatted according to the function" (str item-name " " (price-formatting-function price))) ;; Call the format function to get the right representation of the price
(format-receipt "Loo Roll" 5 format-price-uk); => "Loo Roll £5"
(format-receipt "Toilet Paper" 5 format-price-us); => "Toilet Paper $5" </lang> Obtaining the return value of a function <lang clojure>;;You can assign it to a variable:
(def receipt-us (format-receipt "Toilet Paper" 5 format-price-us))
- Then the variable holds the value
receipt-us; => "Toilet Paper $5"
- Or you can use it in a call to another function
(defn add-store-name [receipt]
"A function to add a footer to the receipt" (str receipt "\n Thanks for shopping at Safeway" ))
- Calls add-store-name with the result of the format function
(add-store-name (format-receipt "Toilet Paper" 5 format-price-us)); => "Toilet Paper $5\n Thanks for shopping at Safeway" </lang> Distinguishing built-in functions and user-defined functions <lang clojure>;; They are indistinguishable in Clojure, and you can even override a built in one
- Using built-in addition
(+ 5 5); => 10
- Using custom defined addition
(defn ? [a b]
"Returns the sum of two numbers" (+ a b))
(? 5 5); => 10
- Overriding a built in function is possible but not recommended
(defn * [a b] ;; Redefining the multiplication operator
"Returns the sum of two numbers" (+ a b))
(* 5 5); => 10 </lang> Distinguishing subroutines and functions <lang clojure>;; They are the same thing - indeed, everything in clojure is a function
- Functions without return values simply return nil
(defn no-return-value [a]
(print (str "Your argument was" a "; now returning nil")))
(no-return-value "hi"); => nil </lang> Stating whether arguments are passed by value or by reference All data structures are immutable, so they are passed by value only. The value returned from the function does not change the original input <lang clojure>;; Set up a variable that we will pass to a function (def the-queen {:name "Elizabeth"
:title "Your Majesty" :address "Buckingham Palace" :pets ["Corgi" "Horse"]})
- A function to modify the data
(defn adopt-pet [person pet]
"Adds pet to the person's list of pets" (update person :pets #(conj % pet)))
- Calling the function returns a new data structure with the modified pets
(adopt-pet the-queen "Ferret"); => {:name "Elizabeth":title "Your Majesty" :address "Buckingham Palace" :pets ["Corgi" "Horse" "Ferret]}
- The original data structure is not changed
the-queen; => {:name "Elizabeth" :title "Your Majesty" :address "Buckingham Palace" :pets ["Corgi" "Horse"]}</lang>
Is partial application possible and how
Yes, it is similar to the discount card case we saw earlier. Instead of having a function return another function, we can use partial:
<lang clojure>(defn apply-discount [discount-percentage price]
"Function to apply a discount to a price" (-> price (* (- 100 discount-percentage)) ;; Apply discount (/ 100.0)))
- Here we have assigned the variable to a partial function
- It means 'call apply-discount with 10 as the first argument'
(def discount-10pc-option-1 (partial apply-discount 10))
- And is equivalent to this
(defn discount-10pc-option-2 [price]
(apply-discount 10 price))
(discount-10pc-option-1 100); => 90
(discount-10pc-option-2 100); => 90 </lang>
CoffeeScript
<lang coffeescript>
- Calling a function that requires no arguments
foo()
- Calling a function with a fixed number of arguments
foo 1
- Calling a function with optional arguments
- (Optional arguments are done using an object with named keys)
foo 1, optionalBar: 1, optionalBaz: 'bax'
- Calling a function with a variable number of arguments
- for a function `foo` defined as `foo = ( args... ) ->`
foo 1, 2, 3, 4
- Calling a function with named arguments
- (Named arguments are done using an object with named keys)
foo bar: 1, bax: 'baz'
- Using a function in statement context
x = foo 1
- Using a function in first-class context within an expression
- (For `foo` defined as `foo = ( x ) -> x + 1`
x = [ 1, 2, 3 ].map foo
- Obtaining the return value of a function
x = foo 1
- Arguments are passed by value, even objects. Objects
- are passed as the _value_ of the reference to an object.
- Example:
bar = ( person ) ->
# Since `person` is a reference # to the person passed in, we can assign # a new value to its `name` key. person.name = 'Bob'
# Since `person` is just the value of # the original reference, assigning to it # does not modify the original reference. person = new Person 'Frank'
- Partial application is only possible manually through closures
curry = ( f, fixedArgs... ) ->
( args... ) -> f fixedArgs..., args...
- Example usage
add = ( x, y ) -> x + y
add2 = curry add, 2
add2 1 #=> 3 </lang>
Common Lisp
<lang lisp>
- Calling a function that requires no arguments
(defun a () "This is the 'A' function") (a)
- Calling a function with a fixed number of arguments
(defun b (x y) (list x y)) (b 1 2)
- Calling a function with optional arguments
(defun c (&optional x y) (list x y)) (c 1)
- Calling a function with a variable number of arguments
(defun d (&rest args) args) (d 1 2 3 4 5 6 7 8)
- Calling a function with named arguments
(defun e (&key (x 1) (y 2)) (list x y)) (e :x 10 :y 20)
- Using a function in first-class context within an expression
(defun f (func) (funcall func)) (f #'a)
- Obtaining the return value of a function
(defvar return-of-a (a))
- Is partial application possible and how
(defun curry (function &rest args-1)
(lambda (&rest args-2) (apply function (append args-1 args-2))))
(funcall (curry #'+ 1) 2) </lang>
Cubescript
<lang Cubescript> // No arguments myfunction
// All functions can take a variable number of arguments. // These can be accessed from within the function with the aliases: // $arg1, $arg2, $arg3... $numargs tells the amount of args passed. myfunction word "text string" 1 3.14
// Getting a function's return value retval = (myfunction)
// Trying to do a variable lookup on a builtin function will return an empty // string. This can be used to distinguish builtin functions from user-defined // ones. if (strcmp $echo "") [echo builtin function] // true if (strcmp $myfunction "") [echo builtin function] // false </lang>
D
<lang d>import std.traits;
enum isSubroutine(alias F) = is(ReturnType!F == void);
void main() {
void foo1() {}
// Calling a function that requires no arguments: foo1(); foo1; // Alternative syntax.
void foo2(int x, int y) {}
immutable lambda = function int(int x) => x ^^ 2;
// Calling a function with a fixed number of arguments: foo2(1, 2); foo2(1, 2); cast(void)lambda(1);
void foo3(int x, int y=2) {}
// Calling a function with optional arguments: foo3(1); foo3(1, 3);
int sum(int[] arr...) { int tot = 0; foreach (immutable x; arr) tot += x; return tot; }
real sum2(Args...)(Args arr) { typeof(return) tot = 0; foreach (immutable x; arr) tot += x; return tot; }
// Calling a function with a variable number of arguments: assert(sum(1, 2, 3) == 6); assert(sum(1, 2, 3, 4) == 10); assert(sum2(1, 2.5, 3.5) == 7);
// Calling a function with named arguments: // Various struct or tuple-based tricks can be used for this, // but currently D doesn't have named arguments.
// Using a function in statement context (?): if (1) foo1;
// Using a function in first-class context within an expression: assert(sum(1) == 1);
auto foo4() { return 1; }
// Obtaining the return value of a function: immutable x = foo4;
// Distinguishing built-in functions and user-defined functions: // There are no built-in functions, beside the operators, and // pseudo-functions like assert().
int myFynction(int x) { return x; } void mySubroutine(int x) {}
// Distinguishing subroutines and functions: // (A subroutine is merely a function that has no explicit // return statement and will return void). pragma(msg, isSubroutine!mySubroutine); // Prints: true pragma(msg, isSubroutine!myFynction); // Prints: false
void foo5(int a, in int b, ref int c, out int d, lazy int e, scope int f) {}
// Stating whether arguments are passed by value, by reference, etc: alias STC = ParameterStorageClass; alias psct = ParameterStorageClassTuple!foo5; static assert(psct.length == 6); // Six parameters. static assert(psct[0] == STC.none); static assert(psct[1] == STC.none); static assert(psct[2] == STC.ref_); static assert(psct[3] == STC.out_); static assert(psct[4] == STC.lazy_); static assert(psct[5] == STC.scope_); // There are also inout and auto ref.
int foo6(int a, int b) { return a + b; }
// Is partial application possible and how: import std.functional; alias foo6b = partial!(foo6, 5); assert(foo6b(6) == 11);
}</lang>
- Output:
true false
Dart
<lang dart>void main() {
// Function definition // See the "Function definition" task for more info void noArgs() {} void fixedArgs(int arg1, int arg2) {} void optionalArgs([int arg1 = 1]) {} void namedArgs({required int arg1}) {} int returnsValue() {return 1;} // Calling a function that requires no arguments noArgs(); // Calling a function with a fixed number of arguments fixedArgs(1, 2); // Calling a function with optional arguments optionalArgs(); optionalArgs(2); // Calling a function with named arguments namedArgs(arg1: 1); // Using a function in statement context if (true) { noArgs(); } // Obtaining the return value of a function var value = returnsValue();
}</lang>
Delphi
Delphi allows everything what Pascal allows. In addition, the following is also possible:
Calling a function without arguments and obtaining its return value: <lang delphi>foo()</lang> Calling a function with optional arguments: <lang delphi>foo(1)</lang> Calling a function with a variable number of arguments: <lang delphi>foo(1, 2, 3, 4, 5)</lang> Using a function in a statement context: <lang delphi>writeLn('Hello world.'); foo; writeLn('Goodbye world')</lang> Like above, an empty parameter list, i. e. (), could be supplied too.
Dragon
- Calling a function that requires no arguments
<lang dragon>myMethod()</lang>
- Calling a function with a fixed number of arguments
<lang dragon>myMethod(97, 3.14)</lang>
Dyalect
Calling a function that requires no arguments:
<lang Dyalect>func foo() { } foo()</lang>
Calling a function with a fixed number of arguments:
<lang Dyalect>func foo(x, y, z) { } foo(1, 2, 3)</lang>
Calling a function with optional arguments:
<lang Dyalect>func foo(x, y = 0, z = 1) { } foo(1)</lang>
Calling a function with a variable number of arguments:
<lang Dyalect>func foo(args...) { } foo(1, 2, 3)</lang>
Calling a function with named arguments:
<lang Dyalect>func foo(x, y, z) { } foo(z: 3, x: 1, y: 2)</lang>
Using a function in statement context:
<lang Dyalect>func foo() { } if true {
foo()
}</lang>
Using a function in first-class context within an expression:
<lang Dyalect>func foo() { } var x = if foo() {
1
} else {
2
}</lang>
Obtaining the return value of a function:
<lang Dyalect>func foo(x) { x * 2 } var x = 2 var y = foo(x)</lang>
Distinguishing built-in functions and user-defined functions:
<lang Dyalect>//Built-in functions are regular functions from an implicitly imported "lang" module //There is no actual difference between these functions and user-defined functions
//You can however write a function that would check if a given function is declared in "lang" module: func isBuiltin(fn) =>
fn.Name is not nil && fn.Name in lang && lang[fn.Name] == fn
//Usage: func foo() { } //A user-defined function print(isBuiltin(foo)) //Prints: false print(isBuiltin(assert)) //Prints: true</lang>
Distinguishing subroutines and functions:
<lang Dyalect>//There is no difference between subroutines and functions: func foo() { } //doesn't explicitly return something (but in fact returns nil) func bar(x) { return x * 2 } //explicitly returns value (keyword "return" can be omitted)</lang>
Stating whether arguments are passed by value or by reference:
<lang Dyalect>//All arguments are passed by reference</lang>
Is partial application possible and how:
<lang Dyalect>//Using a closure: func apply(fun, fst) { snd => fun(fst, snd) }
//Usage: func sum(x, y) { x + y }
var sum2 = apply(sum, 2) var x = sum2(3) //x is 5
//By second argument func flip(fun) { (y, x) => fun(x, y) } func sub(x, y) { x - y }
var sub3 = apply(flip(sub), 3) x = sub3(9) //x is 6</lang>
Déjà Vu
<lang dejavu># all functions used are from the standard library
- calling a function with no arguments:
random-int
- calling a function with a fixed number of arguments:
+ 1 2
- calling a function with optional arguments:
- optional arguments are not really possible as such
- generally differently named functions are used:
sort [ 3 2 1 ] sort-by @len [ "Hello" "World" "Bob" ]
- calling a function with a variable number of arguments:
- generally with a special terminator value, which one depends
- on the function called
concat( 1 2 3 ) [ 1 2 3 ] set{ :foo :bar :spam }
- calling a function with named arguments: not possible
- using a function in first-class context within an expression
$ @-- @len # $ is "compose", so the function returned is "one less than the length"
- obtaining the return value of a function
- return values are always pushed on the stack, so you don't need anything special
random-int
- discarding the return value of a function
drop random-int
- method call:
local :do { :nuthin @pass } do!nuthin !import!fooModule # same as eva!import :fooModule
- arguments are passed by object-identity, like in Python and Lua
- partial application is not possible, due to the fact that
- a function's arity is a property of its behavior and not
- of its definition</lang>
Elena
ELENA 4.1: Declaring closures <lang elena> var c0 := { console.writeLine("No argument provided") }; var c2 := (int a, int b){ console.printLine("Arguments ",a," and ",b," provided") }; </lang> Calling a closure without arguments <lang elena>
c0();
</lang> Calling a closure with arguments <lang elena>
c2(2,4);
</lang> Passing arguments by reference: <lang elena>
var exch := (ref object x){ x := 2 }; var a := 1; exch(ref a);
</lang>
Elixir
<lang elixir>
- Anonymous function
foo = fn() ->
IO.puts("foo")
end
foo() #=> undefined function foo/0 foo.() #=> "foo"
- Using `def`
defmodule Foo do
def foo do IO.puts("foo") end
end
Foo.foo #=> "foo" Foo.foo() #=> "foo"
- Calling a function with a fixed number of arguments
defmodule Foo do
def foo(x) do IO.puts(x) end
end
Foo.foo("foo") #=> "foo"
- Calling a function with a default argument
defmodule Foo do
def foo(x \\ "foo") do IO.puts(x) end
end
Foo.foo() #=> "foo" Foo.foo("bar") #=> "bar"
- There is no such thing as a function with a variable number of arguments. So in Elixir, you'd call the function with a list
defmodule Foo do
def foo(args) when is_list(args) do Enum.each(args, &(IO.puts(&1))) end
end
- Calling a function with named arguments
defmodule Foo do
def foo([x: x]) do IO.inspect(x) end
end </lang>
Erlang
<lang erlang> no_argument() one_argument( Arg ) optional_arguments( Arg, [{opt1, Opt1}, {another_opt, Another}] ) variable_arguments( [Arg1, Arg2 | Rest] ) names_arguments([{name1, Arg1}, {another_name, Another}] ) % Statement context? % First class context? Result = obtain_result( Arg1 ) % No way to distinguish builtin/user functions % Subroutines? % Arguments are passed by reference, but you can not change them. % Partial application is possible (a function returns a function that has one argument bound) </lang>
F#
<lang fsharp>// No arguments noArgs()
// Fixed number of arguments oneArg x
// Optional arguments // In a normal function: optionalArgs <| Some(5) <| None // In a function taking a tuple: optionalArgsInTuple(Some(5), None) // In a function in a type: foo.optionalArgs 5;; // However, if you want to pass more than one paramter, the arguments must be // passed in a tuple: foo.optionalArgs(5, 6)
// Function with a variable number of arguments variableArgs 5 6 7 // etc...
// Named arguments can only be used in type methods taking a tuple. The // arguments can appear in any order. foo.namedArgs(x = 5, y = 6)
// Using a function in a statement for i = 0 to someFunc() do
printfn "Something"
// Using a function in a first-class context funcArgs someFunc
// Obtaining a return value let x = someFunc()
// Built-in functions: do functions like (+) or (-) count?
// Parameters are normally passed by value (as shown in the previous examples), // but they can be passed by reference. // Passing by reference: refArgs &mutableVal
// Partial application example let add2 = (+) 2</lang>
Factor
- Calling a word with no arguments:
<lang Factor>foo</lang>
- Calling a word with a fixed number of arguments. This will pull as many objects as it needs from the stack. If there are not enough, it will result in a stack underflow.
<lang Factor>foo</lang>
- No special support for optional arguments.
- Variable arguments are achieved by defining a word that takes an integer, and operates on that many items at the top of the stack:
<lang Factor>"a" "b" "c" 3 narray ! { "a" "b" "c" }</lang>
- The named arguments idiom is to define a tuple, set its slots, and pass it to a word:
<lang Factor><email>
"jack@aol.com" >>from { "jill@aol.com" } >>to "Hello there" >>subject body >>body
send-email</lang>
- First-class context: this pushes a word to the stack. Use execute to evaluate.
<lang Factor>\ foo</lang> Additionally, you can put words directly inside sequences and quotations for deferred execution: <lang Factor>{ foo } [ foo ]</lang>
- Obtaining the return value, which will be placed on the stack:
<lang Factor>foo</lang>
- Returns true if the word is defined in the Factor VM as opposed to in a vocabulary. It should be noted that there are very few primitives.
<lang Factor>\ foo primitive?</lang>
- Factor makes no distinction between subroutines and functions.
- It's not perfectly accurate to think of words as passing arguments by reference (or at all), since all words simply operate on the data stack. However, it is still important for the programmer to understand that words which make duplicates of objects such as dup and over do so by copying references. If one wishes for a shallow copy of a non-immediate object, one may use clone.
- Partial application is possible by use of curry. Here, the object 2 is curried into the left side of the quotation (anonymous function) [ - ]:
<lang Factor>{ 1 2 3 } 2 [ - ] curry map . ! { -1 0 1 }</lang>
Forth
<lang forth>a-function \ requiring no arguments a-function \ with a fixed number of arguents a-function \ having optional arguments a-function \ having a variable number of arguments a-function \ having such named arguments as we have in Forth ' a-function var ! \ using a function in a first-class context (here: storing it in a variable) a-function \ in which we obtain a function's return value
\ forth lacks 'statement contenxt' \ forth doesn't distinguish between built-in and user-defined functions \ forth doesn't distinguish between functions and subroutines \ forth doesn't care about by-value or by-reference
\ partial application is achieved by creating functions and manipulating stacks
- curried 0 a-function ;
- only-want-third-argument 1 2 rot a-function ;
\ Realistic example:
- move ( delta-x delta-y -- )
y +! x +! ;
- down ( n -- ) 0 swap move ;
- up ( n -- ) negate down ;
- right ( n -- ) 0 move ;
- left ( n -- ) negate right ;</lang>
Fortran
Examples
<lang Fortran>program main implicit none integer :: a integer :: f, g logical :: lresult interface
integer function h(a,b,c) integer :: a, b integer, optional :: c end function
end interface write(*,*) 'no arguments: ', f() write(*,*) '-----------------' write(*,*) 'fixed arguments: ', g(5,8,lresult) write(*,*) '-----------------' write(*,*) 'optional arguments: ', h(5,8), h(5,8,4) write(*,*) '-----------------' write(*,*) 'function with variable arguments: Does not apply!' write(*,*) 'An option is to pass arrays of variable lengths.' write(*,*) '-----------------' write(*,*) 'named arguments: ', h(c=4,b=8,a=5) write(*,*) '-----------------' write(*,*) 'function in statement context: Does not apply!' write(*,*) '-----------------' write(*,*) 'Fortran passes memory location of variables as arguments.' write(*,*) 'So an argument can hold the return value.' write(*,*) 'function result: ', g(5,8,lresult) , ' function successful? ', lresult write(*,*) '-----------------' write(*,*) 'Distinguish between built-in and user-defined functions: Does not apply!' write(*,*) '-----------------' write(*,*) 'Calling a subroutine: ' a = 30 call sub(a) write(*,*) 'Function call: ', f() write(*,*) '-----------------' write(*,*) 'All variables are passed as pointers.' write(*,*) 'Problems can arise if instead of sub(a), one uses sub(10).' write(*,*) '-----------------' end program
!no argument integer function f() f = 10 end function
!fixed number of arguments integer function g(a, b, lresult) integer :: a, b logical :: lresult g = a+b lresult = .TRUE. end function
!optional arguments integer function h(a, b, c) integer :: a, b integer, optional :: c
h = a+b if(present(c)) then
h = h+10*c
end if end function
!subroutine subroutine sub(a) integer :: a a = a*100 write(*,*) 'Output of subroutine: ', a end subroutine </lang>
no arguments: 10 ----------------- fixed arguments: 13 ----------------- optional arguments: 13 53 ----------------- function with variable arguments: Does not apply! An option is to pass arrays of variable lengths. ----------------- named arguments: 53 ----------------- function in statement context: Does not apply! ----------------- Fortran passes memory location of variables as arguments. So an argument can hold the return value. function result: 13 function successful? T ----------------- Distinguish between built-in and user-defined functions: Does not apply! ----------------- Calling a subroutine: Output of subroutine: 3000 Function call: 10 ----------------- All variables are passed as pointers. Problems can arise if instead of sub(a), one uses sub(10). -----------------
In other words
As described in Naming Conventions, First Fortran (1958) allowed user-written functions but with restrictions on the names so that an ordinary variable called SIN would be disallowed because it was deemed to be in conflict with the library function SINF. These constraints were eased with Fortran II, and the rule became that a user could employ any correct-form name, such as SQRT, for a variable's name (simple or array) but then the library function SQRT would become inaccessible in such a routine. Similarly, there would be no point in the user writing a function called SQRT, because it could not be invoked - the compiler would take any invocation as being for the library routine SQRT. Thus, a user-written function could perhaps chance to have the name of an obscure (i.e. one forgotten about) library function, but if you were lucky it would have conflicting parameters and the compiler will complain.
A special case is provided by the "arithmetic statement function" that is defined after declarations but before executable statements in a routine and which has access to all the variables of the routine. Consider <lang Fortran> REAL this,that
DIST(X,Y,Z) = SQRT(X**2 + Y**2 + Z**2) + this/that !One arithmetic statement, possibly lengthy. ... D = 3 + DIST(X1 - X2,YDIFF,SQRT(ZD2)) !Invoke local function DIST.</lang>
In this case, even if "DIST" happened to be the name of some library function, invocations within the routine defining it would not be of the library function.
This flexibility in naming can be turned the other way around. For example, some compilers offer the intrinsic function SIND which calculates sine in degrees. Simply defining an array REAL SIND(0:360)
(and properly initialising it) enables the slowish SIND function to be approximated by the faster indexing of an array. Put another way, an array is a function of a limited span of integer-valued arguments and is called in arithmetic expressions with the same syntax as is used for functions, be they intrinsic or user-written. Those writing in Pascal would be blocked by its insistence that arrays employ [] rather than (). Similarly, when testing, an array's declaration might be commented out and a function of that name defined, which function could check its arguments, write to a log file, note time stamps, or whatever else comes to mind. But alas, there is no "palindromic" or reverse-entry facility whereby a function could handle the assignment of a value to an array that would make this fully flexible.
Within a function there are some delicacies. The usual form is to assign the desired result to the name of the variable as in H = A + B
where H
is the name of the function. However, during evaluation the desired result might be developed over many stages and with reference to prior values. Suppose function H is to combine results from separate statements and it is not convenient to achieve this via one lengthy expression, perhaps because of conditional tests. Something like <lang Fortran> H = A + B
IF (blah) H = 3*H - 7</lang>
As written, the appearance of H
on the right-hand side of an expression does not constitute a call of function H
at all. Some compilers fail to deal with this as hoped, and so one must use a scratch variable such as FH
to develop the value, then remember to ensure that the assignment H = FH
is executed before exiting the function, by whatever route. If the result is a large datum (a long character variable, say) this is annoying.
With the belated recognition of recursive possibilities (introduced by Algol in the 1960s) comes the possibility of a function invoking itself. In the above example, H(3.7,5.5,6.6)
would clearly be a function invocation (because of the parentheses) whereas H
would not be. Actually, Fortran routines have always been able to engage in recursion, it is just the returns that will fail - except on a stack-based system such as the Burroughs 6700 in the 1970s.
Fortran also offers the ability to pass a function as a parameter such that the recipient routine can call it, as in <lang Fortran> REAL FUNCTION INTG8(F,A,B,DX) !Integrate function F.
EXTERNAL F !Some function of one parameter. REAL A,B !Bounds. REAL DX !Step. INTEGER N !A counter. INTG8 = F(A) + F(B) !Get the ends exactly. N = (B - A)/DX !Truncates. Ignore A + N*DX = B chances. DO I = 1,N !Step along the interior. INTG8 = INTG8 + F(A + I*DX) !Evaluate the function. END DO !On to the next. INTG8 = INTG8/(N + 2)*(B - A) !Average value times interval width. END FUNCTION INTG8 !This is not a good calculation!
FUNCTION TRIAL(X) !Some user-written function. REAL X TRIAL = 1 + X !This will do. END FUNCTION TRIAL !Not the name of a library function.
PROGRAM POKE INTRINSIC SIN !Thus, not an (undeclared) ordinary variable. EXTERNAL TRIAL !Likewise, but also, not an intrinsic function. REAL INTG8 !Don't look for the result in an integer place. WRITE (6,*) "Result=",INTG8(SIN, 0.0,8*ATAN(1.0),0.01) WRITE (6,*) "Linear=",INTG8(TRIAL,0.0,1.0, 0.01) END</lang>
This involves a fair amount of juggling special declarations so that the compiler will make the desired assumptions that a function is being called upon, rather than the value of some variable. This is eased somewhat with F90 onwards if the MODULE protocol is used so that at least you do not have to remember to declare INTG8 as REAL. Certain library functions are not allowed as candidates for passing to INTG8 (for instance, the compiler may render them as in-line code, bypassing the protocol used for functions) and arithmetic statement functions are usually rejected, as would be an array masquerading as a function. Arithmetic expressions are not allowable as possible "functions" either - how might something like sin(x) + 3*sqrt(x) + 7
be recognised as a function instead? As INTG8(SIN + 3*SQRT + 7,etc...
? Unlike Algol, Fortran does not offer the call-by-name facility as used in Jensen's Device, which would be something like INTG8(SIN(X) + 3*SQRT(X) + 7,etc...
and would also require passing variable X. Perhaps a keyword BYNAME might be introduced one day. Until then a properly-named function must be declared and its name only be given. And of course, candidate functions must have the correct number and type of parameters, or else...
This works because Fortran passes parameters by reference (i.e. by giving the machine address of the entity), so that for functions, the code's entry point for the function is passed. With normal variables this means that a function (or subroutine) might modify the value of a parameter, as well as returning the function's result - and also mess with any COMMON data or other available storage, so a function EATACARD(IN) might read a line of data into a shared work area (called say ACARD) from I/O unit number IN and return true, otherwise false should it hit end-of-file.
But it is also possible that parameters are passed via copy-in copy-out instead of by reference, with subtle changes in behaviour. This may also be done even on systems that do employ passing by reference. For instance, with <lang Fortran> TYPE MIXED
CHARACTER*12 NAME INTEGER STUFF END TYPE MIXED TYPE(MIXED) LOTS(12000)</lang>
One might hope to try IT = BCHOP(LOTS.NAME,"Fred")
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.
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>
FreeBASIC
<lang freebasic> Sub Saludo()
Print "Hola mundo!"
End Sub
Function Copialo(txt As String, siNo As Short, final As String = "") As String
Dim nuevaCadena As String For cont As Short = 1 To siNo nuevaCadena &= txt Next Return Trim(nuevaCadena) & final
End Function
Sub testNumeros(a As Integer, b As Integer, c As Integer = 0) Print a, b, c End Sub
Sub testCadenas(txt As String)
For cont As Byte = 0 To Len(txt) Print Chr(txt[cont]); ""; Next cont
End Sub
Saludo Print Copialo("Saludos ", 6) Print Copialo("Saludos ", 3, "!!") ? testNumeros(1, 2, 3) testNumeros(1, 2) ? testCadenas("1, 2, 3, 4, cadena, 6, 7, 8, \'incluye texto\'") </lang>
- Output:
Hola mundo! Saludos Saludos Saludos Saludos Saludos Saludos Saludos Saludos Saludos!! 1 2 3 1 2 0 1, 2, 3, 4, cadena, 6, 7, 8, \'incluye texto\'
Free Pascal
See Delphi. Note, calling a function as if it was a procedure [i. e. discarding the return value] is only permitted if you set the compiler setting {$extendedSyntax on}/{$X+}. This is the default.
Gambas
Click this link to run this code
Some of the uses of Procedures/Functions in Gambas
<lang gambas>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</lang> Output:
Hello world! Hello Hello Hello Hello Hello Hello Hello Hello Hello!!
Go
The following examples use functions from the standard packages plus a few dummy local functions:
- <lang go>import (
"image" "image/gif" "io/ioutil" "strings" "unicode" )
func f() (int, float64) { return 0, 0 } func g(int, float64) int { return 0 } func h(string, ...int) {}</lang>
- Calling with no arguments and calling with a fixed number of arguments:
- <lang go> f()
g(1, 2.0) // If f() is defined to return exactly the number and type of // arguments that g() accepts than they can be used in place: g(f()) // But only without other arguments, this won't compile: //h("fail", f()) // But this will: g(g(1, 2.0), 3.0)</lang>
- Calling with a variable number of arguments:
- This is only possible with functions defined with a trailing optional/variable length argument of a single type (as
h
above). <lang go> h("ex1")
- This is only possible with functions defined with a trailing optional/variable length argument of a single type (as
h("ex2", 1, 2) h("ex3", 1, 2, 3, 4) // such functions can also be called by expanding a slice: list := []int{1,2,3,4} h("ex4", list...) // but again, not mixed with other arguments, this won't compile: //h("fail", 2, list...)</lang>
- Optional arguments and named arguments are not supported.
- However, it is reasonably common to see a structure used for this. In this example
gif.Options
is a structure with multiple members which can initialized/assigned by name or omitted (or the whole third argument can just benil
). <lang go> gif.Encode(ioutil.Discard, image.Black, &gif.Options{NumColors: 16})</lang>
- However, it is reasonably common to see a structure used for this. In this example
- Optional arguments are supported.
- <lang go>package main
import "fmt"
type Params struct { a, b, c int } func doIt(p Params) int { return p.a + p.b + p.c }
func main() { fmt.Println(doIt(Params{a: 1, c: 9})) // prt 10 }</lang>
- Named arguments are supported.
- <lang go>package main
import "fmt"
func bar(a, b, c int) { fmt.Printf("%d, %d, %d", a, b, c) }
func main() { args := make(map[string]int) args["a"] = 3 args["b"] = 2 args["c"] = 1 bar(args["a"], args["b"], args["c"]) // prt 3, 2, 1 }</lang>
- Within a statement context.
- Assignment statements are shown later. Only functions returning a single value can be used in a single value context: <lang go> if 2*g(1, 3.0)+4 > 0 {}</lang>
- In a first-class context:
- <lang go> fn := func(r rune) rune {
if unicode.IsSpace(r) { return -1 } return r } strings.Map(fn, "Spaces removed") strings.Map(unicode.ToLower, "Test") strings.Map(func(r rune) rune { return r + 1 }, "shift")</lang>
- Obtaining the value:
- <lang> a, b := f() // multivalue return
_, c := f() // only some of a multivalue return d := g(a, c) // single return value e, i := g(d, b), g(d, 2) // multiple assignment</lang>
- Built-in functions and user defined functions can not be distinguished.
- Functions from the standard packages look like any other. The few truly built-in functions are only different in that they have no package specifier like local functions (and they sometimes have extra capabilities). <lang go> list = append(list, a, d, e, i)
i = len(list)</lang>
- Go has no subroutines, just functions and methods.
- Go arguments are passed by value.
- As with C, a pointer can be used to achieve the effect of reference passing. (Like pointers, slice arguments have their contents passed by reference, it's the slice header that is passed by value).
- Go arguments are passed by value or by reference
- <lang go>package main
import "fmt"
// int parameter, so arguments will be passed to it by value. func zeroval(ival int) { ival = 0 } // has an *int parameter, meaning that it takes an int pointer. func zeroptr(iptr *int) { *iptr = 0 } func main() { i := 1 fmt.Println("initial:", i) // prt initial: 1 zeroval(i) fmt.Println("zeroval:", i) // prt zeroval: 1 zeroptr(&i) fmt.Println("zeroptr:", i) // prt zeroptr: 0 fmt.Println("pointer:", &i) // prt pointer: 0xc0000140b8 }</lang>
- Partial and Currying is not directly supported.
- However something similar can be done, see Partial function application#Go
- <lang go>package main
import "fmt"
func mkAdd(a int) func(int) int { return func(b int) int { return a + b } } func sum(x, y int) int { return x + y }
func partialSum(x int) func(int) int { return func(y int) int { return sum(x, y) } } func main() { // Is partial application possible and how add2 := mkAdd(2) add3 := mkAdd(3) fmt.Println(add2(5), add3(6)) // prt 7 9 // Currying functions in go partial := partialSum(13) fmt.Println(partial(5)) //prt 18 }</lang>
Groovy
There are two types of first-class functions in Groovy.
- The first are functions defined in scripts, although they behave as if they are methods of the script "class".
- The second are closures, which are similar to lambdas in Java, except that they are defined as their own class type and must be explicitly converted to single-method "functional" interfaces. There are many methods within the Groovy API that accept closures as arguments.
- Calling a function that requires no arguments
<lang groovy>noArgs()</lang>
- Calling a function with a fixed number of arguments
<lang groovy>fixedArgs(1, "Zing", Color.BLUE, ZonedDateTime.now(), true)</lang>
- Calling a function with optional arguments
<lang groovy>optArgs("It's", "a", "beautiful", "day") optArgs("It's", "a", "beautiful") optArgs("It's", "a") optArgs("It's")</lang>
- Calling a function with a variable number of arguments
<lang groovy>varArgs("It's", "a", "beautiful", "day") varArgs("It's", "a", "beautiful") varArgs("It's", "a") varArgs("It's")</lang>
- Calling a function with named arguments
It's complicated
- Using a function in statement context
<lang groovy>def mean = calcAverage(1.2, 4.5, 3, 8.9, 22, 3)</lang>
- Using a function in first-class context within an expression
- Create new functions from preexisting functions at run-time
<lang groovy>def oldFunc = { arg1, arg2 -> arg1 + arg2 } def newFunc = oldFunc.curry(30) assert newFunc(12) == 42</lang>
- Store functions in collections
<lang groovy>def funcList = [func1, func2, func3]</lang>
- Use functions as arguments to other functions
<lang groovy>def eltChangeFunc = { it * 3 - 1 } def changedList = list.collect(eltChangeFunc)</lang>
- Use functions as return values of other functions
<lang groovy>def funcMaker = { String s, int reps, boolean caps ->
caps ? { String transString -> ((transString + s) * reps).toUpperCase() } : { String transString -> (transString + s) * reps }
} def func = funcMaker("a", 2, true) assert func("pook") == "POOKAPOOKA"</lang>
- Obtaining the return value of a function
<lang groovy>def retVal = func(x, y, z)</lang>
- Distinguishing built-in functions and user-defined functions
There are no "built-in" functions. All is illusion.
- Stating whether arguments are passed by value or by reference
As with Java everything is passed by value, but object values are actually references (pointers). So,
- if the argument is a primative it is passed by value and changes are not manifested in the caller's context.
- if the argument is an object reference, the reference is passed by value and changes to the reference (re-assignment, for example) are not manifested in the caller's context, but changes in the object are.
- Is partial application possible and how
Partial application in Groovy is performed via currying (demonstrated above)
Haskell
<lang haskell> -- Calling a function with a fixed number of arguments multiply x y = x * y multiply 10 20 -- returns 200
-- Calling a function that requires no arguments -- Normally, you use constant instead of function without arguments: twopi = 6.28 -- But you can also pass special value as the first argument indicating function call: twopi () = 6.28 -- definition twopi :: Num a => () -> a -- its type twopi () -- returns 6.28
-- Partial application and auto-currying is built-in. multiply_by_10 = (10 * ) map multiply_by_10 [1, 2, 3] -- [10, 20, 30] multiply_all_by_10 = map multiply_by_10 multiply_all_by_10 [1, 2, 3] -- [10, 20, 30]
-- TODO: -- Calling a function with optional arguments -- Calling a function with a variable number of arguments -- Calling a function with named arguments -- 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 </lang>
i
<lang i>//The type of the function argument determines whether or not the value is passed by reference or not. //Eg. numbers are passed by value and lists/arrays are passed by reference.
software { print() //Calling a function with no arguments. print("Input a number!") //Calling a function with fixed arguments. print(1,2,3,4,5,6,7,8,9,0) //Calling a function with variable arguments. input = read() //Obtaining the return value of a function. myprint = print myprint("It was: ", input) //Calling first class functions, the same as calling ordinary functions.
//The only distinction that can be made between two functions is if they are 'real' or not. if type(myprint) = concept print("myprint is a not a real function") else if type(myprint) = function print("myprint is a real function") end
//Partial functions can be created with static parts. DebugPrint = print["[DEBUG] ", text] DebugPrint("partial function!") //This would output '[DEBUG] partial function!'
if type(DebugPrint) = concept print("DebugPrint is a not a real function") else if type(DebugPrint) = function print("DebugPrint is a real function") end } </lang>
Icon and Unicon
Icon and Unicon have generalized procedures and syntax that are used to implement functions, subroutines and generators.
- Procedures can return values or not and callers may use the returned values or not.
- Procedures in Icon and Unicon are first class values and can be assigned to variables which can then be used to call procedures. This also facilitates some additional calling syntax.
- Additionally, co-expressions are supported which allow for co-routine like transfers of control between two or more procedures. There are some differences in syntax for co-expression calls.
- There are no differences between calling built-in vs. user defined functions
- Named arguments is not natively supported; however, they can be supported using a user defined procedure as shown in Named parameters
- Method calling is similar with some extended syntax
- Arguments are basically passed by value or reference based on their type. Immutable values like strings, and numbers are passed by value. Mutable data types like structures are essentially references and although these are passed by value the effective behavior is like a call by reference.
For more information see Icon and Unicon Introduction on Rosetta
<lang Icon>procedure main() # demonstrate and describe function calling syntax and semantics
# normal procedure/function calling
f() # no arguments, also command context f(x) # fixed number of arguments f(x,h,w) # variable number of arguments (varargs) y := f(x) # Obtaining the returned value of a function # procedures as first class values and string invocation
f!L # Alternate calling syntax using a list as args (if \x then f else g)() # call (f or g)() f := write # assign a procedure f("Write is now called") # ... and call it "f"() # string invocation, procedure "-"(1) # string invocation, operator
# Co-expressions f{e1,e2} # parallel evaluation co-expression call # equivalent to f([create e1, create e2]) expr @ coexp # transmission of a single value to a coexpression [e1,e2]@coexp # ... of multiple values (list) to a coexpression coexp(e1,e2) # ... same as above but only in Unicon
# Other f("x:=",1,"y:=",2) # named parameters (user defined)
end</lang>
J
A function in J is typically represented by a verb. Under the right circumstances other syntactic entities (nouns, adverbs, conjunctions) can represent functions, but let's focus on the typical case.
A verb, in J, typically supports two syntactic variants:
<lang j> verb noun
noun verb noun</lang>
And a noun, in J, is an array.
An argument list can be represented by an array. Thus, when dealing with multiple arguments, a typical form is:
<lang j> function argumentList</lang>
Here, function
is a verb and argumentList
is a noun.
For example:
<lang j> sum(1,2,3)</lang>
Here sum
is a verb and (1,2,3)
is a noun.
Thus:
A function that requires no arguments can be simulated by calling a function with empty argument list: <lang j>f</lang> Note that an empty list of characters is not the only constant in the language which is an empty list. That said, most operations in the language do not care what type of data is not present, in an array which contains nothing.
A function with a fixed number of arguments gets special treatment in J when the fixed number is 1 or 2. <lang j>f 'one argument'</lang>and <lang j>'this example has two arguments' f 'the other argument'</lang> Alternatively, the function can be written such that an argument list is an error when it's the wrong length.
A function with a variable number of arguments (varargs): See above.
If argument types conflict they will need to be put in boxes and the function will have to take its arguments out of the boxes. Here's an unboxed example with five arguments: <lang j> f 1,2,3,4,5</lang> and here's a boxed example with five arguments: <lang j>f (<1),(<2),(<3),(<4),(<5) </lang> Note that the last set of parenthesis is unnecessary <lang j>f (<1),(<2),(<3),(<4),<5</lang> Note also that J offers some syntactic sugar for this kind of list <lang j>f 1; 2; 3; 4; <5</lang>. Note also that if the last argument in a semicolon list is not boxed there is no need to explicitly box it, since that is unambiguous (it must be boxed so that it conforms with the other members of the list). <lang j>f 1; 2; 3; 4; 5</lang>
A function with named arguments can be accomplished by calling a function with the names of the arguments. <lang j>f 'george';'tom';'howard'</lang> Other interpretations of this concept are also possible. For example, the right argument for a verb might be a list of argument names and the left argument might be a corresponding list of argument values: <lang j>1 2 3 f 'george';'tom';'howard'</lang> Or, for example a function which requires an object could be thought of as a function with named arguments since an object's members have names:<lang j> obj=: conew'blank'
george__obj=: 1 tom__obj=: 2 howard__obj=: 3 f obj coerase obj</lang> Name/value pairs can also be used for this purpose and can be implemented in various ways, including passing names followed by values <lang j>f 'george';1;'tom';2;'howard';3</lang> and passing a structure of pairs <lang j>f ('george';1),('tom';2),:(howard';3)</lang> Or, for example, the pairs could be individually boxed: <lang j>f ('george';1);('tom';2);<howard';3</lang>
Using a function in command context is no different from using a function in any other context, in J. Using a function in first class context within an expression is no different from using a function in any other context, in J.
Obtaining the return value of a function is no different from using a function in j. For example, here we add 1 to the result of a function: <lang j>1 + f 2</lang>
The only differences that apply to calling builtin functions rather than user defined functions is spelling of the function names.
There are no differences between calling subroutines and functions because J defines neither subroutines
nor functions
. Instead, J defines verbs
, adverbs
, and conjunctions
which for the purpose of this task are treated as functions. (All of the above examples used verbs. J's adverbs and conjunctions have stronger valence than its verbs.)
Java
Java does not have functions, but Java classes have "methods" which are equivalent.
- Calling a function that requires no arguments
<lang java>myMethod()</lang> We didn't specify an object (or a class) as the location of the method, so this.myMethod() is assumed. This applies to all the following examples.
- Calling a function with a fixed number of arguments
<lang java>myMethod(97, 3.14)</lang>
- Calling a function with optional arguments
This is possible if the method name is overloaded with different argument lists. For example: <lang java>int myMethod(int a, double b){
// return result of doing sums with a and b
}
int myMethod(int a){
return f(a, 1.414);
}</lang>
The compiler figures out which method to call based on the types of the arguments, so in this case the second argument appears to be optional. If you omit it, the value 1.414 is used. <lang java>System.out.println( myMethod( 97, 3.14 ) ); System.out.println( myMethod( 97 ) );</lang>
- Calling a function with a variable number of arguments
This is possible if the method is defined with varargs syntax. For example: <lang java>void printAll(String... strings){
for ( String s : strings ) System.out.println( s );
}</lang>
The type of strings is actually a string array, but the caller just passes strings: <lang java>printAll( "Freeman" ); printAll( "Freeman", "Hardy", "Willis" );</lang>
To avoid ambiguity, only the last argument to a function can have varargs.
- Calling a function with named arguments
Not directly possible, but you could simulate this (somewhat verbosely): <lang java>int myMethod( Map<String,Object> params ){
return ((Integer)params.get("x")).intValue() + ((Integer)params.get("y")).intValue();
}</lang>
Called like this: <lang java>System.out.println( myMethod(new HashMap<String,Object>()Template:Put("x",27);put("y",52);) );</lang>
Yuk.
- Using a function in statement context
If this means "use a function where a statement is expected", see all the other examples
- Using a function in first-class context within an expression
Not possible - must be wrapped in a class
- Obtaining the return value of a function
<lang java>int i = myMethod(x);</lang>
- Distinguishing built-in functions and user-defined functions
No distinction - all methods belong to classes, and there is no real distinction between built-in and user-defined classes.
- Distinguishing subroutines and functions
If the return type is void, you might consider a method as a subroutine rather than a function.
- Stating whether arguments are passed by value or by reference
All arguments are passed by value, but since object variables contain a reference to an object (not the object itself), objects appear to be passed by reference. For example: <lang java>myMethod(List<String> list){
// If I change the contents of the list here, the caller will see the change
}</lang>
- Is partial application possible and how
Don't know
JavaScript
The arguments to a JavaScript function are stored in a special array-like object which does not enforce arity in any way; a function declared to take n arguments may be called with none‒and vice versa‒without raising an error.
<lang JavaScript>var foo = function() { return arguments.length }; foo() // 0 foo(1, 2, 3) // 3</lang>
Neither optional (see above) nor named arguments are supported, though the latter (and the inverse of the former) may be simulated with the use of a helper object to be queried for the existence and/or values of relevant keys. Seriously, what is "statement context"?
JavaScript functions are first-class citizens; they can be stored in variables (see above) and passed as arguments. <lang JavaScript>var squares = [1, 2, 3].map(function (n) { return n * n }); // [1, 4, 9]</lang>
Naturally, they can also be returned, thus partial application is supported. <lang JavaScript> var make_adder = function(m) {
return function(n) { return m + n }
}; var add42 = make_adder(42); add42(10) // 52</lang>
Calling a user-defined function's toString() method returns its source verbatim; that the implementation is elided for built-ins provides a mechanism for distinguishing between the two.
<lang JavaScript>foo.toString() "function () { return arguments.length }" alert.toString() "function alert() { [native code] }"</lang>
Arguments are passed by value, but the members of collections are essentially passed by reference and thus propagate modification. <lang JavaScript>var mutate = function(victim) {
victim[0] = null; victim = 42;
}; var foo = [1, 2, 3]; mutate(foo) // foo is now [null, 2, 3], not 42</lang>
jq
jq functions are pure functions that are somewhat unusual in two respects:
- They are like commands in modern operating systems, in that they are parameterized filters that can accept input from the previous command and provide output to the next command if there is one in the pipeline of commands.
- Functions can not only process a stream of inputs, one at a time, but each argument can also accept a stream of inputs. The outputs are then a Cartesian product of the various inputs.
In this section, we use the notation fn/N to refer to a function named "fn" with N formal parameters.
Calling a function that requires no arguments
0-arity jq functions are invoked simply by specifying their name.
- Example: .
(Yes, "." is a 0-arity jq function.)
Calling a function with a fixed number of arguments
The conventional syntax is used except that ";" is the parameter separator. "," is used to construct a stream of values.
- Example: range(0;100;2)
Calling a function with optional arguments
Recent versions of jq allow one to define functions with the same name but different arities, and therefore if both fn/1 and fn/2 are defined, we may say that fn requires one parameter but accepts 2. In all cases, the syntax for function invocation is the same.
- Example: range(0; 10) and range(0; 10; 2)
Since jq functions can accept JSON arrays and objects, there are other ways to simulate optional arguments.
Calling a function with a variable number of arguments
See above.
Calling a function with named arguments
This is not directly supported but can be simulated by defining the function to accept JSON objects. For example, if fn were such a function, we might invoke fn like so: fn( {"required": 1, "optional": 2} ).
Using a function in statement context
The assignment to a local variable (e.g. (2*2) as $two) is similar to a statement context in that the expression as a whole does nothing to the flow of values from its input to its output.
Using a function in first-class context within an expression
jq functions cannot be assigned to variables but are otherwise "first-class" in that the composition of functions can be passed as arguments to other functions. No special syntax is required.
- Example: 2 | recurse(. * .) # generate the sequence 2, 4, 16, 256, ...
Obtaining the return value of a function
The value (or stream of values) returned by a function is (or are) automatically available to the next function in the pipeline (e.g. sin | cos); the returned value(s) can also be assigned to a local variable (e.g. sin as $v).
Distinguishing built-in functions and user-defined functions
Currently there is no such distinction, but user-defined functions always have the conventional form, whereas some built-in functions have special syntax.
Distinguishing subroutines and functions
A jq function can be written so as never to return anything (either because it returns without generating a value or because it generates an infinite stream of values), but there is no distinctive marker associated with such functions.
Stating whether arguments are passed by value or by reference
Arguments are in effect passed by value.
Is partial application possible and how
See Currying#jq.
Julia
<lang Julia>
- Calling a function that requires no arguments:
f() = print("Hello world!") f()
- Calling a function with a fixed number of arguments:
function f(x, y, z)
x*y - z^2
end
f(3, 4, 2)
- Calling a function with optional arguments:
- Note Julia uses multiple dispatch based on argument number and type, so
- f() is always different from f(x) unless default arguments are used, as in:
pimultiple(mult=1.0) = pi * mult # so pimultiple() defaults to pi * (1.0) or pi
- Calling a function with a variable number of arguments:
f(a,b,x...) = reduce(+, 0, x) - a - b
- here a and b are single arguments, but x is a tuple of x plus whatever follows x, so:
a = b = c = d = e = 3 f(a,b,c) # x within the function is (c) so == 0 + c - a - b f(a,b,c,d,e) # x is a tuple == (c,d,e) so == (0 + c + d + e) - a - b f(a,b) # x is () so == 0 - a - b
- Calling a function with named arguments:
- Functions with keyword arguments are defined using a semicolon in the function signature,
- as in
- function plot(x, y; style="solid", width=1, color="black")
- When the function is called, the semicolon is optional, so plot here can be
- either called with plot(x, y, width=2) or less commonly as plot(x, y; width=2).
- Using a function in statement context:
- Any function can be used as a variable by its name.
circlearea(x) = x^2 * pi map(circlearea, [r1, r2, r3, r4])
- Using a function in first-class context within an expression:
cylindervolume = circlearea(r) * h
- Obtaining the return value of a function:
radius = 2.5 area = circlearea(2.5)
- Distinguishing built-in functions and user-defined functions:
- Julia does not attempt to distinguish these in any special way,
- but at the REPL command line there is ? help available for builtin
- functions that would not generally be available for the user-defined ones.
- Distinguishing subroutines and functions:
- All subroutines are called functions in Julia, regardless of whether they return values.
- Stating whether arguments are passed by value or by reference:
- As in Python, all arguments are passed by pointer reference, but assignment to a passed argument
- only changes the variable within the function. Assignment to the values referenced by the argument
- DOES however change those values. For instance:
a = 3 b = [3] c = [3]
function f(x, y)
a = 0 b[1] = 0 c = [0]
end # a and c are now unchanged but b = [0]
- Is partial application possible and how:
- In Julia, there are many different ways to compose functions. In particular,
- Julia has an "arrow" operator -> that may be used to curry other functions.
f(a, b) = a^2 + a + b v = [4, 6, 8] map(x -> f(x, 10), v) # v = [30, 52, 82] </lang>
Kotlin
In Kotlin parameters are always passed by value though, apart from the (unboxed) primitive types, the value passed is actually a reference to an object. <lang scala>// 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
}</lang>
- Output:
No arguments One argument = 2 One required argument = 3, one optional argument = 0 Variable number of arguments = [4, 5, 6] One required argument = 7, one optional argument = 8 One argument = 9 20 25 3 No arguments 5.5 Hello world
Lambdatalk
In lambdatalk functions are abstractions {lambda {args} body} whose behaviour is best explained as a part of such a complete expression {{lambda {args} body} values}. <lang scheme>
The command
replace :a0 :a1 ... an-1
in expression containing some occurences of :ai by v0 v1 ... vp-1
is rewritten in a prefixed parenthesized form
{{lambda {:a0 :a1 ... an-1}
expression containing some occurences of :ai} v0 v1 ... vp-1}
so called IIFE (Immediately Invoked Function Expression), and defines an anonymous function containing a sequence of n arguments :ai, immediately invoked on a sequence of p values vi, and returning the expression in its body as so modified:
1) if p < n (partial application)
• the occurrences of the p first arguments are replaced in the function's body by the corresponding p given values, • a function waiting for missing n-p values is created, • and its reference is returned. • example: {{lambda {:x :y} ... :y ... :x ...} hello} -> {lambda {:y} ... :y ... hello ...} // replaces :x by hello -> LAMB_123 // the new functions's reference • called with the value world this function will return ... world ... hello ...
2) if p = n (normal application)
• the occurences of the n arguments are replaced in the function's body by the corresponding p given values, • the body is evaluated and the result is returned. • example {{lambda {:x :y} ... :y ... :x ...} hello world} -> {{lambda {:y} ... :y ... hello ...} world} // replaces :x by hello -> {{lambda {} ... world ... hello ...} } // replaces :y by world -> ... world ... hello ... // the value
3) if p > n (variadicity)
• the occurrences of the n-1 first arguments are replaced in the function's body by the corresponding n-1 given values, • the occurrences of the last argument are replaced in the body by the sequence of p-n supernumerary values, • the body is evaluated and the result is returned. • example: {{lambda {:x :y} ... :y ... :x ...} hello world good morning} -> {{lambda {:y} ... :y ... hello ...} world good morning} -> {{lambda {} ... world good morning ... hello ...}} -> ... world good morning ... hello ... // the value
More can be seen in http://lambdaway.free.fr/lambdawalks/?view=lambda </lang>
langur
User-defined and built-in functions can be called using parentheses.
Built-in functions can also be called using an "unbounded list," which ends at a line return, EOF, closing parenthesis, curly brace, or square bracket, or before a comma preceding a line return.
parentheses
<lang langur>.x()
- call user-defined function</lang>
<lang langur>write(.key, ": ", .value)
- call built-in with parentheses</lang>
unbounded lists
<lang langur>write .key, ": ", .value
- call built-in with unbounded list</lang>
<lang langur>writeln "numbers: ", join ", ", [.a1, .a2, .a3, .a4]
- unbounded lists on writeln and join
- later function join takes remaining arguments</lang>
<lang langur>writeln "numbers: ", join(", ", [.a1, .a2, .a3, .a4]), " === "
- unbounded list on writeln
- join using parentheses so it doesn't take remaining arguments</lang>
<lang langur>val .sum = foldfrom(
f(.sum, .i, .c) .sum + toNumber(.c, 36) x .weight[.i], 0, pseries len .code, split ZLS, .code,
)
- split, pseries, and len using unbounded lists, ending before comma preceding line return</lang>
<lang langur>for .key in sort(keys .tests) {
...
}
- unbounded list on keys bounded by closing parenthesis of sort</lang>
Latitude
Like Ruby, Latitude doesn't have functions in the traditional sense, only methods. Methods can be called with parentheses, as in many languages. If a method takes no arguments, the parentheses may be omitted. If a method takes a single argument and that argument is a literal (such as a literal number or string), then the parentheses may also be omitted. Additionally, Latitude provides an alternative syntax for method calls which replaces the parentheses with a colon.
<lang>foo (1, 2, 3). ; (1) Ordinary call foo (). ; (2) No arguments foo. ; (3) Equivalent to (2) foo (1). ; (4) Single-argument function foo 1. ; (5) Equivalent to (4) foo (bar). ; (6) Parentheses necessary here since bar is not a literal foo: 1, 2, 3. ; (7) Alternative syntax, equivalent to (1)</lang>
Although methods themselves can be passed around as first-class values, the method evaluation semantics often make such an approach suboptimal. If one needs a first-class function in the traditional sense, the usual approach is to wrap it in a Proc
object and then call it explicitly as needed.
<lang>myProc := proc { foo. }. myProc call (1, 2, 3).</lang>
If you want to write a function which can accept either a Proc
or a method object (as many standard library functions do, for convenience), you may use the shield
method to ensure that the object is a Proc
. shield
wraps methods in a Proc
while leaving objects which are already procedures alone.
<lang>myProc1 := #'foo shield. myProc2 := proc { foo. }. myProc3 := proc { foo. } shield.</lang>
All three of the above procedures will act the same.
LFE
Calling a function that requires no arguments:
In some module, define the following: <lang lisp> (defun my-func()
(: io format '"I get called with NOTHING!~n"))
</lang>
Then you use it like so (depending upon how you import it): <lang lisp> > (my-func) I get called with NOTHING! ok </lang>
Calling a function with a fixed number of arguments: In some module, define the following: <lang lisp> (defun my-func(a b)
(: io format '"I got called with ~p and ~p~n" (list a b)))
</lang>
Then you use it like so: <lang lisp> > (my-func '"bread" '"cheese") I got called with "bread" and "cheese" ok </lang>
Calling a function with optional arguments or calling a function with a variable number of arguments:
- Arguments are fixed in LFE/Erlang functions.
- One can have a dictionary, record, or list be the function argument, and use that to achieve something like variable/optional (and named) arguments.
- One can define multiple functions so that it appears that one is calling a function with optional or a variable number of arguments:
<lang lisp> (defmodule args
(export all))
(defun my-func ()
(my-func () () ()))
(defun my-func (a)
(my-func a () ()))
(defun my-func (a b)
(my-func a b ()))
(defun my-func (a b c)
(: io format '"~p ~p ~p~n" (list a b c)))
</lang>
Here is some example usage: <lang lisp> > (slurp '"args.lfe")
- (ok args)
> (my-func) [] [] [] ok > (my-func '"apple") "apple" [] [] ok > (my-func '"apple" '"banana") "apple" "banana" [] ok > (my-func '"apple" '"banana" '"cranberry") "apple" "banana" "cranberry" ok > (my-func '"apple" '"banana" '"cranberry" '"bad arg") exception error: #(unbound_func #(my-func 4)) </lang>
Calling a function with named arguments:
- LFE/Erlang doesn't support named arguments, per se.
- However, by using atoms in function argument patterns (a fairly common pattern), one can achieve similar effects.
- One may also use records or dicts as arguments to achieve similar effects.
Using a function in statement context:
<lang lisp>
...
(cond ((== count limit) (hit-limit-func arg-1 arg-2)) ((/= count limit) (keep-going-func count))) ...
</lang>
Using a function in first-class context within an expression:
From the LFE REPL: <lang lisp> > (>= 0.5 (: math sin 0.5)) true </lang>
Obtaining the return value of a function:
There are many, many ways to assign function outputs to variables in LFE. One fairly standard way is with the (let ...)
form:
<lang lisp>
(let ((x (: math sin 0.5)))
...)
</lang>
Distinguishing built-in functions and user-defined functions:
- There is no distinction made in LFE/Erlang between functions that are built-in and those that are not.
- "Built-in" for LFE/Erlang usually can be figured out: if a function has the module name
erlang
, e.g.,(: erlang list_to_integer ... )</cod>, then it's built-in.
- Most of the functions that come with LFE/Erlang are not even in the
erlang
module, but exist in other modules (e.g.,io
,math
, etc.) and in OTP. - One uses user/third-party modules in exactly the same way as one uses built-ins and modules that come with the Erlang distribution.
Distinguishing subroutines and functions:
- One commonly made distinction between functions and subroutines is that functions return a value (or reference, etc.) and subroutines do not.
- By this definition, LFE/Erlang does not support the concept of a subroutine; all functions return something.
Stating whether arguments are passed by value or by reference:
- Arguments and returns values are passed by reference in LFE/Erlang.
Is partial application possible?
- Not explicitly.
- However, one can use
lambda
s to achieve the same effect.
Liberty BASIC
<lang lb>
'Call a function - Liberty BASIC
'First, function result could not be discarded
' that is, you cannot do "f(x)" as a separate statement
'Calling a function that requires no arguments
res = f() 'brackets required
'Calling a function with a fixed number of arguments
res = g(x)
res = h(x,y)
'Calling a function with optional arguments
'impossible for user-defined functions
'Some build-in functions ex. INSTR and MID$ could be called with last argument omitted
'Calling a function with a variable number of arguments
'impossible
'Calling a function with named arguments
'impossible
'Using a function in statement context
'impossible (see starting notice)
'Using a function in first-class context within an expression
'impossible
'Obtaining the return value of a function
res = g(x)
'Distinguishing built-in functions and user-defined functions
'I would say impossible. Though built-in functions could be EVAL'ed,
'while user-defined would not be called (tries address array instead).
'Still cannot distinguish user-defined function from array.
'Distinguishing subroutines and functions
'then defined, subroutines and functions defined with words
'SUB and FUNCTION (case incensitive)
'Then used, function used as expression (with return value),
res = g(x)
'while subroutines called with special keyword CALL and without brackets
call test x, y
'Stating whether arguments are passed by value or by reference
'Variables passed as arguments into functions and subs are passed "by value" by default
'parameters could be passed "by reference" if formal parameter in sub/function definition uses the "byref" specifier
'Then calling a function, you can prevent pass by reference by changing variable to expression
' like x+0, x$+"" or just (x), (x$)
'Is partial application possible and how
'impossible
</lang>
Lingo
- Calling a function that requires no arguments
<lang lingo>foo()
-- or alternatively:
call(#foo, _movie)</lang>
- Calling a function with a fixed number of arguments
<lang lingo>foo(1,2,3)
-- or alternatively:
call(#foo, _movie, 1, 2, 3)</lang>
- Calling a function with optional arguments
<lang lingo>on foo (a, b)
if voidP(b) then b = 1
return a * b
end</lang>
<lang lingo>put foo(23, 2)
-- 46
put foo(23)
-- 23</lang>
- Calling a function with a variable number of arguments
<lang lingo>on sum ()
res = 0
repeat with i = 1 to the paramCount
res = res + param(i)
end repeat
return res
end</lang>
<lang lingo>put sum (1,2,3)
-- 6</lang>
- Calling a function with named arguments
Not directly supported, but you can of course re-write any function to only accept a single property list (hash) as argument, which can make sense e.g. for functions that have a lot of optional aruments.
- Using a function in statement context
- Using a function in first-class context within an expression
Lingo has no first-class functions, but the call(...) syntax (see above) allows to identify and use functions specified as "symbols" (e.g. #foo). This allows some "first-class alike" features:
<lang lingo>----------------------------------------
-- One of the five native iterative methods defined in ECMAScript 5
-- @param {list} tList
-- @param {symbol} cbFunc
-- @param {object} [cbObj=_movie]
-- @return {list}
on map (tList, cbFunc, cbObj)
if voidP(cbObj) then cbObj = _movie
res = []
cnt = tList.count
repeat with i = 1 to cnt
res[i] = call(cbFunc, cbObj, tList[i], i, tList)
end repeat
return res
end
on doubleInt (n)
return n*2
end</lang>
<lang lingo>l = [1,2,3]
put map(l, #doubleInt)
-- [2, 4, 6]</lang>
- Obtaining the return value of a function
<lang lingo>x = foo(1,2)</lang>
- Distinguishing built-in functions and user-defined functions
In Lingo all user-defined (global) functions are 'methods' of the _movie object, and there is AFAIK no direct way to distinguish those from _movie's built-in functions. But by iterating over of all movie scripts in all castlibs you can get a complete list of all user-defined (global) functions, and then any function not in this list is a built-in function:
<lang lingo>on getAllUserFunctions ()
res = []
repeat with i = 1 to _movie.castlib.count
c = _movie.castlib(i)
repeat with j = 1 to c.member.count
m = c.member[j]
if m.type<>#script then next repeat
if m.scripttype=#movie then
functions = m.script.handlers()
repeat with f in functions
res.append(f)
end repeat
end if
end repeat
end repeat
return res
end</lang>
<lang lingo>put getAllUserFunctions()
-- [#sum, #double, #getAllUserFunctions]</lang>
- Distinguishing subroutines and functions
In Lingo functions (also called "handlers") don't have to return anything, so there is no distinction. The return value of a function without a "return ..." line is VOID, and not distinguishable from a function with the explicit line "return VOID".
- Stating whether arguments are passed by value or by reference
In lingo 'objects' are always passed by reference, all other types (e.g. strings, integers, floats) by value. 'Objects' are e.g. lists (arrays), property lists (hashes), images and script instances. The built-in function objectP() returns TRUE (1) for objects and FALSE (0) for non-objects. To prevent the effects of call-by-reference, some object types (lists, property lists and images) support the method duplicate() to clone the object before passing it to a function:
<lang lingo>on double (someList)
cnt = someList.count
repeat with i = 1 to cnt
someList[i] = someList[i] * 2
end repeat
end</lang>
<lang lingo>l = [1,2,3]
double(l)
put l
-- [2, 4, 6]
l = [1,2,3]
double(l.duplicate())
put l
-- [1, 2, 3]</lang>
Little
The following examples use buldin functions, standard Tcl commands and some
local fuctions.
<lang C>// Calling a function that requires no arguments
void foo() {puts("Calling a function with no arguments");}
foo();
// Calling a function with a fixed number of arguments
abs(-36);
// Calling a function with optional arguments
puts(nonewline: "nonewline is an optional argument");
puts("\n");
// Calling a function with a variable number of arguments
void var_arg_func(...args) {
puts(length(args));
}
var_arg_func(1, 2);
var_arg_func(1, 2, 3);
// Obtaining the return value of a function
int s = clock("seconds"); //current time in seconds
// Calling a function with named arguments
// format is a named argument in Clock_format
int str = Clock_format(s, format: "%B");
puts(str);
// Stating whether arguments are passed by value or by reference
void f(int a, int &b) { a++; b++; }
{
int a = 0;
int b = 0;
f(a, &b);
puts (a);
puts (b);
}</lang>
Lua
<lang lua>-- Lua functions accept any number of arguments; missing arguments are nil-padded, extras are dropped.
function fixed (a, b, c) print(a, b, c) end
fixed() --> nil nil nil
fixed(1, 2, 3, 4, 5) --> 1 2 3
-- True vararg functions include a trailing ... parameter, which captures all additional arguments as a group of values.
function vararg (...) print(...) end
vararg(1, 2, 3, 4, 5) -- 1 2 3 4 5
-- Lua also allows dropping the parentheses if table or string literals are used as the sole argument
print "some string"
print { foo = "bar" } -- also serves as a form of named arguments
-- First-class functions in expression context
print(("this is backwards uppercase"):gsub("%w+", function (s) return s:upper():reverse() end))
-- Functions can return multiple values (including none), which can be counted via select()
local iter, obj, start = ipairs { 1, 2, 3 }
print(select("#", (function () end)())) --> 0
print(select("#", unpack { 1, 2, 3, 4 })) --> 4
-- Partial application
function prefix (pre)
return function (suf) return pre .. suf end
end
local prefixed = prefix "foo"
print(prefixed "bar", prefixed "baz", prefixed "quux")
-- nil, booleans, and numbers are always passed by value. Everything else is always passed by reference.
-- There is no separate notion of subroutines
-- Built-in functions are not easily distinguishable from user-defined functions
</lang>
Luck
<lang luck>/* Calling a function that requires no arguments */
f();;
/* Calling a function with a fixed number of arguments */
f(1,2);;
/* Calling a function with optional arguments
Note: defining the function is cumbersome but will get easier in future versions. */
f(1,2,new {default with x=3, y=4});;
/* Calling a function with a variable number of arguments */
printf("%d %d %d %d":char*,2,3,4,5);;
/* Calling a function with named arguments
Note: may get syntax sugar in future versions */
f(1,2,new {default with x=3, y=4});;
/* Using a function in statement context (what?) */
f();f();f();;
/* Using a function in first-class context within an expression */
[1,2,3].map(string);;
/* Obtaining the return value of a function */
let x:int = f();;
/* Distinguishing built-in functions and user-defined functions */
/* Builtin function i.e. custom calling convention: */
(@ binop "==" l r);;
/* User defined function i.e. normal function */
f(l)(r);;
/* Distinguishing subroutines and functions: both are supported, but compiler is not aware of difference */
sub();;
fun();;
/* Stating whether arguments are passed by value or by reference */
f(value);; /* by value */
f(&value);; /* by pointer reference */
f(ref(value));; /* by managed reference */
/* Is partial application possible and how */
tasty_curry(a)(b)(c)(d)(e)(f)(g)(h)(i)(j)(k)(l)(m)(n)(o)(p)(q)(r)(s)(t)(u)(v)(w)(x)(y)(z);;</lang>
M2000 Interpreter
// Calling a function that requires no arguments
ModuleA ' introduce a namespace can have modules/functions/subs, anything inside, and threads.
Call FunctionA() ' introduce a namespace can have modules/functions/subs, anything inside.
Call LambdaA() ' introduce a namespace can have modules/functions/subs, anything inside.
SubAlfa() ' subroutine this has the same scope as the caller, we can use Local statement to shadow variables/arrays
Print @Simple() ' simple function this has the same scope as the caller, we can use Local statement to shadow variables/arrays
Gosub labelA ' Statement Return used to return from a gosub, no arguments and no local statement used, the code is like any other code in the module/function/lambda scope. No jump out of scope allowed.
Gosub 10020 ' Statement Return used to return from a gosub,, no arguments and no local statement used, the code is like any other code in the module/function/lambda scope. No jump out of scope allowed.
// Calling a function with a fixed number of arguments
ModuleA 100
SubAlfa(100) ' subroutine
Print @Simple(100) ' simple function
Call FunctionA(100)
Call LambdaA(100)
// Calling a function with optional arguments
ModuleA ?,4
SubAlfa(?) ' subroutine
Print @Simple(?) ' simple function
Call FunctionA(,4)
Call LambdaA(,4)
// Calling a function with a variable number of arguments
Call FunctionA(1,2,3,4,5)
Call LambdaA(1,2,3,4,5)
// Calling a function with named arguments
ModuleA %X=10, %Z=20
// Using a function in statement context
Module A
SubAlfa()
Call Void FunctionA()
Call Void LambdaA()
// Using a function in first-class context within an expression
Print (lambda (x,y)->{=x**y}(2,3))*8=64
// Obtaining the return value of a function
A%=FunctionA%() ' integer part from any numeric type. Also A% if get decimals convert to integer using school rounding (0.5, so 3.5 is 4, 2.5 is 3)
A=FunctionA() ' numeric or object
A$=FunctionA$() ' string or object which return string
A=LambdaA()
A$=LambdaA$()
A=@SimpleA()
A$=@SimpleA$()
// Distinguishing built-in functions and user-defined functions
Def Cos(x)=100*X ' change the build in
Print Cos(3), @Cos(3) ' @Cos() is the build in anyway
' we can't use simple function with same name as a bultin function.
// Distinguishing subroutines and functions
Name without parenthesis like a command or statement is a Module
Name with parentesis without operator is a Sub
Name with parenthesis in an expression is a function or array (array come first), using a(*10) we say that it is function even an array a() exist.
Name with @ as first symbol and parenthesis like @alfa() is a simple function unless has a builtin name, so is that function.
// Stating whether arguments are passed by value or by reference
Default:By Value
By Reference: Using & at both sides (caller and calee)
Function Alfa(&x) {
=X : X++
}
Print Alfa(&counter)
Subs and Simple Functions not need a by reference pass because they are in the same scope from the caller, so actuall we pass by value to be local to those entities. But we can use by reference, except for static variables, and array items. Those can only passed by reference on modules and nornal functions and lambdas (which are normal functions plus more).
// Is partial application possible and how
A=Lambda (X) -> {
=Lambda X (Y) -> {
=Y**X
}
}
Cube=A(3) : Square=A(2)
Print Cube(2)=8, Square(2)=4
OOP: Functions/Modules can be members of objects.
Objects may have a name (they live until code get out of scope)
Objects may don't have a name but one or more pointers, (they live until counting references are zero)
ObjectA.moduleA is a call to an object method (a module)
pointerA=>moduleA is a call to an object by a pointer
An object may return values, and may accept parameters too. Also objects if they didn't return values other than a copy of them can be use operators defined as object functions.
Objects may have events and fire them using Call Event "simpleevent", param1, param2...
Functions and lambda (not simple functions), and object functions can be passed by reference as parameters.
Maple
Calling a function with no arguments:<lang Maple> f()</lang>
Calling a function with a fixed number of arguments:<lang Maple>f(1,sin(x), g -> int(g(t),t=0..1)</lang>
Calling a function with optional arguments: <lang Maple>f(1, sin(x), g -> int(g(t),t=0..1)</lang>
Calling a function with a variable number of arguments: <lang Maple>f(1, sin(x), g -> int(g(t),t=0..1)</lang>
Calling a function with named arguments:<lang Maple>f(a,b,method = foo)</lang>
Calling a function in a statements context:<lang Maple>f(a); f(b);</lang>
Using a function in first-class context within an expression:<lang Maple>f(a) + g(b)</lang>
Obtaining the return value of a function:<lang Maple> x := f(1)</lang>
Distinguishing built-in functions and user-defined functions:
<lang Maple>> type( op, 'builtin' );
true
</lang>
Distinguishing subroutines and functions: There is no distinction.
Stating whether arguments are passed by value or by reference: All values are passed by value.
However, if an argument is a name, then it can be assigned to and, if a value is mutable (such as an array), then it can be modified by the function. This is implemented by function definition; there is no distinction when calling the function.
Partial application is supported by the curry
and rcurry
commands.
Mathematica / Wolfram Language
Calling a function that requires no arguments:
<lang Mathematica>f[]</lang>
Calling a function with a fixed number of arguments:
<lang Mathematica>f[1,2]</lang>
Calling a function with optional arguments:
<lang Mathematica>f[1,Option1->True]</lang>
Calling a function with a variable number of arguments:
<lang Mathematica>f[1,Option1->True]
f[1,Option1->True,Option2->False]</lang>
Calling a function with named arguments:
<lang Mathematica>f[Option1->True,Option2->False]</lang>
Using a function in statement context:
<lang Mathematica>f[1,2];f[2,3]</lang>
Using a function in first-class context within an expression:
<lang Mathematica>(#^2)&[3];</lang>
The return value of a function can be formally extracted using Return[]
Built-in functions names by convention start with a capital letter.
No formal distinction between subroutines and functions.
Arguments can be passed by value or by reference.
MATLAB / Octave
<lang Matlab>
% Calling a function that requires no arguments
function a=foo();
a=4;
end;
x = foo();
% Calling a function with a fixed number of arguments
function foo(a,b,c);
%% function definition;
end;
foo(x,y,z);
% Calling a function with optional arguments
function foo(a,b,c);
if nargin<2, b=0; end;
if nargin<3, c=0; end;
%% function definition;
end;
foo(x,y);
% Calling a function with a variable number of arguments
function foo(varargin);
for k=1:length(varargin)
arg{k} = varargin{k};
end;
foo(x,y);
% Calling a function with named arguments
%% does not apply
% Using a function in statement context
%% does not apply
% Using a function in first-class context within an expression
% Obtaining the return value of a function
function [a,b]=foo();
a=4;
b='result string';
end;
[x,y] = foo();
% Distinguishing built-in functions and user-defined functions
fun = 'foo';
if (exist(fun,'builtin'))
printf('function %s is a builtin\n');
elseif (exist(fun,'file'))
printf('function %s is user-defined\n');
elseif (exist(fun,'var'))
printf('function %s is a variable\n');
else
printf('%s is not a function or variable.\n');
end
% Distinguishing subroutines and functions
% there are only scripts and functions, any function declaration starts with the keyword function, otherwise it is a script that runs in the workspace
% Stating whether arguments are passed by value or by reference
% arguments are passed by value, however Matlab has delayed evaluation, such that a copy of large data structures are done only when an element is written to.
</lang>
Nanoquery
<lang nanoquery>// function with no arguments
no_args()
// function with fixed amount of arguments
three_args(a, b, c)
// nanoquery does not support optional, variable, or named arguments
// obtaining a return value
value = returns_value()
// checking if a function called "func" is user-defined
try
type(func)
println "func is user-defined"
catch
println "func is a built-in or doesn't exist"
end</lang>
Nemerle
<lang Nemerle>// no arguments
f()
// fixed arguments
def f(a, b) { ... } // as an aside, functions defined with 'def' use type inference for parameters and return types
f(1, 'a')
// optional arguments
def f(a, b = 0) { ... }
f("hello")
f("goodbye", 2)
f("hey", b = 2) // using the name makes more sense if there's more than one optional argument, obviously
// variable number of arguments
def f(params args) { ... }
def g(a, b, params rest) { ... }
f(1, 2, 3) // arguments should all have the same type or may be coerced to a supertype
g(1.0, 2, "a", "hello")
// named arguments
f(a = 'a', b = 0)
f(b = 0, a = 'a')
f('a', b = 0) // if mixing named and unnamed args, unnamed must be first and in correct order
// statement context
if (f(foo) == 42)
WriteLine($"$foo is the meaning to life, the universe and everything.")
else WriteLine($"$foo is meaningless.")
// first class function in an expression
def a = numList.FoldLeft(f)
// obtaining return value
def a = f(3)
// distinguishing built-in from user functions
// N/A?
// distinguishing subroutines from functions
// N/A
// stating whether passed by value or by reference
// .NET distinguishes between value types and reference types; if a reference type is passed by reference (using ref or out),
// the reference is passed by reference, which would allow a method to modify the object to which the reference refers
def f(a, ref b) { ... }
mutable someVar = "hey there" // doesn't make sense to pass immutable value by ref
f(2, ref someVar)
def g(a, out b) { ... }
mutable someOtherVar // if passed by ref using 'out', the variable needn't be initialized
g(2, out someOtherVar)
// partial application
def f(a, b) { ... }
def g = f(2, _)
def h = f(_, 2)
def a = g(3) // equivalent to: def a = f(2, 3)
def b = h(3) // equivalent to: def b = f(3, 2)</lang>
Nim
Translated from Python, when possible:
<lang nim>proc no_args() =
discard
- call
no_args()
proc fixed_args(x, y) =
echo x
echo y
- calls
fixed_args(1, 2) # x=1, y=2
fixed_args 1, 2 # same call
1.fixed_args(2) # same call
proc opt_args(x=1.0) =
echo x
- calls
opt_args() # 1
opt_args(3.141) # 3.141
proc var_args(v: varargs[string, `$`]) =
for x in v: echo x
- calls
var_args(1, 2, 3) # (1, 2, 3)
var_args(1, (2,3)) # (1, (2, 3))
var_args() # ()
- Named arguments
fixed_args(y=2, x=1) # x=1, y=2
- As a statement
if true:
no_args()
proc return_something(x): int =
x + 1
var a = return_something(2)
- First-class within an expression
let x = return_something(19) + 10
let y = 19.return_something() + 10
let z = 19.return_something + 10</lang>
OCaml
- Calling a function that requires no arguments:
<lang ocaml>f ()</lang>
(In fact it is impossible to call a function without arguments, when there are no particular arguments we provide the type unit
which is a type that has only one possible value. This type is mainly made for this use.)
- Calling a function with a fixed number of arguments:
<lang ocaml>f 1 2 3</lang>
- Calling a function with optional arguments:
For a function that has this signature:
<lang ocaml>val f : ?a:int -> int -> unit</lang>
here is how to call it with or without the first argument omited:
<lang ocaml>f 10
f ~a:6 10</lang>
Due to partial application, an optional argument always has to be followed by a non-optional argument. If the function needs no additional arguments then we use the type unit
:
<lang ocaml>g ()
g ~b:1.0 ()</lang>
- Calling a function with a variable number of arguments:
This is not possible. The strong OCaml type system does not allow this.
The OCaml programmer will instead provide the variable number of arguments in a list, an array, an enumeration, a set or any structure of this kind.
(But if we really need this for a good reason, it is still possible to use a hack, like it has been done for the function Printf.printf
.)
- Calling a function with named arguments:
Named arguments are called labels.
<lang ocaml>f ~arg:3</lang>
If a variable has the same name than the label we can use this simpler syntax:
<lang ocaml>let arg = 3 in
f ~arg</lang>
- Using a function in statement context:
<lang ocaml>(* TODO *)</lang>
- Using a function in first-class context within an expression:
functions in OCaml are first-class citizen.
- Obtaining the return value of a function:
<lang ocaml>let ret = f ()
let a, b, c = f () (* if there are several returned values given as a tuple *)
let _ = f () (* if we want to ignore the returned value *)
let v, _ = f () (* if we want to ignore one of the returned value *)</lang>
- Distinguishing built-in functions and user-defined functions:
There is no difference.
- Distinguishing subroutines and functions:
OCaml only provides functions.
- Stating whether arguments are passed by value or by reference:
OCaml arguments are always passed by reference. OCaml is an impure functional language, for immutable variables there is no difference if the argument is passed by value or by reference, but for mutable variables the programmer should know that a function is able to modify it.
- How to use partial application:
Just apply less arguments than the total number of arguments.
With partial application, the arguments are applied in the same order than they are defined in the signature of the function, except if there are labeled arguments, then it is possible to use these labels to partially apply the arguments in any order.
Oforth
Oforth provides functions (global prodecure, without receiver) and methods (need a receiver).
Oforth uses RPN notation. Arguments must be on the stack before calling a function or method. So, the same syntax is use for calling a function with or without prameters.
If f is a function and c b a ares objects :
<lang Oforth>a b c f</lang>
will push c then b then a on the stack then call f. Calling f does not describe if f will use 1, 2 or 3 arguments (or none).
Oforth adds a notation to describe parameters used by a function. It is only a way to add information about which parameters will be used by f :
<lang Oforth>f(a, b, c)</lang>
Intepreter will replace this second syntax by the first one. It is only "sugar"...
<lang Oforth>a b c f
a b f(c)
a f(b, c)
f(a, b, c)</lang>
are the same call to function f and the interpreter will translate all of them into the first one. Which parameters are really used by f will depend on f implementation.
Methods need a receiver (the object on which the method will apply and the object that will pushed on th stack when self is used into the method body).
The receiver must be on the top of the stack before calling the method. If a, b, c and r are objects and m a method :
<lang Oforth>a b c r m</lang>
will call m with r as its receiver.
It is also possible to use the same "sugar" notation used by functions :
<lang Oforth>r m(a, b, c)</lang>
Ol
<lang scheme>
- 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" is provided, but it can be easily simulated using builtin associative arrays (named "ff")
(define (named-args-function args)
(print "a: " (get args 'a 8)) ; 8 is default value if no variable value given
(print "b: " (get args 'b 13)); same as above
)
(named-args-function #empty)
- ==> a
- 8
- ==> b
- 13
(named-args-function (list->ff '((a . 3))))
- ==> a
- 3
- ==> b
- 13
- or nicer (and shorter) form available from ol version 2.1
(named-args-function '{a 3})
- ==> a
- 3
- ==> b
- 13
(named-args-function '{b 7})
- ==> a
- 8
- ==> b
- 7
(named-args-function '{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
- Using a function in statement context
(let ((function (lambda (x) (* x x))))
(print (function 4))
- ==> 16
- (print (function 4))
- ==> What is 'function'?
- Obtaining the return value of a function
(define (return-value-function)
(print "ok.")
123)
(let ((result (return-value-function)))
(print result))
- ==> ok.
- ==> 123
- Obtaining the return value of a function while breaking the function execution (for example infinite loop)
(print
(call/cc (lambda (return)
(let loop ((n 0))
(if (eq? n 100)
(return (* n n)))
(loop (+ n 1))))))) ; this is infinite loop
- ==> 10000
- Is partial application possible and how
(define (make-partial-function n)
(lambda (x y)
(print (n x y)))
)
(define plus (make-partial-function +))
(define minus (make-partial-function -))
(plus 2 3)
- ==> 5
(minus 2 3)
- ==> -1
- Distinguishing built-in functions and user-defined functions
- ol has no builtin functions but only eight builtin forms
- quote, values, lambda, setq, letq, ifeq, either, values-apply.
- all other functions is "user-defined", and some of them defined in base library, for example (scheme core) defines if, or, and, zero?, length, append...
- Distinguishing subroutines and functions
- Both subroutines and functions is a functions in Ol.
- Btw, the "subroutine" has a different meaning in Ol - the special function that executes simultaneously in own context. The intersubroutine messaging mechanism is provided, sure.
- Stating whether arguments are passed by value or by reference
- The values in Ol always passed as values and objects always passed as references. If you want to pass an object copy - make a copy by yourself.
</lang>
ooRexx
This is to show how a built-in function is invoked when an internal function on the dame name in present.
<lang oorexx>say 'DATE'()
Say date()
Exit
daTe: Return 'my date' </lang>
- Output:
H:\>rexx fdate
31 Mar 2022
my date
PARI/GP
Calling a function is done in GP by writing the name of the function and the arguments, if any, in parentheses. As of version 2.5.0, function calls must use parentheses; some earlier versions allowed functions with an arity of 0 to be called without parentheses. However built-in constants (which are implicit functions of the current precision) can still be called without parentheses.
Optional arguments can be skipped, leaving commas in place. Trailing commas can be dropped.
Functions can be used when statements would be expected without change.
<lang parigp>f(); \\ zero arguments
sin(Pi/2); \\ fixed number of arguments
vecsort([5,6]) != vecsort([5,6],,4) \\ optional arguments
Str("gg", 1, "hh") \\ variable number of arguments
call(Str, ["gg", 1, "hh"]) \\ variable number of arguments in a vector
(x->x^2)(3); \\ first-class
x = sin(0); \\ get function value</lang>
Built-in functions are like user-defined functions in current versions. In older versions built-in functions cannot be passed as closures.
Most arguments are passed by reference. Some built-in functions accept arguments (e.g., flags) that are not GEN
s; these are passed by value or reference depending on their C type. See the User's Guide to the PARI Library section 5.7.3, "Parser Codes".
Pascal
see also: Delphi and Free Pascal
Calling a nullary function and obtaining its return value:
<lang pascal>foo</lang>
Calling an n-ary function (n ≥ 1) and obtaining its return value:
<lang pascal>foo(1, 'abc', true)</lang>
Following are not possible in Pascal as defined by the ISO standards (ISO 7185 and ISO 10206).
- optional arguments
- variable number of arguments
- named arguments
- using a function call as a statement
- distinguishing built-in functions and user-defined functions
- distinguishing subroutines and functions
- stating at the call site whether arguments are passed by value or by reference
- partial application
Perl
The most common syntax; simply calls the function foo on the argument(s) provided.
<lang perl>foo(); # Call foo on the null list
&foo(); # Ditto
foo($arg1, $arg2); # Call foo on $arg1 and $arg2
&foo($arg1, $arg2); # Ditto; ignores prototypes</lang>
Call foo() as a bareword. Only works after the function has been declared, which
can be done normally or with the use subs pragma.
<lang perl>foo;</lang>
Call foo() with the current values of @_<lang perl>&foo;</lang>
Call foo() with the current values of @_, discarding the previous stack frame. Not your grandfather's (harmful) goto, although the keyword can do both.<lang perl>goto &foo;</lang>
For subroutines stored in references (anonymous subroutines).<lang perl>&$fooref('foo', 'bar');
&{$fooref}('foo', 'bar');
$fooref->('foo', 'bar');</lang>
Phix
Phix has three kinds of routines: procedure, function, and type. A procedure does not return a value, whereas a function does.
A type is a specialised kind of function that permits declarations of instances which are automatically validated whenever they are changed, further a type routine always returns either true or false.
- Phix does not allow implicit discard of function results. The explicit discard statement takes the form
{} = myfunction()
- This is in fact a simple contraction of standard multiple assigment (which can be nested as deeply as you like):
{cities,populations} = columnize(muncipalities)
{{},populations} = columnize(muncipalities) -- discard result[1]
{cities,{}} = columnize(muncipalities) -- discard result[2]
{cities} = columnize(muncipalities) -- ""
- Calling a function with no parameters still requires the "()" empty argument list.
- Optional arguments are denoted simply by the presence of a default, and must be grouped on the right:
function myfunction(integer a, string b="default")
return {a,b}
end function
--? myfunction() -- illegal, compile-time error
?myfunction(1) -- displays {1,"default"}
?myfunction(2,"that") -- displays {2,"that"}
- Sequence parameters can be of any length, which is another way to implement optional/variable number of arguments.
- Named arguments can be specified in any order, with an error if any non-optional parameters are missing:
?myfunction(b:="then",a:=3) -- displays {3,"then"}
--?myfunction(b:="though") -- compile-time error
- The programmer is free to use either positional parameters or named parameters, or a mixture of both (with positional parameters first).
- Phix support first-class functions directly, as integers, along with an older routine_id mechanism:
constant integer r_myfunction = routine_id("myfunction"),
first_class = myfunction
?call_func(r_myfunction,{1}) -- displays {1,"default"}
?call_func(myfunction,{1}) -- ""
?call_func(first_class,{1}) -- ""
?first_class(1) -- ""
The value of r_my_func can be passed as an argument to any routine, or stored in a table, and invoked in a similar fashion.
Note however that for performance reasons some builtins do not have a proper routine_id; if you need one you must write a trivial one-line wrapper.
(For a full list, see psym.e/syminit() calls to AutoAsm(), whereas calls to initialAutoEntry() therein indicate builtins that can have routine_ids.)
(Routines that do not have a proper routine_id do not support named parameters either.)
(One day the compiler may be enhanced to automatically create one-line wrappers as needed, but that is quite near the end of a fairly long to-do list.)
- Partial application is usually achieved through a single variable-length "user_data" parameter within a call_func() expression.
- All arguments are passed by reference with copy-on-write semantics: to modify the value of a parameter you must both return and assign it, as in:
s = append(s,item)
- Implicit forward calls are supported, as are optional explicit forward declarations, which can occasionally cure compilation error messages.
Phixmonti
<lang Phixmonti>def saludo
"Hola mundo" print nl
enddef
saludo
getid saludo exec</lang>
PicoLisp
When calling a funcion in PicoLisp directly (does this mean "in a statement context"?), it is always surrounded by parentheses, with or without arguments, and for any kind of arguments (evaluated or not):
<lang PicoLisp>(foo)
(bar 1 'arg 2 'mumble)</lang>
When a function is used in a "first class context" (e.g. passed to another function), then it is not yet called. It is simply used. Technically, a function can be either a number (a built-in function) or a list (a Lisp-level function) in PicoLisp):
<lang PicoLisp>(mapc println Lst) # The value of 'printlin' is a number
(apply '((A B C) (foo (+ A (* B C)))) (3 5 7)) # A list is passed</lang>
Any argument to a function may be evaluated or not, depending on the function. For example, 'setq' evaluates every second argument
<lang PicoLisp>(setq A (+ 3 4) B (* 3 4))</lang>
i.e. the first argument 'A' is not evaluated, the second evaluates to 7, 'B' is not evaluated, then the fourth evaluates to 12.
PureBasic
<lang PureBasic>Procedure Saludo()
PrintN("Hola mundo!")
EndProcedure
Procedure.s Copialo(txt.s, siNo.b, final.s = "")
Define nuevaCadena.s, resul.s
For cont.b = 1 To siNo
nuevaCadena + txt
Next
Resul = Trim(nuevaCadena) + final
ProcedureReturn resul
EndProcedure
Procedure testNumeros(a.i, b.i, c.i = 0)
PrintN(Str(a) + #TAB$ + Str(b) + #TAB$ + Str(c))
EndProcedure
Procedure testCadenas(txt.s)
For cont.b = 1 To Len(txt)
Print(Mid(txt,cont,1))
Next cont
EndProcedure
OpenConsole()
Saludo()
PrintN(Copialo("Saludos ", 6))
PrintN(Copialo("Saludos ", 3, "!!"))
PrintN("")
testNumeros(1, 2, 3)
testNumeros(1, 2)
PrintN("")
testCadenas("1, 2, 3, 4, cadena, 6, 7, 8, \'incluye texto\'")
Input()
CloseConsole()</lang>
- Output:
Same as FreeBASIC entry.
Python
Under the hood all Python function/method parameters are named. All arguments can be passed as name=value pairs or as a dictionary containing such pairs using the myfunc(**key_args) (apply over dictionary) syntax). One can also "apply" a function over a sequence of arguments using the syntax: myfunc(*args) as noted in comments below. Parameters can be mixed so long parameters with default values (optional arguments) follow any "positional" (required) parameters, and catchall parameter (*args) follow those, and any "keyword arguments' parameter" is last. (Any function can only have up to one "catchall" or *args' parameter and up to one "keyword args" **kwargs parameter).
<lang python>def no_args():
pass
- call
no_args()
def fixed_args(x, y):
print('x=%r, y=%r' % (x, y))
- call
fixed_args(1, 2) # x=1, y=2
- Can also called them using the parameter names, in either order:
fixed_args(y=2, x=1)
- Can also "apply" fixed_args() to a sequence:
myargs=(1,2) # tuple
fixed_args(*myargs)
def opt_args(x=1):
print(x)
- calls
opt_args() # 1
opt_args(3.141) # 3.141
def var_args(*v):
print(v)
- calls
var_args(1, 2, 3) # (1, 2, 3)
var_args(1, (2,3)) # (1, (2, 3))
var_args() # ()
- Named arguments
fixed_args(y=2, x=1) # x=1, y=2
- As a statement
if 1:
no_args()
- First-class within an expression
assert no_args() is None
def return_something():
return 1
x = return_something()
def is_builtin(x):
print(x.__name__ in dir(__builtins__))
- calls
is_builtin(pow) # True
is_builtin(is_builtin) # False
- Very liberal function definition
def takes_anything(*args, **kwargs):
for each in args:
print(each)
for key, value in sorted(kwargs.items()):
print("%s:%s" % (key, value))
# Passing those to another, wrapped, function:
wrapped_fn(*args, **kwargs)
# (Function being wrapped can have any parameter list
# ... that doesn't have to match this prototype)
- A subroutine is merely a function that has no explicit
- return statement and will return None.
- Python uses "Call by Object Reference".
- See, for example, http://www.python-course.eu/passing_arguments.php
- For partial function application see:
- http://rosettacode.org/wiki/Partial_function_application#Python</lang>
QBasic
<lang qbasic>FUNCTION Copialo$ (txt$, siNo, final$)
DIM nuevaCadena$
FOR cont = 1 TO siNo
nuevaCadena$ = nuevaCadena$ + txt$
NEXT cont
Copialo$ = LTRIM$(RTRIM$(nuevaCadena$)) + final$
END FUNCTION
SUB Saludo
PRINT "Hola mundo!"
END SUB
SUB testCadenas (txt$)
FOR cont = 1 TO LEN(txt$)
PRINT MID$(txt$, cont, 1); "";
NEXT cont
END SUB
SUB testNumeros (a, b, c)
PRINT a, b, c
END SUB
CALL Saludo
PRINT Copialo$("Saludos ", 6, "")
PRINT Copialo$("Saludos ", 3, "!!")
PRINT
CALL testNumeros(1, 2, 3)
CALL testNumeros(1, 2, 0)
PRINT
CALL testCadenas("1, 2, 3, 4, cadena, 6, 7, 8, \'incluye texto\'")</lang>
- Output:
Igual que la entrada de FreeBASIC.
Quackery
Words in Quackery take zero or more arguments from the (programmer accessible) data stack, and return zero or more results to the data stack.
In this dialogue in the Quackery shell, /mod
takes two arguments and returns two results, pack
takes three arguments (the topmost argument on the stack (2
) specifies how many more arguments it is to take from the stack), echo
takes one argument and returns no results, and cr
takes no arguments and returns no results.
/O> 123 7 /mod
...
Stack: 17 4
/O> 2 pack
...
Stack: [ 17 4 ]
/O> echo cr
...
[ 17 4 ]
Stack empty.
Words can also take one or more items (nests, words or numbers) and arbitrary strings of text following the word as arguments. For example, times
performs the item following it a specified number of times, and say
echoes the string following it to the terminal.
/O> 4 times [ 3 times [ say "badger" sp ] cr ]
... 2 times [ say "mushroom" sp ] cr
...
badger badger badger
badger badger badger
badger badger badger
badger badger badger
mushroom mushroom
Stack empty.
R
Translated from Python, when possible.
<lang rsplus>### Calling a function that requires no arguments
no_args <- function() NULL
no_args()
- Calling a function with a fixed number of arguments
fixed_args <- function(x, y) print(paste("x=", x, ", y=", y, sep=""))
fixed_args(1, 2) # x=1, y=2
fixed_args(y=2, x=1) # y=1, x=2
- Calling a function with optional arguments
opt_args <- function(x=1) x
opt_args() # x=1
opt_args(3.141) # x=3.141
- Calling a function with a variable number of arguments
var_args <- function(...) print(list(...))
var_args(1, 2, 3)
var_args(1, c(2,3))
var_args()
- Calling a function with named arguments
fixed_args(y=2, x=1) # x=1, y=2
- Using a function in statement context
if (TRUE) no_args()
- Using a function in first-class context within an expression
print(no_args)
- Obtaining the return value of a function
return_something <- function() 1
x <- return_something()
x
- Distinguishing built-in functions and user-defined functions
- Not easily possible. See
- http://cran.r-project.org/doc/manuals/R-ints.html#g_t_002eInternal-vs-_002ePrimitive
- for details.
- Distinguishing subroutines and functions
- No such distinction.
- Stating whether arguments are passed by value or by reference
- Pass by value.
- Is partial application possible and how
- Yes, see http://rosettacode.org/wiki/Partial_function_application#R</lang>
Racket
<lang Racket>
- lang racket
- Calling a function that requires no arguments
(foo)
- Calling a function with a fixed number of arguments
(foo 1 2 3)
- Calling a function with optional arguments
- Calling a function with a variable number of arguments
(foo 1 2 3) ; same in both cases
- Calling a function with named arguments
(foo 1 2 #:x 3) ; using #:keywords for the names
- Using a function in statement context
- Using a function in first-class context within an expression
- Obtaining the return value of a function
- -> Makes no sense for Racket, as well as most other functional PLs
- Distinguishing built-in functions and user-defined functions
(primitive? foo)
- but this is mostly useless, since most of Racket is implemented in
- itself
- Distinguishing subroutines and functions
- -> No difference, though `!' is an idiomatic suffix for names of
- side-effect functions, and they usually return (void)
- Stating whether arguments are passed by value or by reference
- -> Always by value, but it's possible to implement languages with
- other argument passing styles, including passing arguments by
- reference (eg, there is "#lang algol60")
- Is partial application possible and how
(curry foo 1 2) ; later apply this on 3
(λ(x) (foo 1 2 x)) ; a direct way of doing the same
</lang>
Raku
(formerly Perl 6)
Theory
Fundamentally, nearly everything you do in Raku is a function call if you look hard enough.
At the lowest level, a function call merely requires a reference to any
kind of invokable object, and a call to its postcircumfix:<( )> method.
However, there are various forms of sugar and indirection that you
can use to express these function calls differently. In particular,
operators are all just sugar for function calls.
Calling a function that requires no arguments:
<lang perl6>foo # as list operator
foo() # as function
foo.() # as function, explicit postfix form
$ref() # as object invocation
$ref.() # as object invocation, explicit postfix
&foo() # as object invocation
&foo.() # as object invocation, explicit postfix
- ($name)() # as symbolic ref</lang>
Calling a function with exactly one argument:
<lang perl6>foo 1 # as list operator
foo(1) # as named function
foo.(1) # as named function, explicit postfix
$ref(1) # as object invocation (must be hard ref)
$ref.(1) # as object invocation, explicit postfix
1.$foo # as pseudo-method meaning $foo(1) (hard ref only)
1.$foo() # as pseudo-method meaning $foo(1) (hard ref only)
1.&foo # as pseudo-method meaning &foo(1) (is hard foo)
1.&foo() # as pseudo-method meaning &foo(1) (is hard foo)
1.foo # as method via dispatcher
1.foo() # as method via dispatcher
1."$name"() # as method via dispatcher, symbolic
+1 # as operator to prefix:<+> function</lang>
Method calls are included here because they do eventually dispatch to a true
function via a dispatcher. However, the dispatcher in question is not going
to dispatch to the same set of functions that a function call of that name
would invoke. That's why there's a dispatcher, after all. Methods are declared
with a different keyword, method, in Raku, but all that does is
install the actual function into a metaclass. Once it's there, it's merely
a function that expects its first argument to be the invocant object. Hence we
feel justified in including method call syntax as a form of indirect function call.
Operators like + also go through a dispatcher, but in this case it is
multiply dispatched to all lexically scoped candidates for the function. Hence
the candidate list is bound early, and the function itself can be bound early
if the type is known. Raku maintains a clear distinction between early-bound
linguistic constructs that force Perlish semantics, and late-bound OO dispatch
that puts the objects and/or classes in charge of semantics. (In any case, &foo,
though being a hard ref to the function named "foo", may actually be a ref to
a dispatcher to a list of candidates that, when called, makes all the candidates behave as a single unit.)
Calling a function with exactly two arguments:
<lang perl6>foo 1,2 # as list operator
foo(1,2) # as named function
foo.(1,2) # as named function, explicit postfix
$ref(1,2) # as object invocation (must be hard ref)
$ref.(1,2) # as object invocation, explicit postfix
1.$foo: 2 # as pseudo-method meaning $foo(1,2) (hard ref only)
1.$foo(2) # as pseudo-method meaning $foo(1,2) (hard ref only)
1.&foo: 2 # as pseudo-method meaning &foo(1,2) (is hard foo)
1.&foo(2) # as pseudo-method meaning &foo(1,2) (is hard foo)
1.foo: 2 # as method via dispatcher
1.foo(2) # as method via dispatcher
1."$name"(2) # as method via dispatcher, symbolic
1 + 2 # as operator to infix:<+> function</lang>
Optional arguments don't look any different from normal arguments.
The optionality is all on the binding end.
Calling a function with a variable number of arguments (varargs):
<lang perl6>foo @args # as list operator
foo(@args) # as named function
foo.(@args) # as named function, explicit postfix
$ref(@args) # as object invocation (must be hard ref)
$ref.(@args) # as object invocation, explicit postfix
1.$foo: @args # as pseudo-method meaning $foo(1,@args) (hard ref)
1.$foo(@args) # as pseudo-method meaning $foo(1,@args) (hard ref)
1.&foo: @args # as pseudo-method meaning &foo(1,@args)
1.&foo(@args) # as pseudo-method meaning &foo(1,@args)
1.foo: @args # as method via dispatcher
1.foo(@args) # as method via dispatcher
1."$name"(@args) # as method via dispatcher, symbolic
@args X @blargs # as list infix operator to infix:<X></lang>
Note: whether a function may actually be called with a variable number of arguments depends entirely
on whether a signature accepts a list at that position in the argument list, but
describing that is not the purpose of this task. Suffice to say that we assume here that the
foo function is declared with a signature of the form (*@params). The calls above might be interpreted as having a single array argument if the signature indicates a normal parameter instead of a variadic one. What you cannot do in Raku (unlike Perl 5) is pass an array as several fixed arguments. By default it must either represent a single argument, or be part of a variadic list. You can force the extra level of argument list interpolation using a prefix | however:
<lang perl6>my @args = 1,2,3;
foo(|@args); # equivalent to foo(1,2,3)</lang>
Calling a function with named arguments:
<lang perl6>foo :a, :b(4), :!c, d => "stuff"
foo(:a, :b(4), :!c, d => "stuff")</lang>
...and so on. Operators may also be called with named arguments, but only
colon adverbials are allowed:
<lang perl6>1 + 1 :a :b(4) :!c :d("stuff") # calls infix:<+>(1,1,:a, :b(4), :!c, d => "stuff")</lang>
Using a function in statement context:
<lang perl6>foo(); bar(); baz(); # evaluate for side effects</lang>
Using a function in first class context within an expression:
<lang perl6>1 / find-a-func(1,2,3)(4,5,6) ** 2;</lang>
Obtaining the return value of a function:
<lang perl6>my $result = somefunc(1,2,3) + 2;</lang>
There is no difference between calling builtins and user-defined functions and operators (or
even control stuctures). This was a major design goal of Raku, and apart from a very few
low-level primitives, all of Raku can be written in Raku.
There is no difference between calling subroutines and functions in Raku, other than that
calling a function in void context that has no side effects is likely to get you a "Useless use of..." warning.
And, of course, the fact that pure functions can participate in more optimizations such as constant folding.
By default, arguments are passed readonly, which allows the implementation to decide whether pass-by-reference or pass-by-value is more efficient on a case-by-case basis. Explicit lvalue, reference, or copy semantics may be requested on a parameter-by-parameter basis, and the entire argument list may be processed raw if that level of control is needed.
Practice
Demonstrating each of the above-mentioned function calls with actual running code, along with the various extra definitions required to make them work (in certain cases). Arguments are checked, and function name / run-sequence number are displayed upon success.
<lang perl6>{
state $n;
multi f () { print ' f' ~ ++$n }
multi f ($a) { die if 1 != $a; print ' f' ~ ++$n }
multi f ($a,$b) { die if 3 != $a+$b; print ' f' ~ ++$n }
multi f (@a) { die if @a != [2,3,4]; print ' f' ~ ++$n }
multi f ($a,$b,$c) { die if 2 != $a || 4 != $c; print ' f' ~ ++$n }
sub g ($a,*@b) { die if @b != [2,3,4] || 1 != $a; print ' g' ~ ++$n }
my \i = -> { print ' i' ~ ++$n }
my \l = -> $a { die if 1 != $a; print ' l' ~ ++$n }
my \m = -> $a,$b { die if 1 != $a || 2 != $b; print ' m' ~ ++$n }
my \n = -> @a { die if @a != [2,3,4]; print ' n' ~ ++$n }
Int.^add_method( 'j', method ()
{ die if 1 != self; print ' j' ~ ++$n } );
Int.^add_method( 'k', method ($a)
{ die if 1 != self || 2 != $a; print ' k' ~ ++$n } );
Int.^add_method( 'h', method (@a)
{ die if @a != [2,3,4] || 1 != self; print ' h' ~ ++$n } );
my $ref = &f; # soft ref
my $f := &f; # hard ref
my $g := &g; # hard ref
my $f-sym = '&f'; # symbolic ref
my $g-sym = '&g'; # symbolic ref
my $j-sym = 'j'; # symbolic ref
my $k-sym = 'k'; # symbolic ref
my $h-sym = 'h'; # symbolic ref
- Calling a function with no arguments:
f; # 1 as list operator
f(); # 2 as function
i.(); # 3 as function, explicit postfix form # defined via pointy-block
$ref(); # 4 as object invocation
$ref.(); # 5 as object invocation, explicit postfix
&f(); # 6 as object invocation
&f.(); # 7 as object invocation, explicit postfix
- ($f-sym)(); # 8 as symbolic ref
- Calling a function with exactly one argument:
f 1; # 9 as list operator
f(1); # 10 as named function
l.(1); # 11 as named function, explicit postfix # defined via pointy-block
$f(1); # 12 as object invocation (must be hard ref)
$ref.(1); # 13 as object invocation, explicit postfix
1.$f; # 14 as pseudo-method meaning $f(1) (hard ref only)
1.$f(); # 15 as pseudo-method meaning $f(1) (hard ref only)
1.&f; # 16 as pseudo-method meaning &f(1) (is hard f)
1.&f(); # 17 as pseudo-method meaning &f(1) (is hard f)
1.j; # 18 as method via dispatcher # requires custom method, via 'Int.^add_method'
1.j(); # 19 as method via dispatcher
1."$j-sym"(); # 20 as method via dispatcher, symbolic
- Calling a function with exactly two arguments:
f 1,2; # 21 as list operator
f(1,2); # 22 as named function
m.(1,2); # 23 as named function, explicit postfix # defined via pointy-block
$ref(1,2); # 24 as object invocation (must be hard ref)
$ref.(1,2); # 25 as object invocation, explicit postfix
1.$f: 2; # 26 as pseudo-method meaning $f(1,2) (hard ref only)
1.$f(2); # 27 as pseudo-method meaning $f(1,2) (hard ref only)
1.&f: 2; # 28 as pseudo-method meaning &f(1,2) (is hard f)
1.&f(2); # 29 as pseudo-method meaning &f(1,2) (is hard f)
1.k: 2; # 30 as method via dispatcher # requires custom method, via 'Int.^add_method'
1.k(2); # 31 as method via dispatcher
1."$k-sym"(2); # 32 as method via dispatcher, symbolic
- Calling a function with a variable number of arguments (varargs):
my @args = 2,3,4;
f @args; # 33 as list operator
f(@args); # 34 as named function
n.(@args); # 35 as named function, explicit postfix # defined via pointy-block
$ref(@args); # 36 as object invocation (must be hard ref)
$ref.(@args); # 37 as object invocation, explicit postfix
1.$g: @args; # 38 as pseudo-method meaning $f(1,@args) (hard ref)
1.$g(@args); # 39 as pseudo-method meaning $f(1,@args) (hard ref)
1.&g: @args; # 40 as pseudo-method meaning &f(1,@args)
1.&g(@args); # 41 as pseudo-method meaning &f(1,@args)
1.h: @args; # 42 as method via dispatcher # requires custom method, via 'Int.^add_method'
1.h(@args); # 43 as method via dispatcher
1."$h-sym"(@args); # 44 as method via dispatcher, symbolic
f(|@args); # 45 equivalent to f(1,2,3)
}</lang>
- Output:
f1 f2 i3 f4 f5 f6 f7 f8 f9 f10 l11 f12 f13 f14 f15 f16 f17 j18 j19 j20 f21 f22 m23 f24 f25 f26 f27 f28 f29 k30 k31 k32 f33 f34 n35 f36 f37 g38 g39 g40 g41 h42 h43 h44 f45
REXX
version 1
<lang rexx>/*REXX pgms demonstrates various methods/approaches of invoking/calling a REXX function.*/
/*╔════════════════════════════════════════════════════════════════════╗
║ Calling a function that REQUIRES no arguments. ║
║ ║
║ In the REXX language, there is no way to require the caller to not ║
║ pass arguments, but the programmer can check if any arguments were ║
║ (or weren't) passed. ║
╚════════════════════════════════════════════════════════════════════╝*/
yr= yearFunc() /*the function name is caseless if it isn't */
/*enclosed in quotes (') or apostrophes (").*/
say 'year=' yr
exit /*stick a fork in it, we're all done. */
yearFunc: procedure /*function ARG returns the # of args.*/
errmsg= '***error***' /*an error message eyecatcher string. */
if arg() \== 0 then say errmsg "the YEARFUNC function won't accept arguments."
return left( date('Sorted'), 3)</lang>
<lang rexx> /*╔════════════════════════════════════════════════════════════════════╗
║ Calling a function with a fixed number of arguments. ║
║ ║
║ I take this to mean that the function requires a fixed number of ║
║ arguments. As above, REXX doesn't enforce calling (or invoking) ║
║ a (any) function with a certain number of arguments, but the ║
║ programmer can check if the correct number of arguments have been ║
║ specified (or not). ║
║ In some languages, these are known as "generic" functions. ║
╚════════════════════════════════════════════════════════════════════╝*/
ggg= FourFunc(12, abc, 6+q, zz%2, 'da 5th disagreement')
say 'ggg squared=' ggg**2
exit /*stick a fork in it, we're all done. */
FourFunc: procedure; parse arg a1,a2,a3 /*obtain the first three arguments. */
a4= arg(4) /*another way to obtain the 4th arg. */
errmsg= '***error***' /*an error message eyecatcher string. */
if arg() \== 4 then do
say err "FourFunc function requires 4 arguments,"
say err "but instead it found" arg() 'arguments.'
exit 13 /*exit function with a RC of 13*/
end
return a1 + a2 + a3 + a4</lang>
<lang rexx> /*╔════════════════════════════════════════════════════════════════════╗
║ Calling a function with optional arguments. ║
║ ║
║ Note that not passing an argument isn't the same as passing a null ║
║ argument (a REXX variable whose value is length zero). ║
╚════════════════════════════════════════════════════════════════════╝*/
x= 12; w= x/2; y= x**2; z= x//7 /* z is x modulo seven. */
say 'sum of w, x, y, & z=' SumIt(w,x,y,,z) /*pass five args, the 4th arg is "null"*/
exit /*stick a fork in it, we're all done. */
SumIt: procedure
$= 0 /*initialize the sum to zero. */
do j=1 for arg() /*obtain the sum of a number of args. */
if arg(j,'E') then $= $ + arg(j) /*the Jth arg may have been omitted. */
end /*j*/
return $</lang>
<lang rexx> /*╔════════════════════════════════════════════════════════════════════╗
║ Calling a function with a variable number of arguments. ║
║ ║
║ This situation isn't any different then the previous example. ║
║ It's up to the programmer to code how to utilize the arguments. ║
╚════════════════════════════════════════════════════════════════════╝*/
/*╔════════════════════════════════════════════════════════════════════╗
║ Calling a function with named arguments. ║
║ ║
║ REXX allows almost anything to be passed, so the following is one ║
║ way this can be accomplished. ║
╚════════════════════════════════════════════════════════════════════╝*/
what= parserFunc('name=Luna', "gravity=.1654", 'moon=yes')
say 'name=' common.name
gr= common.gr
say 'gravity=' gr
exit /*stick a fork in it, we're all done. */
parseFunc: procedure expose common.
do j=1 for arg()
parse var arg(j) name '=' val
upper name /*uppercase it.*/
call value 'COMMON.'name,val
end
return arg()</lang>
<lang rexx> /*╔════════════════════════════════════════════════════════════════════╗
║ Calling a function in statement context. ║
║ ║
║ REXX allows functions to be called (invoked) two ways, the first ║
║ example (above) is calling a function in statement context. ║
╚════════════════════════════════════════════════════════════════════╝*/
/*╔════════════════════════════════════════════════════════════════════╗
║ Calling a function in within an expression. ║
║ ║
║ This is a variant of the first example. ║
╚════════════════════════════════════════════════════════════════════╝*/
yr= yearFunc() + 20
say 'two decades from now, the year will be:' yr
exit /*stick a fork in it, we're all done. */</lang>
<lang rexx> /*╔════════════════════════════════════════════════════════════════════╗
║ Obtaining the return value of a function. ║
║ ║
║ There are 2 ways to get the (return) value (RESULT) of a function. ║
╚════════════════════════════════════════════════════════════════════╝*/
currYear= yearFunc()
say 'the current year is' currYear
call yearFunc
say 'the current year is' result /*result can be RESULT, it is caseless.*/</lang>
<lang rexx> /*╔════════════════════════════════════════════════════════════════════╗
║ Distinguishing built-in functions and user-defined functions. ║
║ ║
║ One objective of the REXX language is to allow the user to use any ║
║ function (or subroutine) name whether or not there is a built-in ║
║ function with the same name (there isn't a penality for this). ║
╚════════════════════════════════════════════════════════════════════╝*/
/*date: as in going out with someone. */
qqq= date() /*number of real dates that Bob was on.*/
/*hopefully, it accurately counts dates*/
say "Bob's been out" qqq 'times.'
www= 'DATE'("USA") /*returns date in format mm/dd/yyyy */
/*any function in quotes is external. */
exit /*stick a fork in it, we're all done. */
date: return 4 /*Bob only "went out" 4 times, no need */
/* to actually count, he quit after 4. */</lang>
<lang rexx> /*╔════════════════════════════════════════════════════════════════════╗
║ Distinguishing subroutines and functions. ║
║ ║
║ There is no programmatic difference between subroutines and ║
║ functions if the subroutine returns a value (which effectively ║
║ makes it a function). REXX allows you to call a function as if ║
║ it were a subroutine. ║
╚════════════════════════════════════════════════════════════════════╝*/
/*╔════════════════════════════════════════════════════════════════════╗
║ In REXX, all arguments are passed by value, never by name, but it ║
║ is possible to accomplish this if the variable's name is passed ║
║ and the subroutine/function could use the built-in-function VALUE ║
║ to retrieve the variable's value. ║
╚════════════════════════════════════════════════════════════════════╝*/
/*╔════════════════════════════════════════════════════════════════════╗
║ In the REXX language, partial application is possible, depending ║
║ how partial application is defined; I prefer the 1st definition ║
║ (as per the "discussion" for "Partial Function Application" task: ║
║ 1. The "syntactic sugar" that allows one to write some examples ║
║ are: map (f 1 9) [1..9] ║
║ or: map (f(1,_,9)) [1, ..., 9] ║
╚════════════════════════════════════════════════════════════════════╝*/</lang>
version 2
<lang rexx>/* 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 doesnt 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</lang>
- Output:
f1 doesn't need an argument
f2: Sum of 3 arguments: 6
f2: Invalid invocation: 4 arguments. Needed: 3
f3: Argument 2 omitted
f3: Argument 3 omitted
f3 sum= 5
f4: Sum of 2 arguments is 3
f4: Sum of 3 arguments is 6
f5: Argument 1 (A) contains 4700
f5: Argument 2 (B) contains 11
f5: sum=4711
f6: dir ft.rex
Datenträger in Laufwerk D: ist DATA
Volumeseriennummer: B0F8-F2C3
Verzeichnis von D:\
29.07.2013 00:33 2.661 ft.rex
1 Datei(en), 2.661 Bytes
0 Verzeichnis(se), 251.050.979.328 Bytes frei
f7 returns 7
f8 returns arg(1)+arg(2)
f8 returns arg(1)+arg(2)
3 = 3
date is my date function
20130729 07/29/2013
fa returns arg(1)+arg(2)
fa returns arg(1)+arg(2)
33 = 3
fb: 1,2
fb: 1,2
Syntax raised in line 39
x=fb(1,2)
rc=44 (Function or message did not return data)
fb cannot be invoked as function (it does not return a value)
Ring
<lang ring>
hello()
func hello
see "Hello from function" + nl
</lang>
<lang ring>
first() second()
func first see "message from the first function" + nl
func second see "message from the second function" + nl
</lang>
<lang ring>
sum(3,5) sum(1000,2000)
func sum x,y see x+y+nl
</lang>
<lang ring>
- this program will print the hello world message first then execute the main function
See "Hello World!" + nl
func main
see "Message from the main function" + nl
</lang>
Ruby
Ruby does not have functions, but Ruby classes have "methods" which are equivalent.
The parentheses around the arguments are optional (definition and call).
A method returns the last expression that was evaluated in the body of the method.
The return keyword can be used to make it explicit that a method returns a value.
- Calling a function that requires no arguments
- <lang ruby>def foo() p "foo" end
foo #=> "foo"
foo() #=> "foo"</lang>
- Calling a function with a fixed number of arguments
- <lang ruby>def foo arg; p arg end # one argument
foo(1) #=> 1
foo "1" #=> "1"
foo [0,1,2] #=> [0, 1, 2] (one Array)</lang>
- Calling a function with optional arguments
- <lang ruby>def foo(x=0, y=x, flag=true) p [x,y,flag] end
foo #=> [0, 0, true]
foo(1) #=> [1, 1, true]
foo(1,2) #=> [1, 2, true]
foo 1,2,false #=> [1, 2, false]</lang>
- Calling a function with a variable number of arguments
- <lang ruby>def foo(*args) p args end
foo #=> []
foo(1,2,3,4,5) #=> [1, 2, 3, 4, 5]</lang>
- Calling a function with named arguments
- <lang ruby>def foo(id:0, name:"", age:0) p [id, name, age] end
foo(age:22, name:"Tom") #=> [0, "Tom", 22]</lang>
- Using a function in statement context
- ?
- Using a function in first-class context within an expression
- The method is not a first-class function. However, there is Proc object.
- Obtaining the return value of a function
- <lang ruby>def foo(a,b) a + b end
bar = foo 10,20
p bar #=> 30
p foo("abc","def") #=> "abcdef"
- return multiple values
def sum_and_product(a,b) return a+b,a*b end
x,y = sum_and_product(3,5)
p x #=> 8
p y #=> 15</lang>
- Distinguishing built-in functions and user-defined functions
- There is no distinction.
- Distinguishing subroutines and functions
- Subroutine and function don't exist at ruby. It is only a method that there is.
- The Kernel module is included by class Object, so its methods are available in every Ruby object.
- These methods are called without a receiver and thus can be called in functional form.
- <lang ruby>puts "OK!" # Kernel#puts
raise "Error input" # Kernel#raise
Integer("123") # Kernel#Integer
rand(6) # Kernel#rand
throw(:exit) # Kernel#throw
- method which can be seen like a reserved word.
attr_accessor # Module#attr_accessor
include # Module#include
private # Module#private
require # Kernel#require
loop { } # Kernel#loop</lang>
- Stating whether arguments are passed by value or by reference
- passed by value of object reference.
- Is partial application possible and how
- However something similar can be done, see Partial function application#Ruby
- Others
- Block Argument:
- The block argument sends a closure from the calling scope to the method.
- The block argument is always last when sending a message to a method. A block is sent to a method using
do ... end
or { ... }
.
- <lang ruby>class Array
def sum(init=0, &blk)
if blk
inject(init){|s, n| s + blk.call(n)}
else
inject(init){|s, n| s + n}
end
end
end
ary = [1,2,3,4,5]
p ary.sum #=> 15
p ary.sum(){|n| (-n).to_s} #=> "-1-2-3-4-5"
p (ary.sum do |n| n * n end) #=> 55</lang>
- Splat operator:
- You can turn an Array into an argument list with * (or splat) operator.
- <lang ruby>def foo(a,b,c) p [a,b,c] end
args = [1,2,3]
foo *args #=> [1, 2, 3]
args = [1,2]
foo(0,*args) #=> [0, 1, 2]</lang>
- Syntax sugar:
- In Ruby, many operators are actually method calls.
- <lang ruby># return value substance
i = 3
p 1 + i #=> 4 1.+(i)
p i < 5 #=> true i.<(5)
p 2 ** i #=> 8 2.**(i)
p -i #=> -3 i.-@()
a = [1,2,3]
p a[0] #=> 1 a.[](0)
a[2] = "0" # a.[]=(2,"0")
p a << 5 #=> [1, 2, "0", 5] a.<<(5)
p a & [4,2] #=> [2] a.&([4,2])
p "abcde"[1..3] #=> "bcd" "abcde".[](1..3)
p "%2d %4s" % [1,"xyz"] #=> " 1 xyz" "%2d %4s".%([1,"xyz"])</lang>
- Method call which was displayed in the comment is usable actually.
Rust
<lang rust>fn main() {
// Rust has a lot of neat things you can do with functions: let's go over the basics first
fn no_args() {}
// Run function with no arguments
no_args();
// Calling a function with fixed number of arguments.
// adds_one takes a 32-bit signed integer and returns a 32-bit signed integer
fn adds_one(num: i32) -> i32 {
// the final expression is used as the return value, though `return` may be used for early returns
num + 1
}
adds_one(1);
// Optional arguments
// The language itself does not support optional arguments, however, you can take advantage of
// Rust's algebraic types for this purpose
fn prints_argument(maybe: Option<i32>) {
match maybe {
Some(num) => println!("{}", num),
None => println!("No value given"),
};
}
prints_argument(Some(3));
prints_argument(None);
// You could make this a bit more ergonomic by using Rust's Into trait
fn prints_argument_into(maybe: I)
where I: Into<Option<i32>>
{
match maybe.into() {
Some(num) => println!("{}", num),
None => println!("No value given"),
};
}
prints_argument_into(3);
prints_argument_into(None);
// Rust does not support functions with variable numbers of arguments. Macros fill this niche
// (println! as used above is a macro for example)
// Rust does not support named arguments
// We used the no_args function above in a no-statement context
// Using a function in an expression context
adds_one(1) + adds_one(5); // evaluates to eight
// Obtain the return value of a function.
let two = adds_one(1);
// In Rust there are no real built-in functions (save compiler intrinsics but these must be
// manually imported)
// In rust there are no such thing as subroutines
// In Rust, there are three ways to pass an object to a function each of which have very important
// distinctions when it comes to Rust's ownership model and move semantics. We may pass by
// value, by immutable reference, or mutable reference.
let mut v = vec![1, 2, 3, 4, 5, 6];
// By mutable reference
fn add_one_to_first_element(vector: &mut Vec<i32>) {
vector[0] += 1;
}
add_one_to_first_element(&mut v);
// By immutable reference
fn print_first_element(vector: &Vec<i32>) {
println!("{}", vector[0]);
}
print_first_element(&v);
// By value
fn consume_vector(vector: Vec<i32>) {
// We can do whatever we want to vector here
}
consume_vector(v);
// Due to Rust's move semantics, v is now inaccessible because it was moved into consume_vector
// and was then dropped when it went out of scope
// Partial application is not possible in rust without wrapping the function in another
// function/closure e.g.:
fn average(x: f64, y: f64) -> f64 {
(x + y) / 2.0
}
let average_with_four = |y| average(4.0, y);
average_with_four(2.0);
}</lang>
Scala
<lang Scala>def ??? = throw new NotImplementedError // placeholder for implementation of hypothetical methods
def myFunction0() = ???
myFunction0() // function invoked with empty parameter list
myFunction0 // function invoked with empty parameter list omitted
def myFunction = ???
myFunction // function invoked with no arguments or empty arg list
/* myFunction() */ // error: does not take parameters
def myFunction1(x: String) = ???
myFunction1("foobar") // function invoked with single argument
myFunction1 { "foobar" } // function invoked with single argument provided by a block
// (a block of code within {}'s' evaluates to the result of its last expression)
def myFunction2(first: Int, second: String) = ???
val b = "foobar"
myFunction2(6, b) // function with two arguments
def multipleArgLists(first: Int)(second: Int, third: String) = ???
multipleArgLists(42)(17, "foobar") // function with three arguments in two argument lists
def myOptionalParam(required: Int, optional: Int = 42) = ???
myOptionalParam(1) // function with optional param
myOptionalParam(1, 2) // function with optional param provided
def allParamsOptional(firstOpt: Int = 42, secondOpt: String = "foobar") = ???
allParamsOptional() // function with all optional args
/* allParamsOptional */ // error: missing arguments for method allParamsOptional;
// follow with `_' if you want to treat it as a partially applied function
def sum[Int](values: Int*) = values.foldLeft(0)((a, b) => a + b)
sum(1, 2, 3) // function accepting variable arguments as literal
val values = List(1, 2, 3)
sum(values: _*) // function acception variable arguments from collection
sum() // function accepting empty variable arguments
def mult(firstValue: Int, otherValues: Int*) = otherValues.foldLeft(firstValue)((a, b) => a * b)
mult(1, 2, 3) // function with non-empty variable arguments
myOptionalParam(required = 1) // function called with named arguments (all functions have named arguments)
myFunction2(second = "foo", first = 1) // function with re-ordered named arguments
mult(firstValue = 1, otherValues = 2, 3) // function with named variable argument as literal
val otherValues = Seq(2, 3)
mult(1, otherValues = otherValues: _*) // function with named variable argument from collection
val result = myFunction0() // function called in an expression context
myFunction0() // function called in statement context
/* myOptionalParam(optional = 1, 2) */ // error: positional after named argument.
def transform[In, Out](initial: In)(transformation: In => Out) = transformation(initial)
val result = transform(42)(x => x * x) // function in first-class context within an expression
def divide(top: Double, bottom: Double) = top / bottom
val div = (divide _) // partial application -- defer application of entire arg list
val halve = divide(_: Double, 2) // partial application -- defer application of some arguments
class Foo(var value: Int)
def incFoo(foo: Foo) = foo.value += 1 // function showing AnyRef's are passed by reference
/* def incInt(i: Int) = i += 1 */ // error: += is not a member of Int
// (All arguments are passed by reference, but reassignment
// or setter must be defined on a type or a field
// (respectively) in order to modify its value.)
// No distinction between built-in functions and user-defined functions
// No distinction between subroutines and functions</lang>
Seed7
- Seed7 provides two kinds of subroutines: proc, which has no return value, and func, which has a return value. The return value of a func must be used by the caller (e.g. assigned to a variable). If you don't want do deal with the return value, use a proc instead.
- Seed7 supports call-by-value, call-by-reference, and call-by-name parameters. Programmers are free to specify the desired parameter passing mechanism. The most used parameter passing mechanism is 'in'. Depending on the type 'in' specifies call-by-value (for integer, float, ...) or call-by-reference (for string, array, ...). It is prohibited to write something to an 'in' parameter.
- All parameters are positional.
- There are no differences between between calling built-in vs. user defined functions.<lang seed7>env := environment; # Call a function that requires no arguments.
env := environment(); # Alternative possibility to call of a function with no arguments.
cmp := compare(i, j); # Call a function with a fixed number of arguments.</lang>
- There are no optional arguments, but a similar effect can be achieved with overloading.<lang seed7>write(aFile, "asdf"); # Variant of write with a parameter to specify a file.
write("asdf"); # Variant of write which writes to the file OUT.</lang>
- Seed7 does not support functions with a variable number of arguments. But a function argument can be an array with as many values as you want:<lang seed7>const func integer: sum (in array integer: intElems) is func
result
var integer: sum is 0;
local
var integer: element is 0;
begin
for element range intElems do
sum +:= element;
end for;
end func;
s := sum([] (1, 2, 3)); # Use an aggregate to generate an array.
t := sum([] (2, 3, 5, 7));</lang>
- Concatenation operators can be used to concatenate arguments. This solution is used to provide the write function:<lang seed7>write("Nr: " <& num); # Use operators to concatenate arguments.</lang>
- The procedure ignore can be used to ignore a return value.<lang seed7>ignore(getln(IN)); # Using a function in statement context (ignore the result).</lang>
- Call-by-name parameters use a function in first-class context. The function doMap from the examples section of the Seed7 homepage uses a given expression to modify the elements of an array:<lang seed7>seq := doMap([](1, 2, 4, 6, 10, 12, 16), x, succ(x));</lang>
SenseTalk
- If no variable is specified, `put` prints the variable to stdout
<lang sensetalk>put zeroArgsFn()
// Function calls can also be made using the following syntax:
put the zeroArgsFn
function zeroArgsFn
put "This function was run with zero arguments."
return "Return value from zero argument function"
end zeroArgsFn</lang>
- Running a function requires a keyword such as `put; if no variable is return, put into e.g. _
<lang sensetalk>put TwoArgFn("variable", (3, 4)) into _
// Alternatively, the function can be called like so:
put the TwoArgFn of "variable", (3, 4)
// The parameter list is flexible, allowing any amount of variable to be passed in.
// These can be accessed with the keyword `the parameterList`
// The specified parameters only limits to named parameters
get TwoArgFn("variable", (3, 4), "hello")
get the TwoArgFn of "variable", (3, 4), "hello"
function TwoArgFn arg1, arg2
put "2 argument function: arg1 = " & arg1 & "; arg2 = " & arg2
put "Parameters = " & the parameterList
end TwoArgFn</lang>
- A parameter is set to "" if nothing is specified
<lang sensetalk>get ThreeArgFn("variable", (3, 4))
function ThreeArgFn arg1, arg2, arg3
put "3 argument function: arg1 = " & arg1 & "; arg2 = " & arg2 & "; arg3 = " & arg3
end ThreeArgFn</lang>
- Using this, default parameter values can be set up if a check if done at the start of the function
<lang sensetalk>get OneArgFn() -- arg1 is 5
get OneArgFn(10) -- arg1 is now 10
function OneArgFn arg1
if arg1 is ""
set arg1 to 5
end if
put "One argument function; arg1 = " & arg1
end OneArgFn</lang>
- All variables are, by default, passed by value
- If the argument prefixed by 'container', the variable is passed by reference
<lang sensetalk>put 3 into a
get AddOne(a)
put "Value of a = " & a
// Value of a = 3
put 5 into b
get AddOne(container b)
put "Value of b = " & b
// Value of b = 6
function AddOne n
add 1 to n
end AddOne</lang>
SenseTalk also distinguishes between functions and subroutines, which it calls handlers:
<lang sensetalk>CustomHandler 1, 2, 3
// Prints: 1 - 2 - 3
to handle CustomHandler arg1, arg2, arg3
put arg1 && "-" && arg2 && "-" && arg3
end CustomHandler</lang>
Subroutines can be called as a command, without storing the output
<lang sensetalk>
MyCommand 1, "variable", (4, 5, 6)
to MyCommand args
...
end MyCommand
</lang>
Functions/subroutines can also be defined with the to, on or function keywords:
<lang sensetalk>to MyFn args
...
end MyFn
function MyFn args
...
end args
on MyFn args
...
end args
</lang>
Sidef
All functions in Sidef are first-class closures
<lang ruby>foo(); # without arguments
foo(1, 2); # with two arguments
foo(args...); # with a variable number of arguments
foo(name: 'Bar', age: 42); # with named arguments
var f = foo; # store the function foo inside 'f'
var result = f(); # obtain the return value of a function
var arr = [1,2,3];
foo(arr); # the arguments are passed by object-reference</lang>
Partial application is possible by using a curry function:
<lang ruby>func curry(f, *args1) {
func (*args2) {
f(args1..., args2...);
}
}
func add(a, b) {
a + b
}
var adder = curry(add, 1);
say adder(3); #=>4</lang>
Smalltalk
Where f is a closure and arguments is an array of values for f to operate on.
<lang smalltalk>f valueWithArguments: arguments.</lang>
SSEM
Assuming the subroutine has been set up in accordance with the Wheeler jump technique as described in the SSEM Function definition entry, calling it requires simply loading the return address into the accumulator and jumping out to the subroutine. Parameters must be passed using "global variables", i.e. storage locations; results may be passed the same way, although it is also possible to pass a return value in the accumulator.
This code fragment, beginning (for the sake of argument) at address 10, performs a Wheeler jump to a subroutine beginning at address 20. The return address is coded in negative (two's complement) form because the SSEM negates values in the process of loading them into the accumulator. As always on the SSEM, jump targets are one less than the actual intended target: this is because the CI ("Current Instruction") register is incremented after an instruction has been executed rather than before.
<lang ssem>00110000000000100000000000000000 10. -12 to c
10110000000000000000000000000000 11. 13 to CI
11001111111111111111111111111111 12. -13
11001000000000000000000000000000 13. 19</lang>
Swift
<lang Swift>// call a function with no args
noArgs()
// call a function with one arg with no external name
oneArgUnnamed(1)
// call a function with one arg with external name
oneArgNamed(arg: 1)
// call a function with two args with no external names
twoArgsUnnamed(1, 2)
// call a function with two args and external names
twoArgsNamed(arg1: 1, arg2: 2)
// call a function with an optional arg
// with arg
optionalArguments(arg: 1)
// without
optionalArguments() // defaults to 0
// function that takes another function as arg
funcArg(noArgs)
// variadic function
variadic(opts: "foo", "bar")
// getting a return value
let foo = returnString()
// getting a bunch of return values
let (foo, bar, baz) = returnSomeValues()
// getting a bunch of return values, discarding second returned value
let (foo, _, baz) = returnSomeValues()</lang>
Tcl
<lang tcl>aCallToACommandWithNoArguments
aCallToACommandWithOne argument
aCallToACommandWith arbitrarily many arguments
aCallToACommandWith {*}$manyArgumentsComingFromAListInAVariable
aCallToACommandWith -oneNamed argument -andAnother namedArgument
aCallToACommandWith theNameOfAnotherCommand
aCallToOneCommand [withTheResultOfAnother]</lang>
Tcl does differentiate between functions and other types of commands in expressions:
<lang tcl>expr {func() + [cmd]}
expr {func(1,2,3} + [cmd a b c]}</lang>
However, there are no deep differences between the two: functions are translated into commands that are called in a particular namespace (thus foo() becomes tcl::mathfunc::foo).
There are no differences in usage between built-in commands and user-defined ones, and parameters are passed to commands by value conceptually (and read-only reference in the implementation).
True BASIC
<lang qbasic>FUNCTION Copialo$ (txt$, siNo, final$)
FOR cont = 1 TO ROUND(siNo)
LET nuevaCadena$ = nuevaCadena$ & txt$
NEXT cont
LET Copialo$ = LTRIM$(RTRIM$(nuevaCadena$)) & final$
END FUNCTION
SUB Saludo
PRINT "Hola mundo!"
END SUB
SUB testCadenas (txt$)
FOR cont = 1 TO ROUND(LEN(txt$))
PRINT (txt$)[cont:cont+1-1]; "";
NEXT cont
END SUB
SUB testNumeros (a, b, c)
PRINT a, b, c
END SUB
CALL Saludo
PRINT Copialo$("Saludos ", 6, "")
PRINT Copialo$("Saludos ", 3, "! !")
PRINT
CALL testNumeros(1, 2, 3)
CALL testNumeros(1, 2, 0)
PRINT
CALL testCadenas("1, 2, 3, 4, cadena, 6, 7, 8, \'incluye texto\'")
END</lang>
- Output:
Igual que la entrada de FreeBASIC.
UNIX Shell
In the shell, there are no argument specifications for functions. Functions obtain their arguments using the positional parameter facilities and functions are simply called by name followed by any arguments that are to be passed:
<lang sh>sayhello # Call a function in statement context with no arguments
multiply 3 4 # Call a function in statement context with two arguments</lang>
The shell does not support the use of named parameters. There is no lookahead in the shell, so functions cannot be called until their definition has been run.
VBA
<lang vb>'definitions/declarations
'Calling a function that requires no arguments
Function no_arguments() As String
no_arguments = "ok"
End Function
'Calling a function with a fixed number of arguments
Function fixed_number(argument1 As Integer, argument2 As Integer)
fixed_number = argument1 + argument2
End Function
'Calling a function with optional arguments
Function optional_parameter(Optional argument1 = 1) As Integer
'Optional parameters come at the end of the parameter list
optional_parameter = argument1
End Function
'Calling a function with a variable number of arguments
Function variable_number(arguments As Variant) As Integer
variable_number = UBound(arguments)
End Function
'Calling a function with named arguments
Function named_arguments(argument1 As Integer, argument2 As Integer) As Integer
named_arguments = argument1 + argument2
End Function
'Using a function in statement context
Function statement() As String
Debug.Print "function called as statement"
statement = "ok"
End Function
'Using a function in first-class context within an expression
'see call the functions
'Obtaining the return value of a function
Function return_value() As String
return_value = "ok"
End Function
'Distinguishing built-in functions and user-defined functions
'There is no way to distinguish built-in function and user-defined functions
'Distinguishing subroutines And functions
'subroutines are declared with the reserved word "sub" and have no return value
Sub foo()
Debug.Print "subroutine",
End Sub
'functions are declared with the reserved word "function" and can have a return value
Function bar() As String
bar = "function"
End Function
'Stating whether arguments are passed by value or by reference
Function passed_by_value(ByVal s As String) As String
s = "written over"
passed_by_value = "passed by value"
End Function
'By default, parameters in VBA are by reference
Function passed_by_reference(ByRef s As String) As String
s = "written over"
passed_by_reference = "passed by reference"
End Function
'Is partial application possible and how
'I don't know
'calling a subroutine with arguments does not require parentheses
Sub no_parentheses(myargument As String)
Debug.Print myargument,
End Sub
'call the functions
Public Sub calling_a_function()
'Calling a function that requires no arguments
Debug.Print "no arguments", , no_arguments
Debug.Print "no arguments", , no_arguments()
'Parentheses are not required
'Calling a function with a fixed number of arguments
Debug.Print "fixed_number", , fixed_number(1, 1)
'Calling a function with optional arguments
Debug.Print "optional parameter", optional_parameter
Debug.Print "optional parameter", optional_parameter(2)
'Calling a function with a variable number of arguments
Debug.Print "variable number", variable_number([{"hello", "there"}])
'The variable number of arguments have to be passed as an array
'Calling a function with named arguments
Debug.Print "named arguments", named_arguments(argument2:=1, argument1:=1)
'Using a function in statement context
statement
'Using a function in first-class context within an expression
s = "no_arguments"
Debug.Print "first-class context", Application.Run(s)
'A function name can be passed as argument in a string
'Obtaining the return value of a function
returnvalue = return_value
Debug.Print "obtained return value", returnvalue
'Distinguishing built-in functions and user-defined functions
'Distinguishing subroutines And functions
foo
Debug.Print , bar
'Stating whether arguments are passed by value or by reference
Dim t As String
t = "unaltered"
Debug.Print passed_by_value(t), t
Debug.Print passed_by_reference(t), t
'Is partial application possible and how
'I don 't know
'calling a subroutine with arguments does not require parentheses
no_parentheses "calling a subroutine"
Debug.Print "does not require parentheses"
Call no_parentheses("deprecated use")
Debug.Print "of parentheses"
End Sub
</lang>
- Output:
no arguments ok
no arguments ok
fixed_number 2
optional parameter 1
optional parameter 2
variable number 2
named arguments 2
function called as statement
first-class context ok
obtained return value ok
subroutine function
passed by value unaltered
passed by reference written over
calling a subroutine does not require parentheses
deprecated use of parentheses
WDTE
<lang wdte>let noargs => + 2 5;
noargs -- print;
let fixedargs a b => + a b;
fixedargs 3 5 -- print;
let m => import 'math';
m.cos 3 -- print;
- WDTE only has expressions, not statements, so statement vs.
- first-class context doesn't make sense.
- Arguments in WDTE are technically passed by reference, in a way, but
- because it's a functional language and everything's immutable
- there's no real usability difference from that.
- Partial application is possible. For example, the following
- evaluates `+ 3` and then passes 7 to the resulting partially applied
- function.
(+ 3) 7 -- print;</lang>
WebAssembly
<lang webassembly>(func $main (export "_start")
(local $result i32)
;;Call a function with no arguments
call $noargfunc
;;Multiply two numbers and store the result, flat syntax
i32.const 12
i32.const 3
call $multipy
set_local $result
;;Multiply two numbers and store the result, indented syntax
(set_local $result
(call $multipy
(i32.const 12)
(i32.const 3)
)
)
;;Add two numbers in linear memory (similar to using pointers)
(i32.store (i32.const 0) (i32.const 5))
(i32.store (i32.const 4) (i32.const 7))
(call $addinmemory
(i32.const 0)
(i32.const 4)
(i32.const 8)
)
)</lang>
Wren
Wren distinguishes between functions and methods.
The former are first-class standalone objects which cannot be overloaded whereas the latter are always members of a class and can be overloaded by arity (i.e. the number of arguments they require). As Wren is dynamically typed and doesn't support type annotations, methods cannot be overloaded by parameter type. Methods can either be instance or static members of their class.
As well as 'ordinary' methods which always have a (possibly empty) parameter list, Wren also has the following types of methods:
1. 'constructors' which always have a parameter list and are really a pair of methods - a static method which creates a new instance of the class and then invokes an initializer on that instance.
2. 'getters' which leave off the parameter list.
3. 'setters' which have '=' after the name followed by a parameter list with just a single parameter.
4. 'operators' which, if they're infix operators, always have a parameter list with a single parameter. However, if they're prefix operators, they have no parameter list.
Wren does not support optional arguments, a variable number of arguments or named arguments though these can be respectively simulated by overloading, passing lists or passing maps.
Arguments are always passed by value though, if they're mutable reference types, it's the reference which gets copied so the function or method can mutate the object itself.
There are no built-in functions but all built-in classes have methods. Unless you know what they are, there is no way to distinguish between them and methods of user-defined classes.
The only way to distinguish between sub-routines and functions it to check whether they return a concrete value. The former always return null by default.
Partial function application is not supported per se but can be simulated as in the task of that name.
Here are some examples:
<lang ecmascript>var f1 = Fn.new { System.print("Function 'f1' with no arguments called.") }
var f2 = Fn.new { |a, b|
System.print("Function 'f2' with 2 arguments called and passed %(a) & %(b).")
}
var f3 = Fn.new { 42 } // function which returns a concrete value
f1.call() // statement context
f2.call(2, 3) // ditto
var v1 = 8 + f3.call() // calling function within an expression
var v2 = f3.call() // obtaining return value
System.print([v1, v2]) // print last two results as a list
class MyClass {
static m() { System.print("Static method 'm' called.") }
construct new(x) { _x = x } // stores 'x' in a field
x { _x } // gets the field
x=(y) { _x = y } // sets the field to 'y'
- { MyClass.new(-_x) } // prefix operator
+(o) { MyClass.new(_x + o.x) } // infix operator
toString { _x.toString } // instance method
}
MyClass.m() // call static method 'm'
var mc1 = MyClass.new(40) // construct 'mc1'
var mc2 = MyClass.new(8) // construct 'mc2'
System.print(mc1.x) // print mc1's field using getter
mc1.x = 42 // change mc1's field using setter
System.print(-mc1.x) // invoke prefix operator -
System.print(mc1 + mc2) // invoke infix operator +</lang>
- Output:
Function 'f1' with no arguments called.
Function 'f2' with 2 arguments called and passed 2 & 3.
[50, 42]
Static method 'm' called.
40
-42
50
XLISP
<lang lisp>; call a function (procedure) with no arguments:
(foo)
- call a function (procedure) with arguments
(foo bar baz)
- the first symbol after "(" is the name of the function
- the other symbols are the arguments
- call a function on a list of arguments formed at run time
(apply foo bar)
- In a REPL, the return value will be printed.
- In other contexts, it can be fed as argument into a further function
(foo (bar baz))
- this calls bar on the argument baz and then calls foo on the return value
- or it can simply be discarded
(foo bar)
- nothing is done with the return value</lang>
XSLT
<lang xml><?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<demo>
<xsl:call-template name="xpath-function-demos"/>
<xsl:call-template name="xslt-template-demos"/>
</demo>
</xsl:template>
<xsl:template name="xpath-function-demos">
<line>This test is <xsl:if test="false()">NOT</xsl:if> OK.</line>
<line>'haystack' does <xsl:if test="not(starts-with('haystack', 'hay'))">NOT</xsl:if> start with 'hay'.</line>
<line>'<xsl:value-of select="substring('haystack', 1, 3)"/>' = 'hay'</line>
<line>'<xsl:value-of select="substring('haystack', 4)"/>' = 'stack'</line>
<line>'<xsl:value-of select="concat('abcd', 'efgh')"/>' = 'abcdefgh'</line>
<line>'<xsl:value-of select="concat('ij', 'kl', 'mn', 'op')"/>' = 'ijklmnop'</line>
<line>The number of root elements in the input document is <xsl:value-of select="count(/*)"/> (should be 1).</line>
<line foo="{concat('Hello, ', 'Hello, ', 'Hello')}!">See attribute.</line>
</xsl:template>
<xsl:template name="xslt-template-demos">
<xsl:call-template name="nullary-demo"/>
<xsl:apply-templates select="/*" mode="nullary-context-demo-mode"/>
<working note="When all parameters are supplied">
<xsl:call-template name="ternary-demo">
<xsl:with-param name="a" select="4"/>
<xsl:with-param name="b">3</xsl:with-param>
<xsl:with-param name="c" select="2 + 3"/>
</xsl:call-template>
</working>
<broken note="When the third parameter 'c' is omitted">
<xsl:call-template name="ternary-demo">
<xsl:with-param name="a" select="4"/>
<xsl:with-param name="b">3</xsl:with-param>
</xsl:call-template>
</broken>
<working name="When all parameters are supplied">
<xsl:call-template name="binary-or-ternary-demo">
<xsl:with-param name="a" select="4"/>
<xsl:with-param name="b" select="3"/>
<xsl:with-param name="c" select="5"/>
</xsl:call-template>
</working>
<working name="When 'a' and 'b' are supplied but 'c' is defaulted to 0">
<xsl:call-template name="binary-or-ternary-demo">
<xsl:with-param name="a" select="4"/>
<xsl:with-param name="b" select="3"/>
</xsl:call-template>
</working>
<xsl:variable name="lesser-demo-result">
<xsl:call-template name="lesser-value">
<xsl:with-param name="a">
<xsl:call-template name="lesser-value">
<xsl:with-param name="a" select="34"/>
<xsl:with-param name="b" select="78"/>
</xsl:call-template>
</xsl:with-param>
<xsl:with-param name="b">
<xsl:call-template name="lesser-value">
<xsl:with-param name="a" select="12"/>
<xsl:with-param name="b" select="56"/>
</xsl:call-template>
</xsl:with-param>
</xsl:call-template>
</xsl:variable>
<line>
<xsl:value-of select="concat('And the answer, which should be 12, is ', $lesser-demo-result, ', of course.')"/>
</line>
</xsl:template>
<xsl:template match="/*" mode="nullary-context-demo-mode">
<xsl:call-template name="nullary-context-demo"/>
</xsl:template>
<xsl:template name="nullary-demo">
<line>No parameters needed here!</line>
</xsl:template>
<xsl:template name="nullary-context-demo">
<xsl:for-each select="self::*">
<line>The context element here is named "<xsl:value-of select="local-name()"/>"</line>
</xsl:for-each>
</xsl:template>
<xsl:template name="ternary-demo">
<xsl:param name="a"/>
<xsl:param name="b"/>
<xsl:param name="c"/>
<line>(<xsl:value-of select="$a"/> * <xsl:value-of select="$b"/>) + <xsl:value-of select="$c"/> = <xsl:value-of select="($a * $b) + $c"/></line>
</xsl:template>
<xsl:template name="binary-or-ternary-demo">
<xsl:param name="a"/>
<xsl:param name="b"/>
<xsl:param name="c" select="0"/>
<line>(<xsl:value-of select="$a"/> * <xsl:value-of select="$b"/>) + <xsl:value-of select="$c"/> = <xsl:value-of select="($a * $b) + $c"/></line>
</xsl:template>
<xsl:template name="lesser-value">
<xsl:param name="a"/>
<xsl:param name="b"/>
<xsl:choose>
<xsl:when test="number($a) < number($b)">
<xsl:value-of select="$a"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$b"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
</lang>
Yabasic
<lang Yabasic>
sub test(a, b, c) : print a, b, c : end sub
test(1, 2, 3) // show 1 2 3
test(1, 2) // show 1 2 0
execute("test", 1, 2, 3) // show 1 2 3
sub test$(a$) // show all members of a "list"
local n, i, t$(1)
n = token(a$, t$(), ", ")
for i = 1 to n
print t$(i), " ";
next
end sub
test$("1, 2, 3, 4, text, 6, 7, 8, \"include text\"")
print</lang>
zkl
The syntax and semantics of function calls is the always the same: name/object(parameters). All calls are varargs, it is up to the callee to do default/optional parameter handling (but that is hidden from the programmer). No named parameters. Pass by reference or value, depending.
Using f has a function, method or object:
<lang zkl>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</lang>
Partial application is done with the .fp* methods or the 'wrap keyword
<lang zkl>
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))</lang>
zonnon
<lang zonnon>
module CallingProcs;
type
{public} Vector = array {math} * of integer;
var
nums: array {math} 4 of integer;
ints: Vector;
total: integer;
procedure Init(): boolean; (* private by default *)
begin
nums := [1,2,3,4];
ints := new Vector(5);
ints := [2,4,6,8,10];
return true;
end Init;
(* function *)
procedure Sum(v: Vector): integer;
var
i,s: integer;
begin
s := 0;
for i := 0 to len(v) - 1 do
(* inc is a predefined subroutine *)
inc(s,v[i])
end;
return s
end Sum;
(* subroutine
* @param v: by value
* @param t: by reference
*)
procedure Sum2(v: array {math} * of integer; var t: integer);
var
i: integer;
begin
t := 0;
for i := 0 to len(v) - 1 do
inc(t,v[i])
end
end Sum2;
begin
Init; (* calling a function without parameters *)
total := Sum(nums);
writeln(total);
(* optional arguments not supported *)
(* variable arguments through open arrays *)
writeln(Sum(ints));
(* named arguments not supported *)
ints := [1,3,5,7,9];
Sum2(ints,total);
writeln(total);
end CallingProcs.
</lang>
Z80 Assembly
Like most assembly languages, the concept of functions are too "high-level" for most of the task to apply. However, subroutines do exist and they are used like a function in a high-level language would be.
- Functions that require no arguments are the simplest. They are simply
CALL
ed without any special input.
- Functions that take a fixed number of arguments usually require the programmer to load the arguments into registers or push them to the stack prior to calling the function.
<lang z80>PackNibbles:
- input
- B = top nibble, C = bottom nibble. Outputs to accumulator.
- usage
- B = &0X, C = &0Y, => A = &XY
LD A,B
AND %00001111
RLCA
RLCA
RLCA
RLCA
OR C
RET</lang>
- Assembly language in general has difficulty with optional and variable numbers of arguments (unless it is written by a compiler, of course). Prior to the function call, an optional argument would most likely be set to zero if intended to be unused in a particular instance of a function.
- Z80 Assembly does not support named arguments directly, but this can be achieved with assembly macros and labels.
- Getting a function's return value depends on how it was programmed. The concept of "return values" is a high-level construct that isn't enforced by assembly languages in general, and Z80 is no exception. The programmer is free to choose which register or section of memory the return value of a function is stored, if one even exists. Generally speaking the accumulator is usually used for this purpose but it depends on the data type as well as the needs of the program.
<lang z80>AddTwoNumbers
- input registers
- A,B. Outputs to A.
ADD a,b
RET</lang>
- Some implementations of Z80 Assembly have built-in functions. These are essentially just subroutines located in ROM at a specific memory address. Functions stored in low memory can be called with the
RST #
instruction. Anything in high memory will need to be CALL
ed like any other user-created subroutine. On the Amstrad CPC, CALL &BB5A
will print the accumulator to the screen as an ASCII character.
- Whether an argument is passed by value or by reference is again up to the programmer. By default, Z80 Assembly is pass-by-value, since it's much easier to operate on registers than directly on memory. The function would have to explicitly store the updated values back into memory for them to be altered. However, some Z80 instructions do directly alter memory, such as
RLD
,RRD
,LDIR
,etc.
ZX Spectrum Basic
On the ZX Spectrum, functions and subroutines are separate entities. A function is limited to being a single expression that generates a return value. Statements are not allowed within a function. A subroutine can perform input and output and can contain statements.
<lang zxbasic>10 REM functions cannot be called in statement context
20 PRINT FN a(5): REM The function is used in first class context. Arguments are not named
30 PRINT FN b(): REM Here we call a function that has no arguments
40 REM subroutines cannot be passed parameters, however variables are global
50 LET n=1: REM This variable will be visible to the called subroutine
60 GO SUB 1000: REM subroutines are called by line number and do not have names
70 REM subroutines do not return a value, but we can see any variables it defined
80 REM subroutines cannot be used in first class context
90 REM builtin functions are used in first class context, and do not need the FN keyword prefix
100 PRINT SIN(50): REM here we pass a parameter to a builtin function
110 PRINT RND(): REM here we use a builtin function without parameters
120 RANDOMIZE: REM statements are not functions and cannot be used in first class context.</lang>
- Programming Tasks
- Solutions by Programming Task
- Functions and subroutines
- Simple
- 11l
- 360 Assembly
- 6502 Assembly
- 68000 Assembly
- 8086 Assembly
- AArch64 Assembly
- ActionScript
- Ada
- ALGOL 68
- ALGOL W
- AntLang
- ARM Assembly
- Arturo
- AutoHotkey
- AWK
- Axe
- BASIC256
- Batch File
- BBC BASIC
- BQN
- Bracmat
- C
- C sharp
- C++
- COBOL
- Clojure
- CoffeeScript
- Common Lisp
- Cubescript
- D
- Dart
- Delphi
- Dragon
- Dyalect
- Déjà Vu
- Elena
- Elixir
- Erlang
- F Sharp
- Factor
- Forth
- Fortran
- Fortress
- FreeBASIC
- Free Pascal
- Gambas
- Go
- Groovy
- Haskell
- I
- Icon
- Unicon
- J
- Java
- JavaScript
- Jq
- Julia
- Kotlin
- Lambdatalk
- Langur
- Latitude
- LFE
- Liberty BASIC
- Lingo
- Little
- Lua
- Luck
- M2000 Interpreter
- Maple
- Mathematica
- Wolfram Language
- MATLAB
- Octave
- Nanoquery
- Nemerle
- Nim
- OCaml
- Oforth
- Ol
- OoRexx
- PARI/GP
- Pascal
- Perl
- Phix
- Phix/basics
- Phixmonti
- PicoLisp
- PureBasic
- Python
- QBasic
- Quackery
- R
- Racket
- Raku
- REXX
- Ring
- Ruby
- Rust
- Scala
- Seed7
- SenseTalk
- Sidef
- Smalltalk
- SSEM
- Swift
- Tcl
- True BASIC
- UNIX Shell
- VBA
- WDTE
- WebAssembly
- Wren
- XLISP
- XSLT
- Yabasic
- Zkl
- Zonnon
- Z80 Assembly
- ZX Spectrum Basic
- GUISS/Omit