Optional parameters in methods

This commit is contained in:
Muhammad Usama 2019-08-30 19:58:06 +05:00 committed by GitHub
parent f0f161981f
commit ec1090d7a8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -180,6 +180,21 @@ class Foo {
def lastName 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 Logical Branching and Looping
*/ */