diff options
author | Muhammad Usama <lifeh2o@gmail.com> | 2019-08-30 19:58:06 +0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-08-30 19:58:06 +0500 |
commit | ec1090d7a8758cb3759ba903b946594257e309f7 (patch) | |
tree | 085b85c2845e08e25b306b8f919199f5dd4dc86f /groovy.html.markdown | |
parent | f0f161981f2e3d35967878b6a7a2c71de75b2b64 (diff) |
Optional parameters in methods
Diffstat (limited to 'groovy.html.markdown')
-rw-r--r-- | groovy.html.markdown | 15 |
1 files changed, 15 insertions, 0 deletions
diff --git a/groovy.html.markdown b/groovy.html.markdown index efbb2b32..89ca973a 100644 --- a/groovy.html.markdown +++ b/groovy.html.markdown @@ -181,6 +181,21 @@ class Foo { } /* + 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 */ |