mirror of
https://github.com/adambard/learnxinyminutes-docs.git
synced 2024-12-25 08:13:29 +03:00
Merge pull request #3614 from SMUsamaShah/patch-1
Optional parameters in methods
This commit is contained in:
commit
4e8e92eb0e
@ -180,6 +180,21 @@ class Foo {
|
||||
def lastName
|
||||
}
|
||||
|
||||
/*
|
||||
Methods with optional parameters
|
||||
*/
|
||||
|
||||
// A mthod can have default values for parameters
|
||||
def say(msg = 'Hello', name = 'world') {
|
||||
"$msg $name!"
|
||||
}
|
||||
|
||||
// It can be called in 3 different ways
|
||||
assert 'Hello world!' == say()
|
||||
// Right most parameter with default value is eliminated first.
|
||||
assert 'Hi world!' == say('Hi')
|
||||
assert 'learn groovy' == say('learn', 'groovy')
|
||||
|
||||
/*
|
||||
Logical Branching and Looping
|
||||
*/
|
||||
|
Loading…
Reference in New Issue
Block a user