Call a function: Difference between revisions

Content added Content deleted
No edit summary
(Added Dyalect programming language)
Line 1,107: Line 1,107:
* Calling a function with a fixed number of arguments
* Calling a function with a fixed number of arguments
<lang dragon>myMethod(97, 3.14)</lang>
<lang dragon>myMethod(97, 3.14)</lang>

=={{header|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)</

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>//Not possible in Dyalect at the moment</lang>

Distinguishing subroutines and functions:

<lang Dyalect>//There is no difference between subroutines and functions:
func foo() { } //doesn't explicitely return something (but in fact returns nil)
func bar(x) { return x * 2 } //explicitely 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>




=={{header|Elena}}==
=={{header|Elena}}==