Jump to content

Named parameters: Difference between revisions

Line 1,499:
Swift has local and external (named) parameters.
 
IfYou youjust want thespecify external parameterfollowed name to match theby local parameter name, you prefix the name with a #.
<lang Swift>func isGreater(#x x:Int, thanY y:Int) -> Bool {
If not you just specify external followed by local name
<lang Swift>func isGreater(#x:Int, thanY y:Int) -> Bool {
return x > y
}
assert(isGreater(x: 5, thanY: 10) == false)</lang>
 
If you do not want the caller to specify parameter names, explicitly use <code>_</code> as external parameter name:
<lang Swift>func isGreater(x:Int, _ y:Int) -> Bool {
return x > y
}
assert(isGreater(5, 10) == false)</lang>
 
Note that when you don't explicitly specify external parameter names, the behavior is different for functions, methods and initializers:
* For a function or method, the first parameter doesn't have an external parameter name by default (as if you used <code>_</code>) unless you specify it; the rest of the parameters automaticallyby default take the local parameter name as external parameter name if you don't specify one (as if you did <code>#</code>).
* For an initializer, all parameters automaticallyby default take the local parameter name as external parameter name if you don't specify one (as if you did <code>#</code>).
 
=={{header|Tcl}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.