Merge pull request #3614 from SMUsamaShah/patch-1

Optional parameters in methods
This commit is contained in:
Divay Prakash 2019-08-30 21:36:36 +05:30 committed by GitHub
commit 4e8e92eb0e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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
*/