Functions in Scala
A function is a collection of statements that perform a certain task. One can divide up the code into separate functions. Functions make code easy to understand and easily debugged.
Declaration of Functions in Scala:
def function_name([parameters]):[return_type]={ //code goes here }
Expansions:
def: It is a keyword that is used to declare a function in scala
function_name : The name of the function which is used to call the trigger. It may consists of lower camel case alphabets and ascii characters.
parameters : Parameters that are used to run the code in the function with comma separated values followed by parameter type.
return_type : The return type of function is optional. If the function return any value ], the datatype of that value shoul br mentioned at return_type.
=: equal to operator is used in scala when the function will return any value. If equal to is not given then the function will not return any value and it is used as subroutine.
Function Calling:
To implement the code that is written in function, the function should be called. There are two ways to call a function in scala.
1.) function_name(paramters)
2.)(instance).function_name(parameters)
Functions with '=' operator:
Output: 30
Functions without '=' operator:
Uses of Functions: