From ec1090d7a8758cb3759ba903b946594257e309f7 Mon Sep 17 00:00:00 2001 From: Muhammad Usama Date: Fri, 30 Aug 2019 19:58:06 +0500 Subject: Optional parameters in methods --- groovy.html.markdown | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/groovy.html.markdown b/groovy.html.markdown index efbb2b32..89ca973a 100644 --- a/groovy.html.markdown +++ b/groovy.html.markdown @@ -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 */ -- cgit v1.2.3