Jump to content

Call a function: Difference between revisions

rearranges in order of the language.
m (Reformatted ALGOL 68 example.)
(rearranges in order of the language.)
Line 506:
% Partial application is possible (a function returns a function that has one argument bound)
</lang>
 
=={{header|F Sharp|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>
 
=={{header|Forth}}==
Line 533 ⟶ 578:
: right ( n -- ) 0 move ;
: left ( n -- ) negate right ;</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}}==
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#Icon_and_Unicon|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%2BUnicon/Intro|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>
 
=={{header|Fortran}}==
Line 741 ⟶ 682:
</pre>
 
=={{header|F Sharp|F#Go}}==
The following examples use functions from the standard packages
<lang fsharp>// No arguments
plus a few dummy local functions:
noArgs()
::<lang go>import (
"image"
"image/gif"
"io/ioutil"
"strings"
"unicode"
)
 
func f() (int, float64) { return 0, 0 }
// Fixed number of arguments
func g(int, float64) int { return 0 }
oneArg x
func h(string, ...int) {}</lang>
 
* Calling with no arguments and calling with a fixed number of arguments:
// Optional arguments
::<lang go> f()
// In a normal function:
g(1, 2.0)
optionalArgs <| Some(5) <| None
// If f() is defined to return exactly the number and type of
// In a function taking a tuple:
// arguments that g() accepts than they can be used in place:
optionalArgsInTuple(Some(5), None)
g(f())
// In a function in a type:
// But only without other arguments, this won't compile:
foo.optionalArgs 5;;
//h("fail", f())
// However, if you want to pass more than one paramter, the arguments must be
// passedBut inthis a tuplewill:
g(g(1, 2.0), 3.0)</lang>
foo.optionalArgs(5, 6)
* 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")
// Function with a variable number of arguments
h("ex2", 1, 2)
variableArgs 5 6 7 // etc...
h("ex3", 1, 2, 3, 4)
 
// Namedsuch argumentsfunctions can onlyalso be usedcalled inby type methods takingexpanding a tuple. Theslice:
list := []int{1,2,3,4}
// arguments can appear in any order.
h("ex4", list...)
foo.namedArgs(x = 5, y = 6)
// but again, not mixed with other arguments, this won't compile:
 
//h("fail", 2, list...)</lang>
// Using a function in a statement
* Optional arguments and named arguments are not supported.
for i = 0 to someFunc() do
::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>
printfn "Something"
* 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>
// Using a function in a first-class context
* In a first-class context:
funcArgs someFunc
::<lang go> fn := func(r rune) rune {
 
if unicode.IsSpace(r) {
// Obtaining a return value
return -1
let x = someFunc()
}
 
return r
// Built-in functions: do functions like (+) or (-) count?
}
 
strings.Map(fn, "Spaces removed")
// Parameters are normally passed by value (as shown in the previous examples),
strings.Map(unicode.ToLower, "Test")
// but they can be passed by reference.
strings.Map(func(r rune) rune { return r + 1 }, "shift")</lang>
// Passing by reference:
* Obtaining the value:
refArgs &mutableVal
::<lang> a, b := f() // multivalue return
 
_, c := f() // only some of a multivalue return
// Partial application example
d := g(a, c) // single return value
let add2 = (+) 2</lang>
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|Haskell}}==
Line 818 ⟶ 775:
-- Stating whether arguments are passed by value or by reference
</lang>
 
=={{header|Icon}} and {{header|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#Icon_and_Unicon|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%2BUnicon/Intro|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>
 
=={{header|J}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.