Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Groovy: Methods

Methods in Groovy are Defined with a return type or with the def keyword. If a method is defined with specific type, then it should return the value of that type only.

int fact(n){
if(n == 0 || n == 1) return 1

return n * fact(n-1)

int result = fact(5)

println "Factorial of 5 is $result"

Factorial of 5 is 120

In the above example, I defined a method ‘fact’ which takes an Integer as input and return an integer value as output.

If you make the return type of the method as def, then you can return value of any type.

This post first appeared on Java Tutorial : Blog To Learn Java Programming, please read the originial post: here

Share the post

Groovy: Methods


Subscribe to Java Tutorial : Blog To Learn Java Programming

Get updates delivered right to your inbox!

Thank you for your subscription