Call a function: Difference between revisions
Content added Content deleted
m (→{{header|Scala}}: Rm Scala imp category) |
(→{{header|Go}}: Add Go (I thought Go was already described here, but I can't find it in the history)) |
||
Line 364: | Line 364: | ||
% Partial application is possible (a function returns a function that has one argument bound) |
% Partial application is possible (a function returns a function that has one argument bound) |
||
</lang> |
</lang> |
||
=={{header|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 <code>h</code> above). <lang go> h("ex1") |
|||
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 <code>gif.Options</code> is a structure with multiple members which can initialized/assigned by name or omitted (or the whole third argument can just be <code>nil</code>). <lang go> gif.Encode(ioutil.Discard, image.Black, &gif.Options{NumColors: 16})</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). |
|||
* Partial application is not directly supported. |
|||
::However something similar can be done, see [[Partial function application#Go]] |
|||
=={{header|Icon}} and {{header|Unicon}}== |
=={{header|Icon}} and {{header|Unicon}}== |