1
0
mirror of https://github.com/adambard/learnxinyminutes-docs.git synced 2025-07-31 12:00:34 +02:00

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

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